Reference exact resource created with for_each

Hello!
I need to reference the exact resource created with for_each loop.
So, this code works fine:

variable "natgw" {
  type = object({
    azs  = set(string)
    zone = string
  })

  default = {
    zone = "1"
    azs  = ["1", "2", "3"]
  }
}

resource "azurerm_public_ip" "pip" {
  for_each            = var.natgw.azs
...
  zones               = [each.value]
}

resource "azurerm_nat_gateway" "natgateway" {
  for_each                = var.natgw.azs
....
  zones                   = [each.value]
}

resource "azurerm_nat_gateway_public_ip_association" "pip_association" {
  for_each             = var.natgw.azs
  nat_gateway_id       = azurerm_nat_gateway.natgateway[each.value].id
  public_ip_address_id = azurerm_public_ip.pip[each.value].id
}

resource "azurerm_subnet_nat_gateway_association" "subnet_association" {
  subnet_id      = azurerm_subnet.exact_subnet.id
  nat_gateway_id = azurerm_nat_gateway.natgateway[var.natgw.zone].id
}

Until I change var.natgw.zone

  default = {
    zone = "2"
    azs  = ["1", "2", "3"]
  }

or just put 2 as index:

 nat_gateway_id = azurerm_nat_gateway.natgateway[2].id

I receive an error:

│ Error: Invalid index
│ 
│"azurerm_subnet_nat_gateway_association" "subnet_association":
│     nat_gateway_id = azurerm_nat_gateway.natgateway[var.natgw.zone].id
│     ├────────────────
│     │ azurerm_nat_gateway.natgateway is object with 1 attribute "1"
│     │ var.natgw.zone is "2"
│ 
│ The given key does not identify an element in this collection value.
terraform state list | grep azurerm_nat_gateway.natgateway                                                                                                                                                    
module.vnet.azurerm_nat_gateway.natgateway["1"]
module.vnet.azurerm_nat_gateway.natgateway["2"]
module.vnet.azurerm_nat_gateway.natgateway["3"]

It looks a bit weird to me since azurerm_nat_gateway_public_ip_association doesn’t through an error that it can’t find indexes.
Or I can’t reference in such a way and need to add some kind of loop there also?
thanks in advance

This doesn’t make sense… but I can see that in some places you have var.nat_gateway and in others var.natgw so I think you have “simplified” your code prior to posting, and in doing so hidden information that is necessary to understand the problem.

well, yes. I removed all unneeded staff like skus, resource_groups, region and so on.
There were also some conditions, but at the end 3 natgateways were created, and I can reference all of them with for_each in the other resource, but can’t the exact one.
I’ve edited error with correct var reference, apology

In the process of simplifying your configuration, you have removed whatever the error is.

I can state that with confidence, because I took your exact shown config, converted it all into null_resources so I could run it without Azure, and tested it working just fine.

variable "natgw" {
  type = object({
    azs  = set(string)
    zone = string
  })

  default = {
    zone = "2"
    azs  = ["1", "2", "3"]
  }
}

resource "null_resource" "pip" {
  for_each = var.natgw.azs
  triggers = {
    zones = jsonencode([each.value])
  }
}

resource "null_resource" "natgateway" {
  for_each = var.natgw.azs
  triggers = {
    zones = jsonencode([each.value])
  }
}

resource "null_resource" "pip_association" {
  for_each = var.natgw.azs
  triggers = {
    nat_gateway_id       = null_resource.natgateway[each.value].id
    public_ip_address_id = null_resource.pip[each.value].id
  }
}

resource "null_resource" "subnet_association" {
  triggers = {
    subnet_id      = "placeholder"
    nat_gateway_id = null_resource.natgateway[var.natgw.zone].id
  }
}