Skip to content

Commit 45bc6f1

Browse files
aknyshclaude
andcommitted
Add NAT Gateway IDs to subnet stats maps
Incorporate feature from PR #225 to expose NAT Gateway IDs in the subnet stats outputs. - Add `nat_gateway_id` field to `named_private_subnets_stats_map` (maps to the NAT Gateway that the private subnet routes to for egress) - Add `nat_gateway_id` field to `named_public_subnets_stats_map` (maps to the NAT Gateway in that public subnet, if any) - Create helper locals to correctly map subnets to NAT Gateways - Update output descriptions to reflect the new fourth field This makes the subnet stats more complete and enables downstream components to reference NAT Gateway IDs when needed (e.g., network firewall routing configurations). Implementation correctly handles our new NAT placement features: - Works with index-based NAT placement (`nat_gateway_public_subnet_indices`) - Works with name-based NAT placement (`nat_gateway_public_subnet_names`) - Private subnets correctly map to the NAT they route to (using existing `private_route_table_to_nat_map`) - Public subnets correctly identify which ones contain NAT Gateways 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent d65c033 commit 45bc6f1

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

main.tf

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,12 +333,22 @@ locals {
333333
compact([for k, v in local.az_public_route_table_ids_map : try(v[i], "")]))
334334
}
335335

336+
# Create a map from public subnet ID to NAT Gateway ID (for public subnets that have NAT Gateways)
337+
public_subnet_to_nat_gateway_map = { for nat in aws_nat_gateway.default : nat.subnet_id => nat.id }
338+
339+
# Create a map from private subnet ID to NAT Gateway ID (the NAT that the private subnet routes to)
340+
private_subnet_to_nat_gateway_map = local.nat_gateway_enabled && local.private4_enabled ? {
341+
for idx, subnet in aws_subnet.private :
342+
subnet.id => aws_nat_gateway.default[local.private_route_table_to_nat_map[idx]].id
343+
} : {}
344+
336345
named_private_subnets_stats_map = { for i, s in local.private_subnets_per_az_names : s => (
337346
[
338347
for k, v in local.az_private_route_table_ids_map : {
339348
az = k
340349
route_table_id = try(v[i], "")
341350
subnet_id = try(local.az_private_subnets_map[k][i], "")
351+
nat_gateway_id = try(local.private_subnet_to_nat_gateway_map[local.az_private_subnets_map[k][i]], "")
342352
}
343353
])
344354
}
@@ -349,6 +359,7 @@ locals {
349359
az = k
350360
route_table_id = try(v[i], "")
351361
subnet_id = try(local.az_public_subnets_map[k][i], "")
362+
nat_gateway_id = try(local.public_subnet_to_nat_gateway_map[local.az_public_subnets_map[k][i]], "")
352363
}
353364
])
354365
}

outputs.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,11 +139,11 @@ output "named_public_route_table_ids_map" {
139139
}
140140

141141
output "named_private_subnets_stats_map" {
142-
description = "Map of subnet names (specified in `private_subnets_per_az_names` or `subnets_per_az_names` variable) to lists of objects with each object having three items: AZ, private subnet ID, private route table ID"
142+
description = "Map of subnet names (specified in `private_subnets_per_az_names` or `subnets_per_az_names` variable) to lists of objects with each object having four items: AZ, private subnet ID, private route table ID, NAT Gateway ID (the NAT Gateway that this private subnet routes to for egress)"
143143
value = local.named_private_subnets_stats_map
144144
}
145145

146146
output "named_public_subnets_stats_map" {
147-
description = "Map of subnet names (specified in `public_subnets_per_az_names` or `subnets_per_az_names` variable) to lists of objects with each object having three items: AZ, public subnet ID, public route table ID"
147+
description = "Map of subnet names (specified in `public_subnets_per_az_names` or `subnets_per_az_names` variable) to lists of objects with each object having four items: AZ, public subnet ID, public route table ID, NAT Gateway ID (the NAT Gateway in this public subnet, if any)"
148148
value = local.named_public_subnets_stats_map
149149
}

0 commit comments

Comments
 (0)