For_each with a local CSV file containing data source target

Hi, I am trying to set up an environment using resources for Genesys Cloud . I came across a problem I have been struggling with for over a week.

I used for_each on resource genesyscloud_routing_queue


locals {
  rg = csvdecode(file("csvQueueCreation.csv"))
}

resource "genesyscloud_routing_queue" "csvdecode" {

  for_each = { for rg in local.rg : rg.number => rg }

  name                              = each.value.QueueName
  division_id = each.value.disivionId
  description                       = each.value.description
  acw_wrapup_prompt                 = var.queue_acw_wrapup_prompt
  acw_timeout_ms                    = var.queue_acw_timeout_ms
  skill_evaluation_method           = var.queue_skill_evaluation_method
  queue_flow_id                     = null
  whisper_prompt_id                 = null
  auto_answer_only                  = var.queue_auto_answer_only
  enable_transcription              = var.queue_enable_manual_assignment
  enable_manual_assignment          = var.queue_enable_manual_assignment
  calling_party_name                = var.queue_calling_party_name
  outbound_messaging_sms_address_id = null
  media_settings_call {
    alerting_timeout_sec      = var.queue_alerting_timeout_sec_call
    service_level_percentage  = var.queue_service_level_percentage_call
    service_level_duration_ms = var.queue_service_level_duration_ms_call
  }
  media_settings_email {
    alerting_timeout_sec      = var.queue_alerting_timeout_sec_email
    service_level_percentage  = var.queue_service_level_percentage_email
    service_level_duration_ms = var.queue_service_level_duration_ms_email
  }
 wrapup_codes = [

   ]
}

my CSV File

number,QueueName,divisionId,description,queueTerraformName
1,testQueueName,data.genesyscloud_auth_division.home.id,test,test

the problem is that the values for Name and description as string are passed without any problem from CSV file, but the value for divisionId which is really a data source that I need to trigger I cannot put with for_each , Is there any way to pass in my example data source name with for_each from CSV file?

Warm regards,
Dawid

Hi @motomeru,

There is no way to refer dynamically to a resource, because Terraform needs to see all of the references between objects statically in order to build the dependency graph before evaluating anything.

You mentioned that you can’t use for_each to look up this data, which would’ve been a relatively easy answer because then you could put the instance keys inside the CSV file and then looked them up like this:

  division_id = data.genesyscloud_auth_division.all[each.value.divisionId].id

However, you can get a similar effect by constructing your own mapping from predefined symbolic names to the data resource objects:

locals {
  divisions = {
    home = data.genesyscloud_auth_division.home
    # and any others you need
  }
}

Then you can use the CSV data to look up in this local value map:

  division_id = local.divisions[each.value.divisionId].id

For this to work, you’d set the divisionId column in your CSV to contain just home, which is a valid key in local.divisions and therefore locates the correct object. (To make this easier to follow it might be better to rename the field to divisionKey, if you’re able to control the structure of that file.)

From the perspective of dependencies, Terraform can see that genesyscloud_routing_queue.csvdecode refers to local.divisions and therefore depends on everything that local value depends on, and so the dependency graph will be conservative, which is to say that Terraform will wait until all of the data resources are finished reading before processing genesyscloud_routing_queue.csvdecode, regardless of which specific division keys are included in the CSV file, and thus ensure the correct ordering.

@apparentlymart thank you very much for your help, I used local mapping, everything works perfect!