Terraform GC Provider: for_each with YAML vars for queues

Trying to generate multiple Genesys Cloud queues from a YAML variable file using for_each. The YAML structure looks like this:

queues:
 - name: "Support-EN"
 description: "English Support"
 out_of_office_enabled: false
 - name: "Support-FR"
 description: "French Support"
 out_of_office_enabled: true

I’m loading this in Terraform with yamldecode(file("vars.yaml")) and iterating over var.queues.queues. The issue is that genesyscloud_routing_queue requires a unique name for each resource, but for_each keys need to be stable strings. Using the queue name as the key works for creation, but if I rename a queue in the YAML, Terraform tries to destroy and recreate it instead of updating. I tried using toset on the names, but then I lose the mapping to the descriptions and other attributes. Is there a clean way to handle this without hardcoding resource blocks? The current setup breaks on any name change.

resource "genesyscloud_routing_queue" "queue" {
 for_each = { for q in var.queues.queues : q.name => q }
 name = each.value.name
 description = each.value.description
 out_of_office_enabled = each.value.out_of_office_enabled
}

The issue is that yamldecode returns a list, but for_each needs a map or set. You can’t iterate a list directly with for_each because it requires unique keys. The quickest fix is to convert that list into a map using toset or a for expression if you need to preserve the index.

Try this in your locals block:

locals {
 queue_map = { for idx, q in yamldecode(file("vars.yaml")).queues : idx => q }
}

resource "genesyscloud_routing_queue" "support_queues" {
 for_each = local.queue_map

 name = each.value.name
 description = each.value.description
 
 out_of_office_enabled = each.value.out_of_office_enabled
}

This creates a map where the keys are just indices (0, 1, etc.). It’s not pretty, but it works. If you want cleaner keys, change your YAML to use map syntax instead of a list. Lists are fine for count, but for_each demands uniqueness. Also, watch out for the out_of_office_enabled default. If it’s missing in some entries, Terraform might complain about inconsistent types. Ensure every object in that YAML has the same keys.