Home > Net >  Terraform : for_each one by one
Terraform : for_each one by one

Time:11-21

I have created a module on terraform, this module creates aws_servicecatalog_provisioned_product resources.
When I call this module from the root I am using for_each to run into a list of objects.
The module runs into this list of objects and creates the aws_servicecatalog_provisioned_product resources in parallel.
Is there a way to create the resources one by one? I want that the module will wait for the first iteration to be done and to create the next just after.

CodePudding user response:

Is there a way to create the resources one by one?

Sadly, there is not such way, unless you remove for_each and create all the modules separately with depends_on.

TF is not a procedural language, and it always will do things in parallel for for_each and count.

CodePudding user response:

You have to remove the for_each and use depends_on for every element if you want to make sure that they are created one after another.

If you want only the first resource to be provisioned before other resources:

Separate the first resource only and use the for_each for the remaining resources. You can put an explicit dependency using depends_on for the remaining resources to depend on the first one. Because for_each expects a set or a map, this input would require some modification to be able to exclude the provisioning of the first resource.

A more drastic approach, if you really need to provision resources one by one, would be to run the apply command with -parallelism=1. This would reduce the number of resources provisioned in parallel to 1. This would apply to the whole project. I would not recommend this, since it would increase drastically the running time for the apply.

CodePudding user response:

Why do you want it to wait for the previous creation? Terraform relies on the provider to know what can happen in parallel and will run in parallel where it can.

Setting the parallelism before the apply operation would be how I would limit it artificiality if I wanted to as it's an technical workaround that keeps your Terraform code simple to read.

TF_CLI_ARGS_apply="-parallelism=1"
terraform apply

If you find this is slowing down all Terraform creations but you need this particular set of resources to be deployed one at a time then it might be time to break these particular resources out into their own Terraform config directory and apply it in a different step to the rest of the resources again with the parallelism setting.

  • Related