Invalid index -- module is object with 2 attributes

Hello All,

When I attempt to utilize the cluster name from the map below, I receive an error about an Invalid index. could anyone help me solve this please?

> { for name in local.confluent_values : name.service_account => name... }
{
  "test" = [
    {
      "cluster_name" = "dev"
      "service_account" = "test"
      "topic" = "topic-1"
    },
    {
      "cluster_name" = "nonprod_01"
      "service_account" = "test"
      "topic" = "topic-3"
    },
  ]
}
module "confluent_service_account" {
  for_each = { for name in local.confluent_values : name.service_account => name... }
  source = ".//modules/confluent_service_account"
  display_name = "${each.key}-service-account"
}

module "api_key" {
  source = ".//modules/confluent_api_key"
  for_each = {for name in local.confluent_values : "${name.service_account}:${name.cluster_name}" => name }
  display_name = "${each.value.service_account}-api-key"
  service_account_id = values(module.confluent_service_account)[*].id
  cluster_name = each.value.cluster_name
}


╷
│ Error: Invalid index
│ 
│   on main.tf line 23, in module "api_key":
│   23:   service_account_id = module.confluent_service_account[each.key].id
│     ├────────────────
│     │ each.key is "test:dev"
│     │ module.confluent_service_account is object with 2 attributes
│ 
│ The given key does not identify an element in this collection value.
╵
╷
│ Error: Invalid index
│ 
│   on main.tf line 23, in module "api_key":
│   23:   service_account_id = module.confluent_service_account[each.key].id
│     ├────────────────
│     │ each.key is "test-1:dev"
│     │ module.confluent_service_account is object with 2 attributes
│ 
│ The given key does not identify an element in this collection value.
╵
╷
│ Error: Invalid index
│ 
│   on main.tf line 23, in module "api_key":
│   23:   service_account_id = module.confluent_service_account[each.key].id
│     ├────────────────
│     │ each.key is "test:nonprod_01"
│     │ module.confluent_service_account is object with 2 attributes
│ 
│ The given key does not identify an element in this collection value.

Thanks

Hi @jjkishore1,

You seem to be using the keys from module.api_key to select instances of module.confluent_service_account, but that’s isn’t valid because their for_each expressions each select different keys.

The confluent_service_account module seems to use the service_account attribute values for its instance keys, and so you could find the corresponding key using each.value.service_account inside the module "api_key" block:

module "api_key" {
  source   = ".//modules/confluent_api_key"
  for_each = {
    for v in local.confluent_values :
    "${v.service_account}:${v.cluster_name}" => v
  }

  display_name       = "${each.value.service_account}-api-key"
  service_account_id = module.confluent_service_account[each.value.service_account].id
  cluster_name       = each.value.cluster_name
}

Thank you very much @apparentlymart , it worked as expected