Terraform for_each with yaml var file failing on resource creation

Context:

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.

locals {
 queues = yamldecode(file("queues.yaml"))
}

resource "genesyscloud_queue" "this" {
 for_each = local.queues
 name = each.value.name
}

Question:

How do I correctly map the YAML keys to for_each without hitting the index error? The YAML structure is a simple list of objects.

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.

  • YAML to HCL type conversion
  • Terraform for_each object requirements
  • Genesys Cloud queue naming constraints

I’d recommend looking at at the genesyscloud_queue resource limitations before scaling this.

  • The API has strict rate limits that Terraform will hit immediately with for_each.
  • Use the Analytics API to validate queue existence first, then batch create via SDK instead of state management.