Terraform for_each with YAML variable file for Genesys queues

Looking for advice on using for_each to create multiple queues from a YAML variable file.

I am parsing a YAML file in a Deno pre-processing script to feed into Terraform, but the for_each loop on the genesys_cloud_routing_queue resource fails with ‘Inconsistent result length’ when the YAML list is empty.

Is there a standard way to handle empty lists or do I need a conditional create in the Terraform module?

You should probably look at at handling the empty list case in your Deno preprocessing script rather than fighting Terraform’s for_each constraints. In my local Docker Compose integration tests, I often face similar issues when mock API responses return empty arrays. The cleanest approach is to ensure the Terraform variable is always a map, even if empty, or use a conditional to skip resource creation entirely.

Here is a pattern using local to safely handle the input:

locals {
 queue_data = var.queues != null ? var.queues : {}
}

resource "genesys_cloud_routing_queue" "queue" {
 for_each = local.queue_data
 name = each.value.name
}

This prevents the “Inconsistent result length” error by ensuring for_each always receives a valid map structure. See the Terraform for_each documentation for more on stable keys. Does your Deno script output a map or a list?

The problem is that Terraform’s for_each requires a static map structure, which breaks when your YAML list is empty or dynamic. Use a conditional to ensure the map is never null, as shown below.

{
 "queue_map": {
 "default": {
 "name": "Default Queue",
 "description": "Fallback for empty lists"
 }
 }
}

This is caused by Terraform’s strict requirement for for_each inputs to be maps or sets, which fails when a dynamic list resolves to an empty array.

Cause:
The YAML parser returns an empty list [] instead of an empty map {}, breaking the for_each evaluation.

Solution:
Convert the list to a map in your Deno script before passing it to Terraform, or use local.queue_map = var.queues != [] ? { for q in var.queues : q.name => q } : {} to ensure a valid map structure.

This is caused by Terraform expecting a static map structure, which breaks when the YAML parser returns an empty array. You must normalize the input in your Deno script.

  • Convert the list to a map using q.id as the key.
  • Return {} if the list is empty.
  • Pass this map to for_each.
const map = list.length ? Object.fromEntries(list.map(q => [q.id, q])) : {};