Terraform 计数索引

Terraform Count Index

有没有人找到做这样的事情的方法?

假设我有 3 个 public 个子网,其中部署了 3 个 nat 网关。

注意在 aws_nat_gateway subnet_id 中添加的 ${count.index} 会引发错误

data "aws_availability_zones" "availability_zones" {}
data "aws_subnet" "public_subnets" {
  count = "${length(data.aws_availability_zones.availability_zones.names)}"
  tags {
    Name = "public-subnet-${count.index}"
  }
}

data "aws_nat_gateway" "natg" {
  count     = "${length(data.aws_availability_zones.availability_zones.names)}"
  subnet_id = "${data.aws_subnet.public_subnets.${count.index}.id}"

  tags {
    Name = "public-subnet-nat-${count.index}"
  }
}

您应该改用 ${data.aws_subnet.public_subnets.*.id[count.index]}。这使用 splat operator 到 return 迭代子网列表,然后根据当前计数索引进行索引。