Attaching a Network interface to a for_each

The concept is to use a for_loop to grab elements from a mapped variable. It works, however was wondering if it is possible to attach the new network interface(s) to each new host. I could make it with multiple aws_instance and aws_network_interface blocks. But was wanting to avoid that.

Was wondering if this is even possible to do?

 resource "aws_network_interface" "test" {
  for_each        = var.hostnames
  subnet_id       = aws_subnet.onsip-us-east-2d-subnet.id
  private_ips     = [each.value.private_ips]
  security_groups = [aws_security_group.allow_ssh.id]

  # attachment {
  # instance     = each.value.instance_name
  # device_index = each.value.index
  #}
  tags = {
    Name = each.value.instance_name
  }
}


resource "aws_instance" "default" {
  for_each      = var.hostnames
  ami           = each.value.version
  instance_type = each.value.instance_type
  subnet_id     = aws_subnet.onsip-us-east-2d-subnet.id
  user_data = templatefile("${path.module}/templates/user_data.tpl", {
    instance_hostname = each.value.instance_name
  })
    #network_interface {
    #    device_index         = 0 # primary interface
    #    network_interface_id = aws_network_interface.test.id
    #  }

Found that it works as long as we invoke a for_each on the network_interface
resource “aws_network_interface” “test” {
for_each = var.hostnames
subnet_id = aws_subnet.onsip-us-east-2d-subnet.id
private_ip = [ach.value.private_ip
security_groups = [aws_security_group.allow_ssh.id]

attachment {
instance = each.value.instance_nam

device_index = each.value.index

#}
tags = {
Name = aws_instance.default[each_key].id
}
}