Is it possible to iterate over a complex yaml structure to spawn queues without hardcoding the map keys? i’m trying to keep our infra code clean and decided to move queue definitions into a separate yaml variable file. the idea is simple: load the yaml, decode it, and use for_each to create the resources. but terraform is throwing a fit about the map keys.
here’s the yaml snippet (queue_vars.yaml):
queues:
support_us:
name: "US Support"
description: "General support for US region"
out_of_office_enabled: false
support_eu:
name: "EU Support"
description: "General support for EU region"
out_of_office_enabled: true
and the tf block:
data "external" "queue_config" {
program = ["python3", "scripts/yaml_to_json.py", "${path.module}/queue_vars.yaml"]
}
locals {
queue_map = yamldecode(file("${path.module}/queue_vars.yaml")).queues
}
resource "genesyscloud_routing_queue" "this" {
for_each = local.queue_map
name = each.value.name
description = each.value.description
out_of_office_enabled = each.value.out_of_office_enabled
# ... other standard settings
}
the error i’m getting is:
Error: Invalid for_each argument
on main.tf line 10, in resource "genesyscloud_routing_queue" "this":
10: for_each = local.queue_map
The given "for_each" argument value is unsuitable: the "for_each" value must be a map, or set of strings, and you have provided a value of type object.
i know i can convert the yaml to a map of maps in python and use the external data source, but that feels like a hack. i’ve seen examples using yamldecode directly with simple lists, but nested objects seem to break the for_each requirement for string keys. is there a way to flatten this or extract the keys dynamically so for_each sees a map? or am i stuck writing a custom script to output json that terraform can ingest as a proper map? using terraform 1.5 and the latest genesyscloud provider. thanks.