From 73d12be51fbaffcdca38ab5b60c54d13355d5b59 Mon Sep 17 00:00:00 2001 From: badra001 Date: Thu, 9 May 2024 14:30:33 -0400 Subject: [PATCH] * 2.9.18 -- 2024-05-09 - tag-shared-vpc-resources - remap Name tag of route_table if the AZ ID is different in the source vs the taget (within the assocaated subnets) --- CHANGELOG.md | 4 + common/version.tf | 2 +- tag-shared-vpc-resources/tag-route-tables.tf | 89 +++++++++++++++++++- tag-shared-vpc-resources/tag-subnets.tf | 1 + 4 files changed, 94 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4f74d64..ed265c0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -391,3 +391,7 @@ * 2.9.17 -- 2024-05-09 - tag-shared-vpc-resources - remap Name tag of subnet if the AZ ID is different in the source vs the taget + +* 2.9.18 -- 2024-05-09 + - tag-shared-vpc-resources + - remap Name tag of route_table if the AZ ID is different in the source vs the taget (within the assocaated subnets) diff --git a/common/version.tf b/common/version.tf index cd89799..a0e1c19 100644 --- a/common/version.tf +++ b/common/version.tf @@ -1,5 +1,5 @@ locals { - _module_version = "2.9.17" + _module_version = "2.9.18" _module_names = { "_main_" = "aws-vpc-setup" diff --git a/tag-shared-vpc-resources/tag-route-tables.tf b/tag-shared-vpc-resources/tag-route-tables.tf index a0d40ea..391e69a 100644 --- a/tag-shared-vpc-resources/tag-route-tables.tf +++ b/tag-shared-vpc-resources/tag-route-tables.tf @@ -29,6 +29,11 @@ locals { _rt_enabled = var.tag_enabled_route_tables route_tables_tags = { for k, v in data.aws_route_table.route_table : k => merge(v.tags, { "boc:vpc:owner_id" = v.owner_id }) } route_tables_tags_map = flatten([for k, v in local.route_tables_tags : [for tk, tv in v : { label = format("%v__%v", k, tk), route_table_id = k, key = tk, value = tv }]]) + + _route_tables_subnets_remap = { for k, v in data.aws_route_table.route_table : k => [for s in v.associations : local.subnets_tag_remap[s.subnet_id]] } + is_route_tables_subnets_remap = { for k, v in local._route_tables_subnets_remap : k => alltrue([for x in v : x.remap]) } + route_tables_subnets_remap = { for k, v in data.aws_route_table.route_table : k => (local._route_tables_subnets_remap[k])[0] } + } resource "aws_ec2_tag" "route_tables" { @@ -36,5 +41,87 @@ resource "aws_ec2_tag" "route_tables" { resource_id = each.value.route_table_id key = each.value.key - value = each.value.value + value = each.value.key == "Name" ? replace(each.value.value, local.route_tables_subnets_remap[each.value.route_table_id].source_az_name, local.route_tables_subnets_remap[each.value.route_table_id].target_az_name) : each.value.value } + + +## @@@ +## +## data "aws_route_table" "route_table" { +## arn = "arn:aws-us-gov:ec2:us-gov-west-1:057405694017:route-table/rtb-06a17d21d528fc702" +## associations = [ +## { +## gateway_id = "" +## main = false +## route_table_association_id = "rtbassoc-04b58ba1fcbea4aee" +## route_table_id = "rtb-06a17d21d528fc702" +## subnet_id = "subnet-0e4c97ca061b151f4" +## }, +## { +## gateway_id = "" +## main = false +## route_table_association_id = "rtbassoc-0d47800832027d874" +## route_table_id = "rtb-06a17d21d528fc702" +## subnet_id = "subnet-0cb23b83c3b982e4e" +## }, +## { +## gateway_id = "" +## main = false +## route_table_association_id = "rtbassoc-0d982a334f8a23398" +## route_table_id = "rtb-06a17d21d528fc702" +## subnet_id = "subnet-070f8a95743e0efdc" +## }, +## { +## gateway_id = "" +## main = false +## route_table_association_id = "rtbassoc-05026e8b616cdcb99" +## route_table_id = "rtb-06a17d21d528fc702" +## subnet_id = "subnet-070508db0ceb6d63c" +## }, +## ] +## id = "rtb-06a17d21d528fc702" +## owner_id = "057405694017" +## route_table_id = "rtb-06a17d21d528fc702" +## +## +## +## data "aws_subnet" "subnet" { +## provider = aws.network_account +## for_each = toset(flatten(concat([for k, v in data.aws_subnets.subnets : v.ids]))) +## id = each.key +## } +## +## +## +## ## +## ## output "subnet" { +## ## value = data.aws_subnet.subnet +## ## } +## +## # the remap of the Name tag is needed because not always, is the AZ the same +## # https://docs.aws.amazon.com/prescriptive-guidance/latest/patterns/use-consistent-availability-zones-in-vpcs-across-different-aws-accounts.html#:~:text=If%20you%20create%20VPCs%20in,to%20the%20same%20AZ%20ID. +## # we have the problem where id az1 is 1b in one account and it is 1a in another. This will remap them +## +## locals { +## _subnet_enabled = var.tag_enabled_subnets +## subnets_tags = { for k, v in data.aws_subnet.subnet : k => merge(v.tags, { "boc:vpc:owner_id" = v.owner_id }) } +## subnets_tags_map = flatten([for k, v in local.subnets_tags : [for tk, tv in v : { label = format("%v__%v", k, tk), subnet_id = k, key = tk, value = tv }]]) +## +## subnets_tag_remap = { for k, v in data.aws_subnet.subnet : k => { +## source_az_name = v.availability_zone +## source_az_id = v.availability_zone_id +## source_tag_name = v.tags.Name +## target_az_name = local.az_id_map[v.availability_zone_id] +## target_az_id = v.availability_zone_id +## target_tag_name = replace(v.tags.Name, v.availability_zone, local.az_id_map[v.availability_zone_id]) +## remap = v.availability_zone != local.az_id_map[v.availability_zone_id] +## } } +## } +## +## resource "aws_ec2_tag" "subnets" { +## for_each = { for t in local.subnets_tags_map : t.label => t } +## +## resource_id = each.value.subnet_id +## key = each.value.key +## value = each.value.key == "Name" ? local.subnets_tag_remap[each.value.subnet_id].target_tag_name : each.value.value +## } diff --git a/tag-shared-vpc-resources/tag-subnets.tf b/tag-shared-vpc-resources/tag-subnets.tf index c316649..9829b5c 100644 --- a/tag-shared-vpc-resources/tag-subnets.tf +++ b/tag-shared-vpc-resources/tag-subnets.tf @@ -34,6 +34,7 @@ locals { subnets_tags_map = flatten([for k, v in local.subnets_tags : [for tk, tv in v : { label = format("%v__%v", k, tk), subnet_id = k, key = tk, value = tv }]]) subnets_tag_remap = { for k, v in data.aws_subnet.subnet : k => { + subnet_id = k source_az_name = v.availability_zone source_az_id = v.availability_zone_id source_tag_name = v.tags.Name