Terraform v0.12.29 provider.aws v3.7.0
Terraform我得到的是count.index始终为0。
10x0.0.0/23
resource "aws_subnet" "public" {
count = length(var.availability_zones)
vpc_id = aws_vpc.default.id
cidr_block = cidrsubnet(format("%s.%s.%s",join(".",slice(split(".",var.cidr_block), 0, 2)), count.index ,split(".",var.cidr_block)[3]), var.public_newbits, count.index)
availability_zone = element(var.availability_zones, count.index)
map_public_ip_on_launch = true
tags = merge(map("Name", "${var.namespace}-${var.environment}-public-${count.index + 1}"), var.tags)
}
地形图
cidr_block两个子网中的值均为0。
# aws_subnet.public[0] will be created
+ resource "aws_subnet" "public" {
+ arn = (known after apply)
+ assign_ipv6_address_on_creation = false
+ availability_zone = "ap-southeast-1a"
+ availability_zone_id = (known after apply)
+ cidr_block = "10.x0.0.0/27"
+ id = (known after apply)
+ ipv6_cidr_block_association_id = (known after apply)
+ map_public_ip_on_launch = true
+ owner_id = (known after apply)
+ vpc_id = (known after apply)
}
# aws_subnet.public[1] will be created
+ resource "aws_subnet" "public" {
+ arn = (known after apply)
+ assign_ipv6_address_on_creation = false
+ availability_zone = "ap-southeast-1b"
+ availability_zone_id = (known after apply)
+ cidr_block = "10.x0.0.32/27"
+ id = (known after apply)
+ ipv6_cidr_block_association_id = (known after apply)
+ map_public_ip_on_launch = true
+ owner_id = (known after apply)
+ vpc_id = (known after apply)
}
下面是我的错误答案。cidrsubnet((相当棘手,我花了一些时间来理解它。
resource "aws_subnet" "public" {
count = length(var.availability_zones)
vpc_id = aws_vpc.default.id
cidr_block = cidrsubnet(var.cidr_block, var.public_newbits, count.index)
availability_zone = element(var.availability_zones, count.index)
map_public_ip_on_launch = true
tags = merge( var.tags, map("Name", "${var.environment}-${var.namespace}-mr-public-${count.index + 1}"))
}