I am trying to create a large number of resources for a certain service in the sequence in which it is defined the TF file, but it is currently being picked in a random sequence.
Let’s say I have 30 resources to be created for the same service and it needs to be created in a sequence only, as the creation of the resource affects the priority setting on the service directly (Something we can not handle as it is managed by Cloudflare). Now when I try Terraform Apply on the TF file, the resources get created in random sequence, thus causing discrepancy in priority order.
I am aware of the usage of
depends_on module, but as the number of resources are huge and the changes are very frequent, maintaining this module is too much work. Link
Is there any other way to create them in a required sequence?
There is no order implied by the sequence of resource blocks in a Terraform configuration. The configuration is declarative and the required order of operations will be determined by Terraform at runtime. The way to define an order between resources is to reference them directly in the resource configuration, or if there is no useful attribute value to reference between the configurations you can use
depends_on to reference the resource as a whole.
If you are referring to instances expanded via
for_each, there is not currently any way to enforce any order between those instances, and they would need to be declared separately to define the dependencies. This may be something to bring up with the provider developers, since it would be preferable to have a way to declare the desired priority order rather than have it implied via the creation order.
We would probably need to see an example of what you are trying to do in order to give more specific advice.
Thank you for your detailed response. That was very very helpful.
I will take the required changes needed to the respective team.