The "for_each" value depends on resource attributes that cannot be determined until apply - with data resources as source

hey everyone,

I am familiar with the root cause of the “The “for_each” value depends on resource attributes that cannot be determined until apply”, but in my case I am getting this error that I cannot explain.

So, we have a tfe_workspace_ids data resource that reads all workspaces from our TFE instance:

data "tfe_workspace_ids" "all" {
 names        = ["*"]
 organization = "XXX"
}

then we filter out some workspaces and create a map with knows keys

locals {
    required_workspace_ids = { for k, v in data.tfe_workspace_ids.all.external_ids : format("XXX/%s", k) => v if length(regexall("test-\\d+_workspace", k)) > 0 }
}

Lastly, we create TFE variables for those workspaces.


resource "tfe_variable" "kv_replication_sp" {
  for_each = merge(local.required_workspace_ids,
    {
       workspace_1 = module.landing_zone.workspace_1.id
     }
   )

    key = "sp_credentials"
    value = {
        object_id       = module.xxx.id
        client_id       = module.xxx.client_id
        client_secret   = module.xxx.secret
      }
 

    category     = "terraform"
    hcl          = false
    sensitive    = true
    workspace_id = each.key
}

in the example above we already have TFE variable created for all workspaces in local.platform_service_workspace_ids and now I am trying to add a workspace_1 = module.landing_zone.workspace_1.id into the for_each map using merge function.

However, it produces the error “The “for_each” value depends on resource attributes that cannot be determined until apply”.

From what I know about terraform the data objects are evaluated during the plan. So, I’d expect terraform to have all workspace IDs from data resource and create a map in locals with known keys during the plan stage. I also add a new k/v pair using merge into for_each map where the key is hardcoded. So, essentially all keys should be known to Terraform during plan stage.

So, my only suspicion is that merge function is not evaluated during the plan stage. Hence, TF doesn’t have the list of knows keys in the map when creating plan.

btw, we are using terraform version 0.12.31