Specify parallelism for null_resource?

Hello

There is possibility to specify null_resource to run it one by one? Not in parallel mode?
But I need only to do this fore some resource, not at all.

Hi @skydion,

There is no way to constrain the concurrency of individual objects, only for the entire graph walk. Internally Terraform is using a semaphore to limit concurrency, and so it’s only aware of the number of active nodes, not which individual nodes are active.

However, since we’re talking about null_resource I expect you’re probably more interested in controlling the sequencing of provisioners rather than of resources – null_resource doesn’t actually do anything by itself – in which case the common answer to forcing a set of steps to happen one by one in a particular sequence is to list them out as a series of provisioners attached to a single resource, like this:

resource "null_resource" "example" {
  provisioner "local-exec" {
    # ...
  }

  provisioner "local-exec" {
    # ...
  }

  provisioner "local-exec" {
    # ...
  }
}

The individual provisioners associated with a single resource will always run sequentially in the order you declared them, once the relevant provider has completed the “create” step for the associated resource.

Thanks for the answer @apparentlymart
Okay, looks I should use something like lock files, because my null_resources changing the same file, and some time this configuration is broken…

Indeed, if you have multiple resources all attempting to manipulate the same file and there is no way to merge it all into a single resource as I showed, then locking in the external programs might be the best answer.

There is a possible middle-ground here, though: perhaps you could change each of the separate resources to write to a separate file and then have one final resource whose job is to collect all of those separate files and produce a merged file from them. That way you can use dependencies to specify that the merging must happen only after all of the other files have been created, but still let the individual resources run concurrently when possible.

How practical that is will of course depend on what sort of files we’re talking about and whether “merge” is a meaningful operation, but hopefully that gives you another option to consider at least.

Thanks for reply @apparentlymart

But situation is not so simple, because there are running command which generate config files, and then restart service… And if few null_resources do the same simultaneously I got broken configuration. At this moment I use simple lock file and problem is gone.

I think the suggestion would be that instead of each of the null resources trying to write to the same file you change things so each writes to their own file, and then a final null resource takes those files and combines them together as well as restarting the service.

That final resource can use triggers and depends_on to try to ensure it always runs when needed as well as being done after the others.