我想从另一个存储库获取子网值。为此,我添加了数据aws_subnet部分。然而,我有一个问题与过滤部分。在值行末尾,我需要对每个子网进行计数。我试着用数数。索引和其他东西。但是我得到这个错误: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
My data part:
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
}