Misplaced Index Error During Vault Alias Lookup

Terraform module error: Cannot find proper key in alias auth backend. New code sample below shows altered variable and function names. Error invalid index. Please advise.

data "vault_backend" "authGroup" {
  for_each = { for entry in var.aliases: entry.mode => entry }
  endpoint = each.value.auth_location
}

resource "vault_id_alias" "entry" {
  for_each = { for entry in var.aliases: entry.label => entry }
  accessor = lookup(data.vault_backend.authGroup[each.key], "handler", null)
  entity   = vault_identity.main.id
}

hey, i suspect the error comes from mismatched keys in your for_each blocks. check if the used keys in your alias block match the keys in the authGroup mapping. could be a typo in the naming.

hey, im wonderin if the key mismats might be deeper? maybe the alias keys were changed vs values? did you try comparing printed outputs? curious if others have seen similar issues with mapping dynamic keys.

The error might also be due to assumptions about what keys you get from the for_each mapping. I encountered a similar issue when combining computed values with manual map keys in Terraform. The key mismatch is often due to the alias used in the for_each expressions, which may differ from the lookup key in the data block. Consider checking the data source outputs to confirm that the key in data.vault_backend.authGroup exactly corresponds to each.key in your resource block. Validate that your variable structure aligns with the module’s expectations.

hey, seems like a key match issue. i’d check if both for_each expressions create the same key – sometimes computed keys get messed up. might be worth verifying var.aliases structure. hope this helps!

I encountered a similar issue where dynamic key construction led to misaligned mappings. In addition to checking for typos, I found that adding intermediary outputs for debugging can be very insightful. By printing intermediate map keys, I confirmed that the transformation of the var.aliases structure was not behaving as expected, which clarified the root cause. Adjusting the transformation logic to create consistent keys resolved the error. Careful alignment of both structures during debugging can help pinpoint these discrepancies early in the development process.