Skip to content

Commit 93bae33

Browse files
Create only required number of NAT gateways
1 parent a51ad01 commit 93bae33

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

main.tf

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ locals {
55
length(var.database_subnets),
66
length(var.redshift_subnets),
77
)
8-
nat_gateway_count = var.single_nat_gateway ? 1 : var.one_nat_gateway_per_az ? length(var.azs) : local.max_subnet_length
8+
nat_gateway_count = var.single_nat_gateway ? 1 : var.one_nat_gateway_per_az ? length(var.azs) : local.max_subnet_length < length(var.public_subnets) ? local.max_subnet_length : length(var.public_subnets)
99

1010
# Use `local.vpc_id` to give a hint to Terraform that subnets should be deleted before secondary CIDR blocks can be free!
1111
vpc_id = element(
@@ -200,7 +200,7 @@ resource "aws_route" "public_internet_gateway_ipv6" {
200200
# There are as many routing tables as the number of NAT gateways
201201
#################
202202
resource "aws_route_table" "private" {
203-
count = var.create_vpc && local.max_subnet_length > 0 ? local.nat_gateway_count : 0
203+
count = var.create_vpc && length(var.private_subnets) > 0 ? var.single_nat_gateway ? 1 : length(var.private_subnets) : 0
204204

205205
vpc_id = local.vpc_id
206206

@@ -956,7 +956,7 @@ resource "aws_nat_gateway" "this" {
956956
}
957957

958958
resource "aws_route" "private_nat_gateway" {
959-
count = var.create_vpc && var.enable_nat_gateway ? local.nat_gateway_count : 0
959+
count = var.create_vpc && var.enable_nat_gateway ? var.single_nat_gateway ? 1 : length(var.private_subnets) : 0
960960

961961
route_table_id = element(aws_route_table.private.*.id, count.index)
962962
destination_cidr_block = "0.0.0.0/0"
@@ -979,7 +979,7 @@ resource "aws_route" "private_ipv6_egress" {
979979
# Route table association
980980
##########################
981981
resource "aws_route_table_association" "private" {
982-
count = var.create_vpc && length(var.private_subnets) > 0 ? length(var.private_subnets) : 0
982+
count = var.create_vpc && length(var.private_subnets) > 0 ? var.single_nat_gateway ? 1 : length(var.private_subnets) : 0
983983

984984
subnet_id = element(aws_subnet.private.*.id, count.index)
985985
route_table_id = element(

0 commit comments

Comments
 (0)