Trying to spin up a bunch of queues from a YAML file using for_each. The YAML looks like this:
queues:
sales:
name: Sales Support
description: Primary sales queue
tech:
name: Tech Support
description: Level 1 tech
I’m reading it with file() and trying to iterate:
locals {
queue_data = yamldecode(file("queues.yaml"))
}
resource "genesyscloud_routing_queue" "queue" {
for_each = local.queue_data.queues
name = each.value.name
...
}
Plan fails with Inappropriate value for attribute "for_each": element keys must be of type string. The keys in the YAML map are strings, so I’m not sure why Terraform is complaining. I’ve tried converting the map to a set of strings with keys(), but then I lose the values for name and description. Is there a clean way to flatten this or do I need to restructure the YAML? The docs show examples with maps, but nothing with nested objects like this.
nah, that structure is tricky because yamldecode gives you a map, but for_each needs a set of strings or a map of objects. if your keys (sales, tech) are static, you’re fine. if they change, you’ll break state.
try wrapping it in a for expression to extract just the queue configs. it’s cleaner than trying to index into the nested map directly in the resource block.
locals {
queue_data = yamldecode(file("queues.yaml"))
# extract the list of queue objects
queue_configs = [
for key, val in local.queue_data.queues : val
]
}
resource "genesyscloud_routing_queue" "q" {
for_each = { for q in local.queue_configs : q.name => q }
name = each.value.name
description = each.value.description
enabled = true
}
just make sure name is unique across your yaml. if it’s not, the map key collision will crash the plan. also, watch out for missing required attributes like outbound_email or wrap_up_policy if your yaml doesn’t have them. terraform will scream.