Terraform for_each with YAML vars fails on queue creation

We are trying to automate queue provisioning by reading a YAML file into Terraform and using for_each to iterate over the list. The environment is using terraform 1.6.5 and the genesyscloud provider version 1.28.0. The setup feels straightforward, but the plan phase throws a cryptic type mismatch error.

The variable file queues.yaml contains a simple list of objects:

queues:
 - name: "Support-DE"
 description: "German Support"
 - name: "Support-UK"
 description: "UK Support"

In the main module, we load this using yamldecode and pass it to the resource:

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

resource "genesyscloud_routing_queue" "queue" {
 for_each = { for q in yamldecode(var.queue_config).queues : q.name => q }
 name = each.value.name
}

The error states: "Invalid for_each argument". It claims that the element each.value is a map while the iteration expects a specific structure. We have verified that yamldecode returns the expected map of lists. The issue seems to stem from how Terraform interprets the nested map during the for_each expansion. We need the exact syntax to map the YAML list items to the queue resource attributes without hitting this type error.

The YAML loader usually spits out a list, but for_each needs a map or set. You’ll want to use toset() on the IDs or convert the list to a map using index. Here’s a quick fix: for_each = toset([for q in var.queues : q.id]). That should clear up the type mismatch.