如何将Terraform计数从资源传递到模块?

时间:2020-04-10 01:49:33

标签: terraform

如何将从资源创建的值列表传递给需要项目列表的模块?我尝试运行[count.index]并返回到列表[0]中的第一项,但是我收到一条消息,您只能将计数对象传递给resourcedata块。

resource "aws_subnet" "private_subnets" {
  count                   = length(data.aws_availability_zones.available.names)
  vpc_id                  = data.aws_vpc.selected.id
  cidr_block              = "192.168.${10 + count.index}.0/24"
  availability_zone       = data.aws_availability_zones.available.names[count.index]
  map_public_ip_on_launch = false

  tags = {
    Name                  = "private-subnet"
  }

module "my_module" {
  source                        = "../../"
  cluster_name                  = local.cluster_name
  subnets                       = aws_subnet.public_subnets[count.index].id

1 个答案:

答案 0 :(得分:0)

Terraform版本0.12+具有所有goodies

我更新了对资源的调用,使其像以下那样循环返回:

[ for subnet in aws_subnet.public_subnets: subnet.id ]