Terraform 数据过滤器:获取多个值

Terraform data filter : get multiple values

我想从另一个存储库获取子网值。为此,我添加了数据 aws_subnet 部分。但是我在过滤部分遇到了问题。在值行的末尾,我需要计算每个子网。我尝试使用 count.index 和不同的东西。但我收到此错误:The "count" object can only be used in "module", "resource", and "data" blocks, and only when the "count" argument is set. 那么我如何使用 * 作为过滤值部分。如:${var.vpcname}-Public-*

我的子网:

myvpc-Private-0
myvpc-Private-1
myvpc-Private-2
myvpc-Public-0
myvpc-Public-1
myvpc-Public-2

我的数据部分:

data "aws_subnet" "public" {
  filter {
    name   = "tag:Name"    
    values = ["${var.vpcname}-Public-"]
  }
}

data "aws_subnet" "private" {
  filter {
    name   = "tag:Name"
    values = ["${var.vpcname}-Private-"]
  }
}

想要查看具有以下输出部分的所有子网。

output "private" {
  value = data.aws_subnet.private.*.id
}

output "public" {
  value = data.aws_subnet.public.*.id
}

您应该使用 aws_subnets,而不是 aws_subnet

data "aws_subnets" "public" {
  filter {
    name   = "tag:Name"    
    values = ["${var.vpcname}-Public-*"]
  }
}

data "aws_subnets" "private" {
  filter {
    name   = "tag:Name"
    values = ["${var.vpcname}-Private-*"]
  }
}

然后

output "private" {
  value = data.aws_subnets.private.ids
}

output "public" {
  value = data.aws_subnets.public.ids
}