Terraform for_each queue creation from YAML fails with invalid type

Trying to spin up queues using for_each based on a YAML variable file. The docs show a simple map, but my YAML parser returns a list of objects. Terraform complains about the type mismatch immediately.

variable “queues” {
type = any
default = yamldecode(file(“queues.yaml”))
}

resource “genesyscloud_routing_queue” “main” {
for_each = var.queues
name = each.value.name
}

Error: Invalid for_each argument. Can’t figure out how to flatten or map this list properly.

this is a classic terraform headache. the issue is that yamldecode returns whatever structure your yaml has, and if it’s a list of objects, for_each chokes because it expects a map or set. you can’t just iterate a list directly with for_each unless you convert it.

the cleanest fix is to use a locals block to transform that list into a map keyed by something unique, like the queue name. then you use that map in your resource.

locals {
 # transform the list into a map keyed by name
 # assuming your yaml is like:
 # - name: "Sales"
 # description: "Sales Queue"
 queue_map = { for q in var.queues : q.name => q }
}

resource "genesyscloud_routing_queue" "main" {
 for_each = local.queue_map
 
 name = each.key
 description = each.value.description
 
 # don't forget the required members, or it'll fail validation
 members = [
 {
 member_id = "12345" # replace with actual user/queue id lookup
 skill = {}
 }
 ]
}

if your yaml structure is slightly different, just adjust the for expression. the key is that for_each needs a map where the key is the identifier. also, double check that var.queues isn’t null before passing it to the local, or you’ll get a null reference error.