另一个模块中的参考数数索引
我正在使用Terraform模块来创建两个EC2实例,并用子网分配它们。 我也在使用模块。
module/network:
resource "aws_subnet" "web_subnet1" {
count = 2
vpc_id = aws_vpc.web_vpc.id
cidr_block = cidrsubnet(var.network_cidr, 2, count.index)
availability_zone = element(var.availability_zones, count.index)
}
output "aws_subnet_web1_id" {
value = aws_subnet.web_subnet1[*].id
}
main.tf文件包含引用的AWS_SUBNET_WEB1_ID
module "ec2"{
source = "./modules/ec2"
aws_subnet_web1_id = module.network.aws_subnet_web1_id
问题是当我尝试在EC2模块中使用这些子网ID来创建两个EC2实例时:
resource "aws_instance" "web1" {
count = 2
ami = "${lookup(var.ami_ids, "us-west-2")}"
instance_type = "t2.micro"
subnet_id = "${element(aws_subnet_web1_id[*].id, count.index % length(aws_subnet_web1_id[*].id))}"
}
我会遇到Bellow错误:
│ Error: Invalid reference
│
│ on modules/ec2/ec2.tf line 11, in resource "aws_instance" "web1":
│ 11: subnet_id = "${element(aws_subnet_web1_id[*].id, count.index % length(aws_subnet_web1_id[*].id))}"
│
│ A reference to a resource type must be followed by at least one attribute access, specifying the resource name.
如果有人有任何IDEEA,则会非常感谢。 谢谢
I am using Terraform modules to create two EC2 instances and assign them with subnets.
I am also using modules.
module/network:
resource "aws_subnet" "web_subnet1" {
count = 2
vpc_id = aws_vpc.web_vpc.id
cidr_block = cidrsubnet(var.network_cidr, 2, count.index)
availability_zone = element(var.availability_zones, count.index)
}
output "aws_subnet_web1_id" {
value = aws_subnet.web_subnet1[*].id
}
main.tf file containing the referenced aws_subnet_web1_id
module "ec2"{
source = "./modules/ec2"
aws_subnet_web1_id = module.network.aws_subnet_web1_id
Issue is when i am trying to use those subnet id's in the ec2 module to create two ec2 instances:
resource "aws_instance" "web1" {
count = 2
ami = "${lookup(var.ami_ids, "us-west-2")}"
instance_type = "t2.micro"
subnet_id = "${element(aws_subnet_web1_id[*].id, count.index % length(aws_subnet_web1_id[*].id))}"
}
I am getting the bellow error:
│ Error: Invalid reference
│
│ on modules/ec2/ec2.tf line 11, in resource "aws_instance" "web1":
│ 11: subnet_id = "${element(aws_subnet_web1_id[*].id, count.index % length(aws_subnet_web1_id[*].id))}"
│
│ A reference to a resource type must be followed by at least one attribute access, specifying the resource name.
If anybody has any ideea it would be very appreciated.
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议这样做的是使用
for_each
meta-argument [1],因为如果您决定将子网数增加到两个以上,会发生什么?因此,在EC2
模块中,您将执行此操作:在这里,您将使用
toset
内置功能[2]将列表转换为集合。这是可能的,因为您从网络模块中获得的输出已经成为列表。[1] https://wwwww.terraform.io/language/language/meta-arguments/meta-arguments/meta-arguments/for_each/for_each
[2] https://www.terraform.io/language/functions/functions/toset/toset
What I would suggest doing here is using
for_each
meta-argument [1] because what would happen if you decided to increase the number of subnets to more than two? So, in theec2
module, you would do this:Here you would use
toset
built-in function [2] to convert a list to a set. This is possible because the output you are getting from the network module is already a list.[1] https://www.terraform.io/language/meta-arguments/for_each
[2] https://www.terraform.io/language/functions/toset