Skip to content

Commit

Permalink
* 2.9.18 -- 2024-05-09
Browse files Browse the repository at this point in the history
  - 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)
  • Loading branch information
badra001 committed May 9, 2024
1 parent 41cab9b commit 73d12be
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
2 changes: 1 addition & 1 deletion common/version.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
locals {
_module_version = "2.9.17"
_module_version = "2.9.18"
_module_names = {
"_main_" = "aws-vpc-setup"

Expand Down
89 changes: 88 additions & 1 deletion tag-shared-vpc-resources/tag-route-tables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,99 @@ 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" {
for_each = { for t in local.route_tables_tags_map : t.label => t }

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
## }
1 change: 1 addition & 0 deletions tag-shared-vpc-resources/tag-subnets.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 73d12be

Please sign in to comment.