Skip to main content
In this lesson, you’ll learn how to leverage provisioners in Terraform to execute scripts or commands during resource creation or destruction. Provisioners offer flexibility by allowing you to perform custom actions on your resources, such as logging details or cleaning up after resource deletion.

Creation-Time Provisioners

Creation-time provisioners are triggered when a resource is created. Terraform supports both remote and local execution of commands during this phase. For instance, the example below launches an AWS EC2 instance and uses the local-exec provisioner to record the instance’s public IP address to a local file:
After applying this configuration, you can verify the output by running:
Remember to verify that the specified file path exists and is writable by your user.

Destroy-Time Provisioners

Terraform also allows you to execute provisioners right before a resource is destroyed. By setting the when argument to destroy, you can define commands to be executed during the resource’s teardown process. The example below shows a resource block that contains both creation and destroy-time provisioners:
After the resource is destroyed, checking the file might show an output similar to:

Handling Provisioner Failures

By default, if a provisioner fails, Terraform stops the execution and the entire terraform apply fails. For example, if the command in the creation-time provisioner attempts to write to a non-existent directory, Terraform will produce an error:
Running terraform apply with this configuration may produce an output similar to:
When this error occurs, Terraform marks the associated resource as tainted, indicating it may need to be recreated.
Ensure that any file paths or commands used in provisioners are valid and tested. Failing provisioners can block your deployment process.

Allowing Provisioner Failures

In some cases, the execution of a provisioner may be optional and should not halt the entire apply process if it fails. To override the default behavior, set the on_failure argument to continue:
With the on_failure option set to continue, Terraform proceeds with the apply process even if the provisioner fails:
This concludes the lesson on Terraform provisioners. Experiment with these configurations to solidify your understanding and enhance your Terraform deployments. For more information, consider visiting the following resources:

Watch Video

Practice Lab