-
Notifications
You must be signed in to change notification settings - Fork 9.4k
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
Execution order within for_each
loop
#30841
Comments
Thanks for the well-written feature request. As you have noted, Terraform does not currently have a way to ensure these resources are created serially and in order - it will create them in parallel. It is also not possible to use |
Since maps are lexically sorted by keys, someone could think a for_each block would be processed in that order, but that's not the case indeed. |
I am facing the same exact issue and for priorities 1 - 5 (orders) I added 5 blocks, however, that doesn't look right. I really believe that a priority for_each would be a great feature |
@djfinnoy did you manage to work around this without duplicating the resources 10 times? |
No, as far as I know this is not possible right now. |
I have a somewhat similiar issue. I am trying to create several Bigquery view using a
Is there a place where I could upvote this feature request? example of my setup: resource "google_bigquery_table" "bigquery-view" {
for_each = { for idx, record in local.bigquery_views_list : record["view"] => record }
depends_on = [for x in each.value.depends_on : google_bigquery_table.bigquery-view[x]]
dataset_id = each.value.dataset
table_id = each.value.view
view {
...
}
} |
Please use the 👍 on the original issue description to upvote; that is how we determine top issues. You might also try asking this question on the community forum where there are more people ready to help. The GitHub issues here are monitored only by a few core maintainers. Thanks! |
I am having an issue with the If I could make each resource dependent on the last ( ordering is largely irrelevant ) then I could fix it. I was thinking of perhaps a 'depends_on' and using the index function to make it depend on the last. The main issue being that the first one would have a depends_on for no resource and it would break. Would be interesting to hear if anyone has some workarounds... My only way to fix this for now is to literally force Terraform to do |
@boxrick did you ever figure out a better way to do that? Out of curiosity |
Sadly not, I worked around it by changing parallelism to 1. But only for the apply, then disabled a refresh during apply and used the outputted plan file. This then allowed me to use normal parallelism during the much shower plan stage. |
Not only we cannot control parallelism, but we cannot control the order in which resources will be created either... Sigh... And that is true not only for for_each loops, but count loops as well. We have a business requirement that IPs from IP prefixes need to be created with their names reflecting their own IP addresses. For instance, 'pip-101.102.103.104' would have address 101.102.103.104. The way Azure API handles requests support that, for the 1st IP in a prefix will surely be the first available address in it, the 2nd will be the second, and so on. But Terraform falls short and sadly does not offer a way to achieve that allocation pattern. Okay. We can't control parallelism per resource, but we do have the flag '-parallelism=1' to force the resources to be created sequentially. Hurray, let's kill our performance to adhere to our stupid rule! At least that would work, right? Well, think again... Yes, we can control parallelism with that flag. But NO, WE CANNOT ENSURE ORDERING! For some reason known only by Terraform devs (and the Devil himself), a count-loop is NOT executed in order when we use the '-parellelism=1' flag. Example of one such apply:
Why? WHY? Why wouldn't a count-loop be executed in order when parallelism is set to 1? I'm honestly crying inside for having to work with Terraform right now... And yeah, some will come with that old excuse: "TF operates on resources, not resource instances, so we cannot use count or for_each loops to create dependencies between resource instances, and bla, bla, bla..." First, we are not talking about physical dependencies (like that of a VM and a Vnet/Subnet), but simply a logical relationship. Second, why would any iteration loop not execute in order if no parallelism is at play? No respectful language would randomly iterate through a repetition loop. |
Current Terraform Version
Use-cases
for_each
is great for reducing the number ofresource
blocks within your modules.But sometimes one is forced to split a
for_each
resource into multiple resources because you needdepends_on
.Assume you have ten helm charts to install, each chart depends on the previous chart.
In an ideal world, you would simply do this:
As far as I know, this won't work when there are dependencies between the objects in
var.my_charts
, so in the case where there are 10 helm charts that need to be applied in order, you would need ten separate resource blocks.Proposal
It would be nice if one could configure the order in which a
for_each
loop should be applied.An idea that comes to mind is that resoucres/modules that accept
for_each
, also accept an option list of keys that determines execution order, eg:A more flexible approach that could cover situations where the keys are unknown, is to filter based on some key within each objects values:
The text was updated successfully, but these errors were encountered: