Passing For_each to another For_each

I have some code that creates AWS subnets, EIP’s & NAT Gateways (along with many other resources).

All 3 resources are created using For_Each. The subnets & EIP’s create correctly. But I cannot get the NAT Gateway resource to accept the correct allocation_id (from the EIP output)

elastic_ip = ["a","b","c"]

tgw_az_subnet = {
  egress1 = {
    az          = "eu-west-2a"
    cidr        = "x.x.x.x/27"
    subnet_name = "a"
  },
  egress2 = {
    az          = "eu-west-2b"
    cidr        = "x.x.x.x./27"
    subnet_name = "b"
  },
  egress3 = {
    az          = "eu-west-2c"
    cidr        = "x.x.x.x/27"
    subnet_name = "c"
  }
}


#######################
#   Subnets                                  #
#######################

resource "aws_subnet" "subnet" {
  for_each = var.tgw_az_subnet

  vpc_id            = module.vpc.vpc_id
  availability_zone = each.value["az"]
  cidr_block        = each.value["cidr"]
  tags = merge(
    {
      "Name" = each.value["subnet_name"]
    },
    var.tags,
  )
}

#########
# EIP          #
########

resource "aws_eip" "egress" {
  for_each = toset(var.elastic_ip)
  vpc      = true
  tags = merge(
    {
      "Name" = each.value
    },
    var.tags,
  )
}



###################
# NAT Gateway                #
##################

resource "aws_nat_gateway" "egress" {
  for_each = tomap({
    for k, i in aws_subnet.subnet : k => i.id
  })
  allocation_id = aws_eip.egress[each.value].id
  subnet_id     = each.value

  tags = merge(
    {
      "Name" = "egress"
    },
    var.tags,
  )

}

The error I receive is:

aws_eip.egress is object with 3 attributes
each.value is “subnet-xxxxxxxx”

So allocation_id = aws_eip.egress[each.value].id is returning a subnet ID instead of the allocation ID I am trying to get.

Any help on this would be great.

Thanks

You have not shown your Terraform resource definitions, so there isn’t enough information here to provide help.