Terraform for_each with YAML variable file for queues fails on parse

Error: Invalid for_each argument

Trying to create queues from a YAML file using for_each = var.queues where var.queues is loaded via yamldecode. The provider throws this parse error even though the JSON structure matches the docs.

terraform {
 required_providers {
 genesyscloud = {
 source = "mypurecloud/genesyscloud"
 }
 }
}

variable "queues" {
 type = any
 default = yamldecode(file("./queues.yaml"))
}

resource "genesyscloud_routing_queue" "this" {
 for_each = var.queues
 name = each.value.name
}

You using the latest version of the purecloud/genesyscloud provider? Sometimes older versions choke on complex nested maps from yamldecode.

I ran into this exact issue last week. The problem is usually that yamldecode returns a list of maps, but for_each needs a map where the keys are unique strings. If your YAML is a simple list, Terraform doesn’t know how to index it for the for_each loop.

Try converting the list to a map using the index as the key. It’s a bit ugly but it works reliably.

variable "queues_raw" {
 type = any
 default = yamldecode(file("./queues.yaml"))
}

locals {
 # Convert list to map. Key is index, Value is the queue object
 queues_map = { for idx, q in var.queues_raw : idx => q }
}

resource "genesyscloud_routing_queue" "this" {
 for_each = local.queues_map

 name = each.value.name
 description = each.value.description
 
 # ... other attributes
}

Make sure your YAML file is actually a list structure, not a map of maps. If it’s a map, you might not need the locals conversion, but the 409 error suggests the provider is seeing duplicate keys or invalid types. Check the output of terraform console by typing var.queues_raw to see exactly what structure you’re feeding into the resource.