-
Notifications
You must be signed in to change notification settings - Fork 9.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Resource for_each not working with empty list #22281
Comments
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:
|
Ahh, thanks for reporting this! Indeed, this is a quirk caused by the fact that in For now I think the most reliable workaround would be to manually convert to a map instead of a set, which shouldn't run into the same problem because map keys are always strings: for_each = { for v in var.datalab_user_list : v => v } A conversion like the above is essentially what Terraform itself does when handling a set of strings, but it checks the set element type first to produce that "Invalid for_each set element" error. |
This threw me off here too here #22622 I use a resource with I think the fix could be for_each checks the length first and aborts when 0. If there are items, then check that they are strings. |
I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues. If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further. |
Terraform Version
Terraform Configuration Files
Debug Output
Crash Output
Expected Behavior
Terraform plan and apply are successful and no resources are created
Actual Behavior
Steps to Reproduce
Try to use an empty list with for_each on a resource.
Additional Context
As a workaround, this seems to work fine:
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
The text was updated successfully, but these errors were encountered: