I am creating a VPC using terraform VPC module,
and trying to give the subnets names that make sense like: data-vpc-private-subnet-a
,
the "a" represents the availability zone that the subnet is located in.
but I am not sure how to do that, this is what I have so far:
module "vpc" {
source = "terraform-aws-modules/vpc/aws"
name = "${local.name}"
cidr = "10.70.0.0/16"
azs = ["${local.region}a", "${local.region}b"]
az = ["a", "b"]
private_subnets = ["10.70.1.0/24", "10.70.2.0/24"]
public_subnets = ["10.70.3.0/24", "10.70.4.0/24"]
manage_default_network_acl = true
default_network_acl_tags = { Name = "${local.name}-default-nacl" }
manage_default_route_table = true
default_route_table_tags = { Name = "${local.name}-default-route-table" }
manage_default_security_group = true
default_security_group_tags = { Name = "${local.name}-default-sg" }
enable_dns_hostnames = true
enable_dns_support = true
map_public_ip_on_launch = false
enable_nat_gateway = true
enable_vpn_gateway = true
single_nat_gateway = false
one_nat_gateway_per_az = false
reuse_nat_ips = true # <= Skip creation of EIPs for the NAT Gateways
external_nat_ip_ids = "${aws_eip.nat.*.id}" # <= IPs specified here as input to the module
# VPC Flow Logs (Cloudwatch log group and IAM role will be created)
enable_flow_log = true
create_flow_log_cloudwatch_log_group = true
create_flow_log_cloudwatch_iam_role = true
flow_log_max_aggregation_interval = 60
tags = var.vpc_tags
private_subnet_tags = { Name = "${local.name}-private-subnet-${az[count.index]}" }
public_subnet_tags = { Name = "${local.name}-public-subnet-${az[count.index]}" }
}
Any help would be appriciated.
2
Answers
With a great help from @Maciej Rostański answer, I was able to give the subnets their own unique names.
To also won't have to get rid of the tags I wanted to put on all the resources I used each resource default-tag.
So this is what I got at the end:
In the public vpc module, subnet tags are defined in the module like this (example for public subnets):
So in the main module you would have to set up:
and then the name od the subnet is e.g.
my-project-private-eu-central-1a
.If you mean to get rid of the region and leave only
a
, then you would have to download the module, modify lines that define tags (for examplemain.tf
line 394 for private networks) and use this code as a local module source:The above code removes region from the subnet’s name, provided of course that you also push the
region
variable into the module.