diff --git a/main.tf b/main.tf index 4db4996db..dae3cf3b9 100644 --- a/main.tf +++ b/main.tf @@ -4,6 +4,7 @@ terraform { locals { max_subnet_length = "${max(length(var.private_subnets), length(var.elasticache_subnets), length(var.database_subnets), length(var.redshift_subnets))}" + nat_gateway_count = "${var.single_nat_gateway ? 1 : local.max_subnet_length}" } ###### @@ -80,11 +81,11 @@ resource "aws_route" "public_internet_gateway" { # 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.max_subnet_length : 0}" + count = "${var.create_vpc && local.max_subnet_length > 0 ? local.nat_gateway_count : 0}" vpc_id = "${aws_vpc.this.id}" - tags = "${merge(var.tags, var.private_route_table_tags, map("Name", format("%s-private-%s", var.name, element(var.azs, count.index))))}" + tags = "${merge(var.tags, var.private_route_table_tags, map("Name", (var.single_nat_gateway ? "${var.name}-private" : format("%s-private-%s", var.name, element(var.azs, count.index)))))}" lifecycle { # When attaching VPN gateways it is common to define aws_vpn_gateway_route_propagation @@ -203,7 +204,7 @@ locals { } resource "aws_eip" "nat" { - count = "${var.create_vpc && (var.enable_nat_gateway && !var.reuse_nat_ips) ? (var.single_nat_gateway ? 1 : length(var.azs)) : 0}" + count = "${var.create_vpc && (var.enable_nat_gateway && !var.reuse_nat_ips) ? local.nat_gateway_count : 0}" vpc = true @@ -211,7 +212,7 @@ resource "aws_eip" "nat" { } resource "aws_nat_gateway" "this" { - count = "${var.create_vpc && var.enable_nat_gateway ? (var.single_nat_gateway ? 1 : length(var.azs)) : 0}" + count = "${var.create_vpc && var.enable_nat_gateway ? local.nat_gateway_count : 0}" allocation_id = "${element(local.nat_gateway_ips, (var.single_nat_gateway ? 0 : count.index))}" subnet_id = "${element(aws_subnet.public.*.id, (var.single_nat_gateway ? 0 : count.index))}" @@ -222,7 +223,7 @@ resource "aws_nat_gateway" "this" { } resource "aws_route" "private_nat_gateway" { - count = "${var.create_vpc && var.enable_nat_gateway ? length(var.private_subnets) : 0}" + count = "${var.create_vpc && var.enable_nat_gateway ? local.nat_gateway_count : 0}" route_table_id = "${element(aws_route_table.private.*.id, count.index)}" destination_cidr_block = "0.0.0.0/0" @@ -246,7 +247,7 @@ resource "aws_vpc_endpoint" "s3" { } resource "aws_vpc_endpoint_route_table_association" "private_s3" { - count = "${var.create_vpc && var.enable_s3_endpoint ? length(var.private_subnets) : 0}" + count = "${var.create_vpc && var.enable_s3_endpoint ? local.nat_gateway_count : 0}" vpc_endpoint_id = "${aws_vpc_endpoint.s3.id}" route_table_id = "${element(aws_route_table.private.*.id, count.index)}" @@ -276,7 +277,7 @@ resource "aws_vpc_endpoint" "dynamodb" { } resource "aws_vpc_endpoint_route_table_association" "private_dynamodb" { - count = "${var.create_vpc && var.enable_dynamodb_endpoint ? length(var.private_subnets) : 0}" + count = "${var.create_vpc && var.enable_dynamodb_endpoint ? local.nat_gateway_count : 0}" vpc_endpoint_id = "${aws_vpc_endpoint.dynamodb.id}" route_table_id = "${element(aws_route_table.private.*.id, count.index)}" @@ -296,28 +297,28 @@ resource "aws_route_table_association" "private" { count = "${var.create_vpc && length(var.private_subnets) > 0 ? length(var.private_subnets) : 0}" subnet_id = "${element(aws_subnet.private.*.id, count.index)}" - route_table_id = "${element(aws_route_table.private.*.id, count.index)}" + route_table_id = "${element(aws_route_table.private.*.id, (var.single_nat_gateway ? 0 : count.index))}" } resource "aws_route_table_association" "database" { count = "${var.create_vpc && length(var.database_subnets) > 0 ? length(var.database_subnets) : 0}" subnet_id = "${element(aws_subnet.database.*.id, count.index)}" - route_table_id = "${element(aws_route_table.private.*.id, count.index)}" + route_table_id = "${element(aws_route_table.private.*.id, (var.single_nat_gateway ? 0 : count.index))}" } resource "aws_route_table_association" "redshift" { count = "${var.create_vpc && length(var.redshift_subnets) > 0 ? length(var.redshift_subnets) : 0}" subnet_id = "${element(aws_subnet.redshift.*.id, count.index)}" - route_table_id = "${element(aws_route_table.private.*.id, count.index)}" + route_table_id = "${element(aws_route_table.private.*.id, (var.single_nat_gateway ? 0 : count.index))}" } resource "aws_route_table_association" "elasticache" { count = "${var.create_vpc && length(var.elasticache_subnets) > 0 ? length(var.elasticache_subnets) : 0}" subnet_id = "${element(aws_subnet.elasticache.*.id, count.index)}" - route_table_id = "${element(aws_route_table.private.*.id, count.index)}" + route_table_id = "${element(aws_route_table.private.*.id, (var.single_nat_gateway ? 0 : count.index))}" } resource "aws_route_table_association" "public" {