I’m completely stumped as to why my for_each loop fails when creating queues from a YAML file using the genesyscloud provider. I get a state drift error immediately.
Error: Invalid for_each argument. A value on the left side of for_each must be a map or set of strings.
My environment:
- Terraform 1.5.7
- genesyscloud provider 1.20.0
- Python 3.10 for YAML parsing
Here is the snippet:
resource "genesyscloud_routing_queue" "this" {
for_each = var.queues
name = each.value.name
}
The YAML output is a list of objects, not a map. How do I convert it?
Cause: YAML parsing often returns lists or complex objects, not the flat map required by for_each.
Solution: Convert the parsed structure explicitly.
locals {
queue_map = { for q in yaml_queue_list : q.name => q }
}
resource "genesyscloud_routing_queue" "q" {
for_each = local.queue_map
}
The main issue here is that for_each requires a map or set, but your YAML parser likely returns a list of objects.
- Define a local variable to convert the list to a map keyed by a unique identifier like
name.
- Use that local map in the
for_each attribute of the genesyscloud_routing_queue resource.
locals {
queue_map = { for q in var.queue_list : q.name => q }
}
resource "genesyscloud_routing_queue" "this" {
for_each = local.queue_map
name = each.value.name
}