From 074b98146fbe6f5004f37e192d2fb6f494d3c42d Mon Sep 17 00:00:00 2001 From: Kay Date: Fri, 29 Nov 2024 10:35:12 +0000 Subject: [PATCH] feat: conditionally enable public and private subnet tags --- main.tf | 53 ++++++++++++++++++++++++++++++++-------------------- variables.tf | 12 ++++++++++++ 2 files changed, 45 insertions(+), 20 deletions(-) diff --git a/main.tf b/main.tf index 77cba6715..006005f29 100644 --- a/main.tf +++ b/main.tf @@ -111,17 +111,23 @@ resource "aws_subnet" "public" { private_dns_hostname_type_on_launch = var.public_subnet_private_dns_hostname_type_on_launch vpc_id = local.vpc_id - tags = merge( - { - Name = try( - var.public_subnet_names[count.index], - format("${var.name}-${var.public_subnet_suffix}-%s", element(var.azs, count.index)) + dynamic "tags" { + for_each = var.enable_public_subnet_tags ? [1] : [] + content { + tags = merge( + { + Name = try( + var.public_subnet_names[count.index], + format("${var.name}-${var.public_subnet_suffix}-%s", element(var.azs, count.index)) + ) + }, + var.tags, + var.public_subnet_tags, + lookup(var.public_subnet_tags_per_az, element(var.azs, count.index), {}) ) - }, - var.tags, - var.public_subnet_tags, - lookup(var.public_subnet_tags_per_az, element(var.azs, count.index), {}) - ) + } + } + } locals { @@ -246,17 +252,24 @@ resource "aws_subnet" "private" { private_dns_hostname_type_on_launch = var.private_subnet_private_dns_hostname_type_on_launch vpc_id = local.vpc_id - tags = merge( - { - Name = try( - var.private_subnet_names[count.index], - format("${var.name}-${var.private_subnet_suffix}-%s", element(var.azs, count.index)) + dynamic "tags" { + for_each = var.enable_private_subnet_tags ? [1] : [] + content { + tags = merge( + { + Name = try( + var.private_subnet_names[count.index], + format("${var.name}-${var.private_subnet_suffix}-%s", element(var.azs, count.index)) + ) + }, + var.tags, + var.private_subnet_tags, + lookup(var.private_subnet_tags_per_az, element(var.azs, count.index), {}) ) - }, - var.tags, - var.private_subnet_tags, - lookup(var.private_subnet_tags_per_az, element(var.azs, count.index), {}) - ) + } + } + + } # There are as many routing tables as the number of NAT gateways diff --git a/variables.tf b/variables.tf index 095cc8bdf..fa8b7cfcf 100644 --- a/variables.tf +++ b/variables.tf @@ -178,6 +178,12 @@ variable "public_subnets" { default = [] } +variable "enable_public_subnet_tags" { + description = "Indicates whether tags should be applied to public subnets. Default: `true`" + type = bool + default = true +} + variable "public_subnet_assign_ipv6_address_on_creation" { description = "Specify true to indicate that network interfaces created in the specified subnet should be assigned an IPv6 address. Default is `false`" type = bool @@ -318,6 +324,12 @@ variable "private_subnets" { default = [] } +variable "enable_private_subnet_tags" { + description = "Indicates whether tags should be applied to private subnets. Default: `true`" + type = bool + default = true +} + variable "private_subnet_assign_ipv6_address_on_creation" { description = "Specify true to indicate that network interfaces created in the specified subnet should be assigned an IPv6 address. Default is `false`" type = bool