Rename private subnet "Name" attribute for each subnet created from terraform module

Viewed 1028

I have used the terraform vpc module from registry to create vpc and subnets as follows:

#https://registry.terraform.io/modules/terraform-aws-modules/vpc/aws/latest
module "vpc" {
  source  = "terraform-aws-modules/vpc/aws"
  version = "2.77.0"
  # insert the 49 required variables here
  name            = "${var.vpc_name}"
  cidr            = "${var.vpc_cidr}"
  azs             = "${var.azs}"
  private_subnets = ["10.0.0.64/26", "10.0.0.128/26"]
  public_subnets  = "${var.public_subnets}"

  enable_dns_hostnames = true
  enable_dns_support   = true

  enable_nat_gateway = true
  single_nat_gateway = true

  private_subnet_tags = {
    Name = "nprod-net"
  }
}

As you can see I have 2 private subnet with tag "nprod-net" but I want to name each private subnet with a different name say "nprod-net-app-1a" and "nprod-net-vpce-1a". Any help would be appreciated.

1 Answers

You can not customise tags for each private subnet because the module just accept list of tags for all private, but after creating the subnets you can leverage aws_ec2_tag resource to tag the subnets again, for example:

resource "aws_ec2_tag" "private_subnet_tag" {
  resource_id    = module.vpc.private_subnets[0]
  key            = "Name"
  value          = "nprod-net-app-1a"
}
Related