terraform-google-modules/terraform-google-gcloud

"random_id.cache is empty tuple"

Closed this issue · 3 comments

mrzor commented

Terraform 0.12.28

I have a module that contains two instances of the gcloud module.
Here is the configuration for one of them:

module "ffhq-config" {
  source        = "terraform-google-modules/gcloud/google"
  platform      = "linux"
  enabled       = var.copy_ffhq_config
  skip_download = true
  upgrade       = false

  create_cmd_entrypoint = "gsutil"
  create_cmd_body       = "..." # irrelevant

  destroy_cmd_entrypoint = "gsutil"
  destroy_cmd_body       = "..." # irrelevant

  # Having this runs `gcloud auth activate-service-account` which has the
  # unfortunate side-effect of changing the gcloud auth on the system running Terraform
  service_account_key_file = "..." # irrelevant
}

terraform plan, terraform apply, terraform refresh all function properly.
However, for some odd, largely unrelated terraform import executions, the following errors will show up:

Error: Invalid index

  on .terraform/modules/lab-hugo.base_image/terraform-google-gcloud-1.3.0/main.tf line 19, in locals:
  19:   cache_path           = local.skip_download ? "" : "${path.module}/cache/${random_id.cache[0].hex}"
    |----------------
    | random_id.cache is empty tuple

The given key does not identify an element in this collection value.


Error: Invalid index

  on .terraform/modules/lab-hugo.ffhq-config/terraform-google-gcloud-1.3.0/main.tf line 19, in locals:
  19:   cache_path           = local.skip_download ? "" : "${path.module}/cache/${random_id.cache[0].hex}"
    |----------------
    | random_id.cache is empty tuple

The given key does not identify an element in this collection value.


Error: Invalid index

  on .terraform/modules/lab-test-zor-auto-1.base_image/terraform-google-gcloud-1.3.0/main.tf line 19, in locals:
  19:   cache_path           = local.skip_download ? "" : "${path.module}/cache/${random_id.cache[0].hex}"
    |----------------
    | random_id.cache is empty tuple

The given key does not identify an element in this collection value.


Error: Invalid index

  on .terraform/modules/lab-test-zor-auto-1.ffhq-config/terraform-google-gcloud-1.3.0/main.tf line 19, in locals:
  19:   cache_path           = local.skip_download ? "" : "${path.module}/cache/${random_id.cache[0].hex}"
    |----------------
    | random_id.cache is empty tuple

The given key does not identify an element in this collection value.

skip_download is true. I'm not sure if Terraform evaluates both sides of the conditional expression or if local.skip_download is false in this context for some reason.

I had a similar problem in my own configuration - where I could fix it myself - and the only fix I know of is to use try("${path.module}/cache/${random_id.cache[0].hex}", null), which should fix this issue - at the cost of replacing this error by a more cryptic one in the case of skip_download=false and random_id.cache is indeed an empty tuple.

I am seeing this with Terraform v0.12.29 and with the v2.0.2 release.

Error: Invalid index

  on .terraform/modules/gke-cstack.gcloud_wait_for_cluster/google/gcloud/main.tf line 25, in locals:
  25:   download_override = var.enabled ? data.external.env_override[0].result.download : ""
    |----------------
    | data.external.env_override is empty tuple

The given key does not identify an element in this collection value.

It only shows up when doing an import but it will prevent the import from succeeding even if the resource is elsewhere in the state.

For my issue, patching main.tf line 25 with

  download_override = var.enabled && length(data.external.env_override) > 0 ? data.external.env_override[0].result.download : ""

appears to have resolved the issue.

This issue is stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 7 days