另一个模块中的参考数数索引

发布于 2025-02-13 23:40:41 字数 1278 浏览 0 评论 0原文

我正在使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

红ご颜醉 2025-02-20 23:40:41

我建议这样做的是使用for_each meta-argument [1],因为如果您决定将子网数增加到两个以上,会发生什么?因此,在EC2模块中,您将执行此操作:

resource "aws_instance" "web1" { 
  for_each      = toset(var.aws_subnet_web1_id)
  ami           = lookup(var.ami_ids, "us-west-2")
  instance_type = "t2.micro"

  subnet_id     = each.value

}

在这里,您将使用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 the ec2 module, you would do this:

resource "aws_instance" "web1" { 
  for_each      = toset(var.aws_subnet_web1_id)
  ami           = lookup(var.ami_ids, "us-west-2")
  instance_type = "t2.micro"

  subnet_id     = each.value

}

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文