I have a situation where I am trying to create some subnets dynamically with the cidrsubnet function in Terraform. Here is how I have it defined:
data "aws_availability_zones" "availability_zones" {
exclude_names = [
"us-west-2c",
"us-west-2d"
]
}
locals {
availability_zone_names = "${data.aws_availability_zones.availability_zones.names}"
}
resource "aws_subnet" "public_subnets" {
count = "${length(local.availability_zone_names)}"
vpc_id = "${aws_vpc.vpc.id}"
cidr_block = "${cidrsubnet(aws_vpc.vpc.cidr_block, 11, count.index)}"
availability_zone = "${local.availability_zone_names[count.index]}"
map_public_ip_on_launch = true
tags {
Name = "${var.name}-public.${local.availability_zone_names[count.index]}"
}
}
I am only trying to create these subnets in two availability zones in the region (hence why I have 2 AZs being excluded. Upon applying this code, I am getting the result I want in one of the subnets I am trying to create. However the cidr block address for the other subnet I am trying to get an address of 10.32.2.0/27 but the cidrsubnet function is generating 10.32.0.32/27. Any advice where I went wrong would be helpful.
Planned Output
aws_subnet.public_subnets[1]
id: <computed>
arn: <computed>
assign_ipv6_address_on_creation: "false"
availability_zone: "us-west-2b"
availability_zone_id: <computed>
cidr_block: "10.32.0.32/27"
aws_vpc.vpc.cidr_block
? – Marcin