How to wait until a given condition is met?

I need to work around an issue in the MongoDB Atlas provider, which causes certain attributes under mongodbatlas_cluster not to be available until the cluster is fully operational (the resource is currently marked as created as soon as the creation request is accepted, not when the cluster is actually ready).

The current workaround is to wait a few minutes and then run terraform refresh (or, in my case, re-apply because other resources depend on the missing attributes).

So I’m hoping to work around this within Terraform to avoid any human intervention, but the only options I’ve found are less than ideal:

  • Using time_sleep (or one of its siblings): I’d have to set a really high number to make sure the attributes will be available (if it completed successfully).
  • Using local-exec to run a script that ends with exit 0 once the cluster is available.

Is there a pure Terraform solution (no *-exec) that could simply wait until a given condition is met? I use Terraform Cloud in case that’s relevant.

Thanks! And happy new year.

Hi @gnarea,

Unfortunately it seems like any solution here depends on being able to write some custom logic to define what it means for a cluster to be ready, which in the Terraform ecosystem is typically the provider’s responsibility and so there isn’t an explicit way to get it done outside of a provider.

Unless you want to write a provider yourself (a heavy lift for a single requirement), the mechanisms to run outside code like shell scripts are the easiest way to include custom logic, as you’ve already seen. Until that provider problem is addressed upstream, I think a local-exec provisioner inside the resource which declares the cluster would be the most straightforward solution.

1 Like