profile
viewpoint

Ask questionsResource for_each not working with empty list

<!-- Hi there,

Thank you for opening an issue. Please note that we try to keep the Terraform issue tracker reserved for bug reports and feature requests. For general usage questions, please see: https://www.terraform.io/community.html.

If your issue relates to a specific Terraform provider, please open it in the provider's own repository. The index of providers is at https://github.com/terraform-providers . -->

Terraform Version

<!--- Run terraform -v to show the version, and paste the result between the ``` marks below.

If you are not running the latest version of Terraform, please try upgrading because your issue may have already been fixed. -->

Terraform v0.12.6
+ provider.google v2.11.0
+ provider.null v2.1.2
+ provider.random v2.1.2

Terraform Configuration Files

<!-- Paste the relevant parts of your Terraform configuration between the ``` marks below.

For large Terraform configs, please use a service like Dropbox and share a link to the ZIP file. For security, you can also encrypt the files using our GPG public key. -->

variable "datalab_user_list" {
    type = list(string)
    default = []

}
resource "google_service_account" "datalab_service_account" {
    for_each = toset(var.datalab_user_list)
    account_id = "datalab-${split("@", each.key)[0]}"
    project = google_project.hipaa_project.project_id
}

Debug Output

<!-- Full debug output can be obtained by running Terraform with the environment variable TF_LOG=trace. Please create a GitHub Gist containing the debug output. Please do not paste the debug output in the issue, since debug output is long.

Debug output may contain sensitive information. Please review it before posting publicly, and if you are concerned feel free to encrypt the files using the HashiCorp security public key. -->

Crash Output

<!-- If the console output indicates that Terraform crashed, please share a link to a GitHub Gist containing the output of the crash.log file. -->

Expected Behavior

<!-- What should have happened? --> Terraform plan and apply are successful and no resources are created

Actual Behavior

<!-- What actually happened? -->

Error: Invalid for_each set argument

  on .terraform/modules/hipaa_worker_project1/datalab.tf line 23, in resource "google_service_account" "datalab_service_account":
  23:     for_each = toset(var.datalab_user_list)

The given "for_each" argument value is unsuitable: "for_each" supports maps
and sets of strings, but you have provided a set containing type dynamic.

Steps to Reproduce

<!-- Please list the full steps required to reproduce the issue, for example:

  1. terraform init
  2. terraform apply --> Try to use an empty list with for_each on a resource.

Additional Context

<!-- Are there anything atypical about your situation that we should know? For example: is Terraform running in a wrapper script or in a CI system? Are you passing any unusual command line options or environment variables to opt-in to non-default behavior? --> As a workaround, this seems to work fine:

variable "datalab_user_list" {
    type = "map"
    default = {}

}
resource "google_service_account" "datalab_service_account" {
    for_each = var.datalab_user_list
    account_id = "datalab-${split("@", each.key)[0]}"
    project = google_project.hipaa_project.project_id
}

However, I don't really need a map and it makes what I pass in to the module a bit more confusing to read.

References

<!-- Are there any other GitHub issues (open or closed) or Pull Requests that should be linked here? For example:

  • #6017

-->

hashicorp/terraform

Answer questions adarobin

I also tried:

variable "datalab_user_list" {
    type = list(string)
    default = [ "" ]
}
resource "google_service_account" "datalab_service_account" {
    for_each = toset(compact(var.datalab_user_list))
    account_id = "datalab-${split("@", each.key)[0]}"
    project = google_project.hipaa_project.project_id
}

But that still throws a similar error:

Error: Invalid for_each set argument

  on .terraform/modules/hipaa_worker_project1/datalab.tf line 24, in resource "google_service_account" "datalab_service_account":
  24:     for_each = toset(compact(var.datalab_user_list))

The given "for_each" argument value is unsuitable: "for_each" supports maps
and sets of strings, but you have provided a set containing type dynamic.
useful!
source:https://uonfu.com/
Github User Rank List