1

我正在尝试使用 for_each 和 locals 创建多个网络接口,这是我的代码

locals {
  network_interfaces = {
    a = {
      subnet_id      = module.vpc.public_subnets[0],
      security_group = [aws_security_group.ssh.id],
    },

    b = {
      subnet_id       = module.vpc.public_subnets[0],
      security_groups = [aws_security_group.ssh.id],
    },
    c = {
      subnet_id       = module.vpc.public_subnets[1],
      security_groups = [aws_security_group.ssh.id],
    },
    d = {
      subnet_id       = module.vpc.public_subnets[1],
      security_groups = [aws_security_group.ssh.id],
    },

  }

}

在这里我试图用 for_each 迭代 local.network_interfaces,这样我就可以创建 4 个网络接口

resource "aws_network_interface" "this" {
  for_each  = local.network_interfaces
  subnet_id = each.value.subnet_id
  security_groups = each.value.security_groups
  tags = {
    Name = "${each.key}_network_interface"
  }
}

但是当我运行 Terraform 应用时,它给了我这个错误

│ Error: Unsupported attribute
│ 
│   on main.tf line 61, in resource "aws_network_interface" "this":
│   61:   security_groups = each.value.security_groups
│     ├────────────────
│     │ each.value is object with 2 attributes
│ 
│ This object does not have an attribute named "security_groups"

任何帮助将不胜感激

4

1 回答 1

2

与该object键对应的值中的安全组对应map的键需要与映射a中其他值中的键匹配:object

a = {
  subnet_id       = module.vpc.public_subnets[0],
  security_groups = [aws_security_group.ssh.id],
},
于 2022-01-21T13:36:46.773 回答