我正在尝试使用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"
如有任何帮助,将不胜感激
map
和a
密钥的值中安全组对应的object
密钥需要与映射中其他object
值中的密钥匹配:
a = {
subnet_id = module.vpc.public_subnets[0],
security_groups = [aws_security_group.ssh.id],
},