-
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
Better user experience when using for_each with lists #29904
Comments
Hi @acdha, thanks for filing the issue. The premise here seems to be founded on some misunderstanding of the error condition, but perhaps we can try and find out how we could better communicate this with users. If I understand correctly what you mean by "Expose the dependency graph", there is no missing dependency when you use the All the error is trying to express, is that the keys for the |
This is what I was referring to with the dependency graph: in most of the cases I've encountered, all of the provided values are known when Terraform starts (i.e. using only variables or resource properties set from variables) so it seems like Terraform should be able count the number of unique values, just as it does with the map syntax, so the resource with the I understand that there are cases where something like a data provider or a dependency on a value provided by a resource would not allow that optimization. |
Thanks @acdha, in the original example above, if Terraform also cannot count the number of unique values, because the values are unknown, and there is no way to determine their uniqueness. A value of |
@jbardin so maybe something like Terraform introducing of auto targeting with multiple apply stages would be a workaround to this issue? Because leaving this problem to the users doing partial applies is not something we can script in the end. For example in the piplines which deploy infrastructure we would have to know beforehand and script all those targets. |
We have many requests over the years for something like automatic targeting (or progressive apply, partial apply, "stacks", there have been many names), but no holistic workflow has yet to be devised that covers the desired use cases. You can start here for some background: #4149. This is definitely still something we're very interested in, but I think the final solution may take a bit different form. Even with that feature in mind however, we would still recommend restructuring the configuration to avoid the error altogether, rather than working around it with multiple application stages. Perhaps the suggestion in the error output is a little misleading, since we don't necessarily recommend using Since we have other features in mind to cover similar use cases, I'm going to close this one out. Thanks for the feedback! |
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. |
Terraform somewhat recently added stricter validation to resource's
for_each
arguments. This is generally good for avoiding bugs but I've found one area where the experience could be more ergonomic:In many cases, the data I'm using for the
for_each
argument is provided in a list. This causes an error, of course, so the easiest option is to wrap it intoset()
:This works in many cases, especially on existing projects, but use of the function has the subtle effect of breaking the dependency analysis. This means that if you use any resource which does not already exist you'll get an error like this:
This is somewhat slow going — 30 seconds to get the lock + state from Terraform cloud, a couple of minutes to refresh resources, repeat to add
-target …
, etc. — and it feels like a flaw that Terraform can't tell the length of a static input. This is notably different from the experience with a map literal, which always works:Two ways to improve the experience come to mind:
toset()
so it would work like the previous experience with lists and current experience with maps.set(aws_iam_user.user1, aws_iam_user.user2)
would have a known size and would also be able to return an error if you referenced the same value twice.The text was updated successfully, but these errors were encountered: