Skip to content

Request: custom names for subnets and route tables #167

@RupertExact

Description

@RupertExact

It would be very helpful to be able to use custom names in subnet and route table Name tags. Alternatively, a way to create additional custom subnets and route tables in addition to the 3 provided.

variables.tf
`variable "public_subnet_suffix" {
description = "Suffix to append to public subnets name"
default = "public"
}

variable "private_subnet_suffix" {
description = "Suffix to append to private subnets name"
default = "private"
}

variable "database_subnet_suffix" {
description = "Suffix to append to database subnets name"
default = "database"
}

variable "redshift_subnet_suffix" {
description = "Suffix to append to redshift subnets name"
default = "redshift"
}

variable "elasticache_subnet_suffix" {
description = "Suffix to append to elasticache subnets name"
default = "elasticache"
}`

main.tf
`#################

Private routes

There are so many routing tables as the largest amount of subnets of each type (really?)

#################
resource "aws_route_table" "private" {
count = "${var.create_vpc && local.max_subnet_length > 0 ? local.nat_gateway_count : 0}"

vpc_id = "${local.vpc_id}"

tags = "${merge(map("Name", (var.single_nat_gateway ? "${var.name}-${var.private_subnet_suffix}" : format("%s-${var.private_subnet_suffix}-%s", var.name, element(var.azs, count.index)))), var.private_route_table_tags, var.tags)}"

lifecycle {
# When attaching VPN gateways it is common to define aws_vpn_gateway_route_propagation
# resources that manipulate the attributes of the routing table (typically for the private subnets)
ignore_changes = ["propagating_vgws"]
}
}

#################

Database routes

#################
resource "aws_route_table" "database" {
count = "${var.create_vpc && var.create_database_subnet_route_table && length(var.database_subnets) > 0 ? 1 : 0}"

vpc_id = "${local.vpc_id}"

tags = "${merge(var.tags, var.database_route_table_tags, map("Name", "${var.name}-${var.database_subnet_suffix}"))}"
}

#################

Redshift routes

#################
resource "aws_route_table" "redshift" {
count = "${var.create_vpc && var.create_redshift_subnet_route_table && length(var.redshift_subnets) > 0 ? 1 : 0}"

vpc_id = "${local.vpc_id}"

tags = "${merge(var.tags, var.redshift_route_table_tags, map("Name", "${var.name}-${var.redshift_subnet_suffix}"))}"
}

#################

Elasticache routes

#################
resource "aws_route_table" "elasticache" {
count = "${var.create_vpc && var.create_elasticache_subnet_route_table && length(var.elasticache_subnets) > 0 ? 1 : 0}"

vpc_id = "${local.vpc_id}"

tags = "${merge(var.tags, var.elasticache_route_table_tags, map("Name", "${var.name}-${var.elasticache_subnet_suffix}"))}"
}

#################

Intra routes

#################
resource "aws_route_table" "intra" {
count = "${var.create_vpc && length(var.intra_subnets) > 0 ? 1 : 0}"

vpc_id = "${local.vpc_id}"

tags = "${merge(map("Name", "${var.name}-intra"), var.intra_route_table_tags, var.tags)}"
}

################

Public subnet

################
resource "aws_subnet" "public" {
count = "${var.create_vpc && length(var.public_subnets) > 0 && (!var.one_nat_gateway_per_az || length(var.public_subnets) >= length(var.azs)) ? length(var.public_subnets) : 0}"

vpc_id = "${local.vpc_id}"
cidr_block = "${var.public_subnets[count.index]}"
availability_zone = "${element(var.azs, count.index)}"
map_public_ip_on_launch = "${var.map_public_ip_on_launch}"

tags = "${merge(map("Name", format("%s-${var.public_subnet_suffix}-%s", var.name, element(var.azs, count.index))), var.public_subnet_tags, var.tags)}"
}

#################

Private subnet

#################
resource "aws_subnet" "private" {
count = "${var.create_vpc && length(var.private_subnets) > 0 ? length(var.private_subnets) : 0}"

vpc_id = "${local.vpc_id}"
cidr_block = "${var.private_subnets[count.index]}"
availability_zone = "${element(var.azs, count.index)}"

tags = "${merge(map("Name", format("%s-${var.private_subnet_suffix}-%s", var.name, element(var.azs, count.index))), var.private_subnet_tags, var.tags)}"
}

##################

Database subnet

##################
resource "aws_subnet" "database" {
count = "${var.create_vpc && length(var.database_subnets) > 0 ? length(var.database_subnets) : 0}"

vpc_id = "${local.vpc_id}"
cidr_block = "${var.database_subnets[count.index]}"
availability_zone = "${element(var.azs, count.index)}"

tags = "${merge(map("Name", format("%s-${var.database_subnet_suffix}-%s", var.name, element(var.azs, count.index))), var.database_subnet_tags, var.tags)}"
}

resource "aws_db_subnet_group" "database" {
count = "${var.create_vpc && length(var.database_subnets) > 0 && var.create_database_subnet_group ? 1 : 0}"

name = "${lower(var.name)}"
description = "Database subnet group for ${var.name}"
subnet_ids = ["${aws_subnet.database.*.id}"]

tags = "${merge(map("Name", format("%s", var.name)), var.database_subnet_group_tags, var.tags)}"
}

##################

Redshift subnet

##################
resource "aws_subnet" "redshift" {
count = "${var.create_vpc && length(var.redshift_subnets) > 0 ? length(var.redshift_subnets) : 0}"

vpc_id = "${local.vpc_id}"
cidr_block = "${var.redshift_subnets[count.index]}"
availability_zone = "${element(var.azs, count.index)}"

tags = "${merge(map("Name", format("%s-${var.redshift_subnet_suffix}-%s", var.name, element(var.azs, count.index))), var.redshift_subnet_tags, var.tags)}"
}

resource "aws_redshift_subnet_group" "redshift" {
count = "${var.create_vpc && length(var.redshift_subnets) > 0 ? 1 : 0}"

name = "${var.name}"
description = "Redshift subnet group for ${var.name}"
subnet_ids = ["${aws_subnet.redshift.*.id}"]

tags = "${merge(map("Name", format("%s", var.name)), var.redshift_subnet_group_tags, var.tags)}"
}

#####################

ElastiCache subnet

#####################
resource "aws_subnet" "elasticache" {
count = "${var.create_vpc && length(var.elasticache_subnets) > 0 ? length(var.elasticache_subnets) : 0}"

vpc_id = "${local.vpc_id}"
cidr_block = "${var.elasticache_subnets[count.index]}"
availability_zone = "${element(var.azs, count.index)}"

tags = "${merge(map("Name", format("%s-${var.elasticache_subnet_suffix}-%s", var.name, element(var.azs, count.index))), var.elasticache_subnet_tags, var.tags)}"
}

resource "aws_elasticache_subnet_group" "elasticache" {
count = "${var.create_vpc && length(var.elasticache_subnets) > 0 ? 1 : 0}"

name = "${var.name}"
description = "ElastiCache subnet group for ${var.name}"
subnet_ids = ["${aws_subnet.elasticache.*.id}"]
}`

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions