WEBVTT

00:01.000 --> 00:01.940
In this lecture,

00:02.110 --> 00:05.770
we will see the different ways
we can use provisioners in Terraform.

00:07.420 --> 00:08.630
In the previous lecture,

00:08.990 --> 00:12.280
the remote exec and the
local-exec provisioners we used,

00:12.460 --> 00:14.990
run tasks when a resource is created.

00:15.980 --> 00:17.520
This is the default behavior

00:17.690 --> 00:20.190
and is known as the
creation-time provisioner.

00:21.170 --> 00:24.080
Let's use the same example
of the local-exec provisioner

00:24.250 --> 00:26.100
that we used in the previous lecture.

00:26.820 --> 00:27.450
When applied,

00:27.620 --> 00:31.030
the provisioner will record the
public IP address of the webserver

00:31.200 --> 00:32.520
on the local machine.

00:33.860 --> 00:37.600
We can also make the provisioner run
before a resource is destroyed.

00:38.160 --> 00:40.680
This is called
a destroy-time provisioner.

00:41.790 --> 00:43.330
This can be specifically set

00:43.500 --> 00:45.590
by making use of the event argument

00:45.760 --> 00:48.030
as specifying its value as destroy,

00:48.190 --> 00:48.990
like this.

00:50.070 --> 00:52.490
Another default behavior
of a provisional block

00:52.610 --> 00:54.940
is to cause a Terraform apply to fail

00:55.170 --> 00:58.460
if the tasks specified in it
fails for any reason.

00:59.320 --> 01:00.140
For example,

01:00.260 --> 01:03.220
if the creation-time provisioner,
that we used earlier,

01:03.380 --> 01:06.070
makes use of an incorrect path
in the command,

01:06.280 --> 01:07.650
the default behavior would be

01:07.820 --> 01:11.280
for the Terraform apply to fail
with an error, like this.

01:12.490 --> 01:12.930
Here,

01:13.100 --> 01:17.130
there is no /temp directory
located in the local machine,

01:17.270 --> 01:19.150
and this results in a failure.

01:20.090 --> 01:21.800
The same behavior can be specified

01:21.960 --> 01:25.360
by adding an on_failure argument
in the provisional block

01:25.570 --> 01:26.940
with a value of fail,

01:27.110 --> 01:27.960
like this.

01:29.000 --> 01:30.530
This is the default value,

01:30.690 --> 01:32.800
so it's not really required
to be set.

01:33.920 --> 01:35.760
Please note, that when this happens,

01:35.930 --> 01:38.160
Terraform marks the resource
as tainted.

01:39.110 --> 01:42.410
We have a lecture on resource taint
in the following section.

01:43.580 --> 01:46.120
We may not always want
the apply step to fail

01:46.290 --> 01:47.860
if the provisioner fails.

01:48.490 --> 01:49.280
For example,

01:49.450 --> 01:51.830
let's say,
the provisioner script is optional

01:52.070 --> 01:53.860
and we don't care if it fails,

01:54.310 --> 01:57.880
we want the apply step
to ignore this error and proceed.

01:59.050 --> 02:01.100
To override the default behavior

02:01.220 --> 02:03.370
and for apply to go through
successfully,

02:03.550 --> 02:07.200
use the on_failure argument
with "continue" as the value.

02:08.280 --> 02:10.280
When we run Terraform apply now,

02:10.490 --> 02:12.720
the resource will be provisioned
successfully.

02:13.780 --> 02:16.330
Well, yhat brings us
to the end of this lecture.

02:16.870 --> 02:18.320
Head over to the Hands-On Labs

02:18.490 --> 02:21.040
and practice working
with Terraform provisioners.

