[英]How can I retrieve the subnets in a given VPC with Terraform?
I was trying to retrieve the su.nets in a given VPC, in my case the default VPC, however I keep getting an error:我试图在给定的 VPC 中检索 su.net,在我的例子中是默认 VPC,但是我不断收到错误消息:
data "aws_vpc" "default_vpc" {
default = true
}
data "aws_subnet" "subnets" {
vpc_id = data.aws_vpc.default_vpc.id
}
output "subnets_out" {
value = data.aws_subnet.subnets
}
Error: multiple EC2 Su.nets matched;
错误:匹配多个 EC2 Su.net; use additional constraints to reduce matches to a single EC2 Su.net
使用额外的约束来减少对单个 EC2 Su.net 的匹配
The first data call works, I get my default VPC id, however the second one fails.第一个数据调用有效,我得到了我的默认 VPC id,但是第二个失败了。
Originally I had 1 su.net in my default VPC and at that time, the second data call worked too but to my surprise it returned the su.net as a map instead of a map within a set.最初我在我的默认 VPC 中有 1 个 su.net,那时,第二个数据调用也有效,但令我惊讶的是它返回 su.net 作为 map 而不是集合中的 map。 So I was curious what would happen if there was more than one su.nets in my VPC but then the data call failed.
所以我很好奇,如果我的 VPC 中有多个 su.net,然后数据调用失败,会发生什么情况。
What I find really confusing is that the Terraform documentation says:我发现真正令人困惑的是Terraform 文档说:
This resource can be useful for getting back a set of su.net ID s .
此资源可用于取回一组su.net ID 。
If it's supposed to return a set of su.net IDs, then why is it an issue that multiple su.nets were matched?如果它应该返回一组 su.net ID,那么为什么匹配多个 su.net 是个问题?
aws_su.net
is a resource for a specific su.net. aws_su.net
是特定 su.net 的资源。 Its not for a collection of su.nets.它不是用于 su.net 的集合。 so when you pass the VPC_ID, aws is finding more than one su.net in that VPC.
因此,当您传递 VPC_ID 时,aws 会在该 VPC 中找到多个 su.net。 Its not possible to store more than one su.nets in
aws_su.net
aws_su.net 中不能存储多个
aws_su.net
https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/su.net https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/su.net
aws_su.net provides details about a specific VPC su.net.
aws_su.net 提供有关特定 VPC su.net 的详细信息。
It sounds like you should be using aws_su.nets
https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/su.nets听起来你应该使用
aws_su.nets
https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/su.nets
This resource can be useful for getting back a set of su.net IDs.
此资源可用于取回一组 su.net ID。
This will return you a list of su.net ID's which you can then use in a foreach in aws_su.net
to get the specific details of each su.net这将返回一个 su.net ID 的列表,然后您可以在
aws_su.net
的 foreach 中使用它来获取每个 su.net 的具体细节
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.