Reprovision on terraform apply

Terraform is a great tool for cloud provisioning and if you are not already I highly suggest to look into it. When cloud resources are already provisioned with Terraform sometimes we need to do reprovisioning, ideally each time we run terraform apply. That is not supported in Terraform, at least not yet. However, it has a great feature which enables you to do reprovisioning on infrastructure change. An example would be when the new node is added to AWS and it needs to be joined to other nodes in the cluster. We can do this with null_resource, which acts as any other resource, but it has support for triggers. The trigger needs to be some value that will change, otherwise, it will not run every time.

To learn more about null_resource please take a look at official docs

AWS resource provisioning

Here is an example of AWS instance provisioning with null_resource:

As you can see, this is just sudo apt-get update command, which does not make perfect sense, but it is used just for demonstration purposes. The trigger is rerun variable and it will be different on each run. This is important because using static variable will not work. I defined rerun variable and used built in uuid() function as a value to make sure it is different on each terraform apply. Connection details are AWS instance details from mean resource and we use the same private key and public IP address.

Finally running terraform apply looks like this:

If you are new with Terraform and looking for a reprovision feature, this will do the job.

Follow me

Alen Komljen

Building and automating infrastructure with Docker, Kubernetes, kops, Helm, Rancher, Terraform, Ansible, SaltStack, Jenkins, AWS, GKE and many others.
Follow me

Latest posts by Alen Komljen (see all)

Alen KomljenReprovision on terraform apply