0.13 - Parallel module for_each executions

While using for_each in modules, I noticed that it doesn’t behave as I would expect from an ordinary loop.
Each iteration run in parallel, that is, while iteration 0 is execution iteration 1 starts. Is this intended? If so, is it possible to execute sequentially?

There’s the parallelism flag for apply, which applies to the entire operation.

You might have luck using depends_on or a normal variable to route output from one instance to another. Something along the lines of:

locals {
  set = toset(["1", "2"])
module mod {
  for_each = local.set
  sequencecontrol = index(local.set, each.key) > 0 ? mod[index(local.set,each.key) - 1].output : None

I haven’t tried anything resembling the above.

Why do instances of the module need to run sequentially? Do they have real interdependencies, or is it a limitation of the provider or host system?

It might be easier to chain explicit instantiation rather than attempt to control how terraform walks the graph. Eg

module mod1 {
  source = "./mymodule"

module mod2 {
  source = "./mymodule"
  depends_on = module.mod1

Hi, thank you for coming back.

Had tried the suggestion of indexed based outputs for modules iteration but it creates cyclic dependencies and terraform doesn’t like that.
Unfortunately it is related to for_each not supporting inline provider definition. In this case, it is to use kubectl/helm, had to refactor the modules to not depend on the providers configuration because of multiple cluster but hit this limitation. So refactor did nothing :smiley: .

Unfortunately I’ll have to explicitly instantiate each module instance, which defeats the purpose of having for_each.

It would be really handy to be able to execute the iterations in sequence.

1 Like