Terraform expression for_each invalid index error

HI Guys and happy new year to all

I got an issue to gatter the token generate by the bloc ressource below which have an iteration with an for_each loop.

my varibale map is :

 variable "wvd_hostpool" {
 description = "Please provide the required information to create a WVD hostpool."
 type        = map(any)
 default = {
   hp-azcan-weu-wvd-01 = {
     "name"                             = "hp-azcan-weu-wvd-01"
     "type"                             = "Personal"
     "load_balancer_type"               = "DepthFirst"
     "personal_desktop_assignment_type" = "Automatic"
     "maximum_sessions_allowed"         = 16
     "expiration_date"                  = "2022-02-10T18:46:43Z"
     "friendly_name"                    = "Canary"
     "description"                      = "Dedicated to canary deployments."
     "location"                         = "westeurope"
     "vm_count"                         = 1
     "vm_size"                          = "Standard_F4s_v2"
     "vm_prefix"                        = "AZWEUHP01TST"
     "validate_environment"             = "true"
   },
   hp-azprd-weu-wvd-01 = {
     "name"                             = "hp-azprd-weu-wvd-01"
     "type"                             = "Pooled"
     "load_balancer_type"               = "DepthFirst"
     "personal_desktop_assignment_type" = "Automatic"
     "maximum_sessions_allowed"         = 16
     "expiration_date"                  = "2022-02-10T18:46:43Z"
     "friendly_name"                    = "desktop"
     "description"                      = "Dedicated to medium workload type (Microsoft Word, CLIs, ...)."
     "location"                         = "westeurope"
     "vm_count"                         = 1
     "vm_size"                          = "Standard_F4s_v2"
     "vm_prefix"                        = "AZWEUHP01WKT"
     "validate_environment"             = "false"
   },

the ressource bloc :

  resource "azurerm_virtual_desktop_host_pool" "wvd_hostpool" {
  for_each                         = var.wvd_hostpool
  name                             = each.value.name
  location                         = each.value.location
  custom_rdp_properties            = "audiocapturemode:i:1;audiomode:i:0;"
  resource_group_name              = data.azurerm_resource_group.avd_rg.name
  validate_environment             = each.value.validate_environment
  type                             = each.value.type
  load_balancer_type               = each.value.load_balancer_type
  friendly_name                    = each.value.friendly_name
  description                      = each.value.description
  personal_desktop_assignment_type = each.value.personal_desktop_assignment_type
  maximum_sessions_allowed         = each.value.maximum_sessions_allowed

  registration_info {
    expiration_date = each.value.expiration_date
  }
}

I would get the value of the token generate under registration_info to save it to a key vault for reuse or export it to an output but has you can see I getting an error with invalid index. I speding 2 day without sucess at this could you help me please ?

  resource "azurerm_key_vault_secret" "wvd_registration_info" {
  for_each     = var.wvd_hostpool
  name         = each.value.name
  value        = azurerm_virtual_desktop_host_pool.wvd_hostpool[each.value.name].registration_info[0].token
  key_vault_id = azurerm_key_vault.wvd_key_vault.id
  depends_on   = [azurerm_role_assignment.wvd_sp]
}

the same result

Error: Invalid index
│
│   on security.tf line 115, in resource "azurerm_key_vault_secret" "wvd_registration_info":
│  115:   value        = azurerm_virtual_desktop_host_pool.wvd_hostpool[each.value.name].registration_info[0].token
│     ├────────────────
│     │ azurerm_virtual_desktop_host_pool.wvd_hostpool is object with 3 attributes
│     │ each.value.name is "hp-azprd-weu-wvd-02"
│
│ The given key does not identify an element in this collection value: the collection has no elements

on my terraform plan it seem ok but when to apply I getting the same error

azurerm_virtual_desktop_host_pool.wvd_hostpool["hp-azprd-weu-wvd-02"] must be replaced

-/+ resource "azurerm_virtual_desktop_host_pool" "wvd_hostpool" {

      ~ id                               = "/subscriptions//resourceGroups/rg-tst-weu-avd-01/providers/Microsoft.DesktopVirtualization/hostPools/hp-azprd-weu-wvd-02" -> (known after apply)

        name                             = "hp-azprd-weu-wvd-02"

      + personal_desktop_assignment_type = "Automatic" # forces replacement

      - tags                             = {

          - "app" = "avd"

          - "env" = "tst"

          - "prj" = "pop304"

        } -> null

        # (11 unchanged attributes hidden)

      + registration_info {

          + expiration_date = "2022-02-10T18:46:43Z"

          + reset_token     = (known after apply)

          + token           = (sensitive value)

        }

    }

could some of you can help me please

As both for_each use the same variable, you could refer to each.key in this case.

value= lookup(azurerm_virtual_desktop_host_pool.wvd_hostpool[each.key].registration_info,token,"none“}

Thank for you answer but It was an issue of from last azurerm provider version.the register token if are not create and the token value wasn’t not created.I revert back the the provider previous version and it’s work fine.