From b426e6c2f5ffb8800a148791d434265c85649853 Mon Sep 17 00:00:00 2001 From: shubhamsinha-sf <73998079+shubhamsinha-sf@users.noreply.github.com> Date: Sat, 10 Feb 2024 17:34:24 +0530 Subject: [PATCH] split locals for nat gateway ids from input and from the resource within the module --- modules/subnets/locals.tf | 6 +++++- modules/subnets/main.tf | 2 +- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/modules/subnets/locals.tf b/modules/subnets/locals.tf index 687ed44..08f0941 100644 --- a/modules/subnets/locals.tf +++ b/modules/subnets/locals.tf @@ -7,5 +7,9 @@ locals { // then no routes to public internet are added. public_internet_mapping = { for x in var.private_subnets : x.name => replace(x.name, "-private-", "-public-") if var.nat_gateway_enabled == true } - subnet_ngw_ids = { for x in var.private_subnets : x.name => var.nat_gateway_enabled == true ? aws_nat_gateway.public[local.public_internet_mapping[x.name]].id : lookup(var.az_ngw_ids, x.availability_zone, null) } + subnet_ngw_ids_resource = { for x in var.private_subnets : x.name => var.nat_gateway_enabled == true ? aws_nat_gateway.public[local.public_internet_mapping[x.name]].id : null } + + subnet_ngw_ids_custom = { for x in var.private_subnets : x.name => lookup(var.az_ngw_ids, x.availability_zone, null) } + + subnet_ngw_ids = var.nat_gateway_enabled == true ? local.subnet_ngw_ids_resource : { for key,value in local.subnet_ngw_ids_custom : key => value if value != null } } diff --git a/modules/subnets/main.tf b/modules/subnets/main.tf index 042917e..076669f 100644 --- a/modules/subnets/main.tf +++ b/modules/subnets/main.tf @@ -84,7 +84,7 @@ resource "aws_route_table" "private" { } resource "aws_route" "default" { - for_each = { for x in var.private_subnets : x.name => local.subnet_ngw_ids[x.name] if local.subnet_ngw_ids[x.name] != null } + for_each = local.subnet_ngw_ids route_table_id = aws_route_table.private[each.key].id nat_gateway_id = each.value