In this lecture,
we will see the different ways we can use provisioners in Terraform.
In the previous lecture,
the remote exec and the local-exec provisioners we used,
run tasks when a resource is created.
This is the default behavior
and is known as the creation-time provisioner.
Let's use the same example of the local-exec provisioner
that we used in the previous lecture.
When applied,
the provisioner will record the public IP address of the webserver
on the local machine.
We can also make the provisioner run before a resource is destroyed.
This is called a destroy-time provisioner.
This can be specifically set
by making use of the event argument
as specifying its value as destroy,
like this.
Another default behavior of a provisional block
is to cause a Terraform apply to fail
if the tasks specified in it fails for any reason.
For example,
if the creation-time provisioner, that we used earlier,
makes use of an incorrect path in the command,
the default behavior would be
for the Terraform apply to fail with an error, like this.
Here,
there is no /temp directory located in the local machine,
and this results in a failure.
The same behavior can be specified
by adding an on_failure argument in the provisional block
with a value of fail,
like this.
This is the default value,
so it's not really required to be set.
Please note, that when this happens,
Terraform marks the resource as tainted.
We have a lecture on resource taint in the following section.
We may not always want the apply step to fail
if the provisioner fails.
For example,
let's say, the provisioner script is optional
and we don't care if it fails,
we want the apply step to ignore this error and proceed.
To override the default behavior
and for apply to go through successfully,
use the on_failure argument with "continue" as the value.
When we run Terraform apply now,
the resource will be provisioned successfully.
Well, yhat brings us to the end of this lecture.
Head over to the Hands-On Labs
and practice working with Terraform provisioners.