WEBVTT

00:00.520 --> 00:05.740
In this lecture, we will take a look
at the Terraform taint and untaint command.

00:06.500 --> 00:09.280
There would be cases
when a resource creation fails

00:09.280 --> 00:11.280
when we run Terraform apply.

00:11.940 --> 00:14.060
Let's take the same example as before

00:14.060 --> 00:18.000
where we use a local provisioner
to store the public IP address

00:18.000 --> 00:20.160
of the EC2 instance in a file.

00:21.000 --> 00:23.960
However, when we try to apply the changes,

00:23.960 --> 00:25.440
the resource creation failed

00:25.440 --> 00:29.960
because the part specified in the command
of the local exit provisioner is invalid.

00:31.000 --> 00:34.720
When this happens and a resource creation
fails for any reason,

00:34.860 --> 00:37.260
Terraform marks the resource as tainted.

00:37.920 --> 00:41.400
This can be seen when we run
the Terraform plan command again.

00:42.760 --> 00:45.400
The resource called web server is tainted,

00:45.540 --> 00:50.380
and as a result, Terraform will attempt
to recreate it during the subsequent apply.

00:50.840 --> 00:52.200
In this example,

00:52.200 --> 00:56.460
the step that failed was the command
executed by the local exit provisioner.

00:56.960 --> 01:00.200
The EC2 instance itself was
successfully provisioned.

01:00.480 --> 01:04.320
In spite of this, Terraform will attempt
to recreate the entire resource

01:04.320 --> 01:06.460
because it was marked tainted.

01:07.280 --> 01:12.220
Now there would be cases when we want to
force a particular resource to be recreated.

01:12.720 --> 01:17.860
For example, consider a case where manual
changes were made to an AWS instance

01:17.860 --> 01:19.280
which is managed by Terraform,

01:19.460 --> 01:23.880
such as changing the version of nginx
running on it by manual methods.

01:24.280 --> 01:27.120
To reword this, we can either
destroy the resource

01:27.120 --> 01:30.880
using the command Terraform destroy,
and then run apply again,

01:31.080 --> 01:34.100
or the better way would be
to taint the resource

01:34.100 --> 01:36.800
using the Terraform taint command like this.

01:37.380 --> 01:39.440
When we run Terraform plan next,

01:39.440 --> 01:42.280
we will see that the resource
is set to be recreated.

01:43.460 --> 01:46.800
To undo a taint that was applied
for a specific resource,

01:46.800 --> 01:49.900
use the Terraform untaint command like this.

01:50.740 --> 01:55.020
This resource will not be recreated now
during a subsequent Terraform apply.

