Count 将安全组附加到我的aws实例的Terraform问题

Count 将安全组附加到我的aws实例的Terraform问题,count,terraform,Count,Terraform,我对地形还不够了解,我认为我对count和count.index的用法有误解 我正在使用count参数创建一些EC2实例,它运行良好 resource "aws_instance" "server" { ami = data.aws_ami.app_ami.id instance_type = "t2.micro" key_name = "DeirdreKey" subnet_id = aws_subnet.my_s

我对地形还不够了解,我认为我对count和count.index的用法有误解

我正在使用count参数创建一些EC2实例,它运行良好

resource "aws_instance" "server" {
ami = data.aws_ami.app_ami.id
instance_type = "t2.micro"
key_name = "DeirdreKey"
subnet_id = aws_subnet.my_subnet_a.id
count = 2
tags = {
 Name = "server.${count.index}"
}
我想将一个安全组与这两个实例关联,因此我创建了以下

resource "aws_network_interface_sg_attachment" "sg_attachment" {
security_group_id    = aws_security_group.allow_internet.id
network_interface_id = aws_instance.server.primary_network_interface_id
}
resource "aws_network_interface_sg_attachment" "sg_attachment" {
security_group_id    = aws_security_group.allow_internet.id
network_interface_id = aws_instance.server[count.index].primary_network_interface_id
然而,我遇到了这个错误

Error: Missing resource instance key

on lb.tf line 57, in resource "aws_network_interface_sg_attachment" "sg_attachment":
57:   network_interface_id = aws_instance.server.primary_network_interface_id

Because aws_instance.server has "count" set, its attributes must be
accessed on specific instances.

For example, to correlate with indices of a referring resource, use:
aws_instance.server[count.index]
我理解错误的原因。这是因为我在中引用的本地资源名称不是唯一的,因为我已经创建了2个名为“server”的aws实例。不过我不知道怎么修。我试着用下面的

resource "aws_network_interface_sg_attachment" "sg_attachment" {
security_group_id    = aws_security_group.allow_internet.id
network_interface_id = aws_instance.server.primary_network_interface_id
}
resource "aws_network_interface_sg_attachment" "sg_attachment" {
security_group_id    = aws_security_group.allow_internet.id
network_interface_id = aws_instance.server[count.index].primary_network_interface_id
但是我得到了下面的错误

Error: Reference to "count" in non-counted context

on lb.tf line 53, in resource "aws_network_interface_sg_attachment" "sg_attachment":
53:   network_interface_idaws_instance.server[count.index].primary_network_interface_id

The "count" object can only be used in "module", "resource", and "data"
blocks, and only when the "count" argument is set.
这是否意味着必须在本地资源名称中引入count.index?我试过几种方法,但似乎不起作用

resource "aws_instance" "server${count.index}" {

要使用
count.index
,您需要资源上的count语句。Count语句可能会失控,因此如果您有多个资源在逻辑上需要相同的计数,请使用变量或局部值:

local {
  replications = 2
}

resource "aws_instance" "server" {
  count = local.replications
  ami = data.aws_ami.app_ami.id
  instance_type = "t2.micro"
  key_name = "DeirdreKey"
  subnet_id = aws_subnet.my_subnet_a.id
  tags = {
    Name = "server.${count.index}"
  }
}

resource "aws_network_interface_sg_attachment" "sg_attachment" {
  count                = local.replications
  security_group_id    = aws_security_group.allow_internet.id
  network_interface_id = aws_instance.server[count.index].primary_network_interface_id
}

这将为每台服务器创建一个安全组附件,并为您提供一个可以作为
aws\u实例.server[0]
aws\u实例.server[1]
引用的服务器列表,以及一个可以以相同方式引用的附件列表。

谢谢!现在更有意义了!