diff --git a/vpc-transit-gateway-association/self/associate.tf b/vpc-transit-gateway-association/self/associate.tf index 70fb1b5..9c0fcc8 100644 --- a/vpc-transit-gateway-association/self/associate.tf +++ b/vpc-transit-gateway-association/self/associate.tf @@ -1,3 +1,10 @@ +locals { + attachment_tags = { + Name = format("%v:%v", data.aws_iam_account_alias.current.account_alias, var.vpc_full_name) + "boc:tgw_environment" = var.transit_gateway_environment + } +} + #--- # attach this vpc to tgw (my region, my account) #--- @@ -14,17 +21,16 @@ resource "aws_ec2_transit_gateway_vpc_attachment" "vpc_attachment" { tags = merge( local.base_tags, - { - Name = format("tgwa-%v-%v-%v", var.transit_gateway_label, var.vpc_full_name, local.region), - "boc:tgw_environment" = var.transit_gateway_environment, - }, + var.tags, + local.attachment_tags, ) } # tag in network account with passed-in tags, plus Name=current_account:vpc_name +# if this is the network account, no need to add the additional tags as they are already there resource "aws_ec2_tag" "vpc_attachment" { provider = aws.self - for_each = merge(var.tags, { Name = format("%v:%v", data.aws_iam_account_alias.current.account_alias, var.vpc_full_name) }) + for_each = ! local.self_is_network_account ? merge(local.base_tags, var.tags, local.attachment_tags) : {} resource_id = aws_ec2_transit_gateway_vpc_attachment.vpc_attachment.id key = each.key diff --git a/vpc-transit-gateway-association/self/main.tf b/vpc-transit-gateway-association/self/main.tf index e3338c8..762caf3 100644 --- a/vpc-transit-gateway-association/self/main.tf +++ b/vpc-transit-gateway-association/self/main.tf @@ -44,10 +44,11 @@ */ locals { - account_id = var.account_id != "" ? var.account_id : data.aws_caller_identity.current.account_id - account_environment = data.aws_arn.current.partition == "aws-us-gov" ? "gov" : "ew" - region = data.aws_region.current.name - region_short = join("", [for c in split("-", local.region) : substr(c, 0, 1)]) + account_id = var.account_id != "" ? var.account_id : data.aws_caller_identity.current.account_id + account_environment = data.aws_arn.current.partition == "aws-us-gov" ? "gov" : "ew" + region = data.aws_region.current.name + region_short = join("", [for c in split("-", local.region) : substr(c, 0, 1)]) + self_is_network_account = data.aws_caller_identity.current.account_id == data.aws_caller_identity.network_account.account_id base_tags = { "boc:tf_module_version" = local._module_version