Skip to content

Commit

Permalink
fix: Correctly manage route tables for database subnets when multiple…
Browse files Browse the repository at this point in the history
… NAT gateways present (#518)
  • Loading branch information
Diego Rodriguez authored Feb 22, 2021
1 parent 67fd404 commit 8997520
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -223,13 +223,17 @@ resource "aws_route_table" "private" {
# Database routes
#################
resource "aws_route_table" "database" {
count = var.create_vpc && var.create_database_subnet_route_table && length(var.database_subnets) > 0 ? 1 : 0
count = var.create_vpc && var.create_database_subnet_route_table && length(var.database_subnets) > 0 ? var.single_nat_gateway || var.create_database_internet_gateway_route ? 1 : length(var.database_subnets) : 0

vpc_id = local.vpc_id

tags = merge(
{
"Name" = "${var.name}-${var.database_subnet_suffix}"
"Name" = var.single_nat_gateway || var.create_database_internet_gateway_route ? "${var.name}-${var.database_subnet_suffix}" : format(
"%s-${var.database_subnet_suffix}-%s",
var.name,
element(var.azs, count.index),
)
},
var.tags,
var.database_route_table_tags,
Expand All @@ -249,7 +253,7 @@ resource "aws_route" "database_internet_gateway" {
}

resource "aws_route" "database_nat_gateway" {
count = var.create_vpc && var.create_database_subnet_route_table && length(var.database_subnets) > 0 && false == var.create_database_internet_gateway_route && var.create_database_nat_gateway_route && var.enable_nat_gateway ? local.nat_gateway_count : 0
count = var.create_vpc && var.create_database_subnet_route_table && length(var.database_subnets) > 0 && false == var.create_database_internet_gateway_route && var.create_database_nat_gateway_route && var.enable_nat_gateway ? var.single_nat_gateway ? 1 : length(var.database_subnets) : 0

route_table_id = element(aws_route_table.database.*.id, count.index)
destination_cidr_block = "0.0.0.0/0"
Expand Down Expand Up @@ -1007,7 +1011,7 @@ resource "aws_route_table_association" "database" {
subnet_id = element(aws_subnet.database.*.id, count.index)
route_table_id = element(
coalescelist(aws_route_table.database.*.id, aws_route_table.private.*.id),
var.single_nat_gateway || var.create_database_subnet_route_table ? 0 : count.index,
var.create_database_subnet_route_table ? var.single_nat_gateway || var.create_database_internet_gateway_route ? 0 : count.index : count.index,
)
}

Expand Down

0 comments on commit 8997520

Please sign in to comment.