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

@Hamza-Aziz,

You have a typo in your first map entry with a key of security_group.

1 Like

Yeah, I just saw it, thanks a lot @jbardin