Search code examples
terraformterraform-provider-awsaws-networking

Error: Unsupported attribute , each.value is object with 2 attributes


i’m trying to create multiple Network interfaces by using for_each and locals, here is my code

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],
    },

  }

}

and here i’m trying to iterate over the local.network_interfaces with for_each so I can create 4 network interfaces

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"
  }
}

but when i run Terraform apply, it gives me this error

│ 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"

any help will be appreciated


Solution

  • The object key corresponding to the security groups in the value for the map with the a key needs to match the keys in the other object values in the maps:

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