Quick question about using for_each with a YAML variable file to create multiple queues. I am parsing the YAML in locals and passing it to the resource block, but the apply fails with Error: Invalid index on the for_each argument.
The best way to fix this is ensuring the YAML structure outputs a map object, not a list. Terraform for_each requires an object or map. If yamldecode returns a list, you get the invalid index error. You must convert the list to a map using keys as identifiers.
locals {
queues_raw = yamldecode(file("queues.yaml"))
queues_map = { for q in local.queues_raw : q.name => q }
}
resource "genesyscloud_queue" "this" {
for_each = local.queues_map
name = each.value.name
}
This pattern is critical for Genesys Cloud resources like users, teams, and webchat configs. I see this fail constantly when devs copy-paste YAML structures without checking the HCL type conversion. The API expects unique names or IDs, so the map key must be stable.