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
for_each cannot predict how many instances with lookup and resource reference #25700
Comments
Hi @kimor79! Sorry for this odd behavior, and thanks for reporting it. It seems like the terraform/lang/funcs/collection.go Lines 280 to 282 in ef071f3
Specifically, it's exiting early with an unknown value if there's an unknown value nested anywhere in the data structure of the first argument, rather than considering only the first nesting level. Until that's improved, I think you could get this working by using the more general for_each = try(local.items.bar, {}) The documentation for for_each = local.items.bar |
Yes, this is a simplified example for the bug report and just to make sure it wasn't user error in the actual config :) In my use case I'm |
I had a very similar issue to this a while ago, IIRC because part of my data structure referred to an uninitialised My workaround (which looks like it may work for you to, but it's certainly very frustrating) was to do a targeted apply (with |
Ok, here's a simplified version of our actual config
this results in:
if I swap out the
so it would seem that |
I think I'm having the same issue, with terraform 0.13.0. main.tf locals {
instances = toset(["1"])
}
provider null {}
resource null_resource r1 {
for_each = local.instances
}
resource null_resource r2 {
for_each = null_resource.r1
}
module m {
source = "./mod"
for_each = null_resource.r1
} mod/module.tf resource null_resource n { } $ terraform apply
Error: Invalid for_each argument
on main.tf line 17, in module "m":
17: for_each = null_resource.r1
The "for_each" value depends on resource attributes that cannot be determined
until apply, so Terraform cannot predict how many instances will be created.
To work around this, use the -target argument to first apply only the
resources that the for_each depends on. null_resource r2 has no problem with for_each's input coming from null_resource r1's map. As @ashleydavies mentioned, using -target to create resource the module depends on first works. |
same here and use case is
Error appear after plan
However if i first created redis and then re-execute the plan , it works... |
@gogovan-vincentngai This is my case...
|
my example, at least, seems to be working with tf-0.14 for both
|
Thanks @kimor79! I've confirmed that this is fixed in 0.14 -- since we have no plans to do further work on older versions of Terraform, I'm closing this issue. |
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
I should see a plan similar to:
Actual Behavior
I see a plan like:
Steps to Reproduce
terraform init
terraform plan
Additional Context
If
local.items
doesn't contain any references to resources, the plan "works", ie I see the expected behavior ifid = null_resource.main.id,
is commented out.References
The text was updated successfully, but these errors were encountered: