WEBVTT

00:00.360 --> 00:01.300
-In this lecture,

00:01.300 --> 00:04.280
we will take a look at
the for_each meta argument

00:04.400 --> 00:06.000
and its uses in Terraform.

00:07.180 --> 00:10.040
In the previous lecture,
we saw that when we use count,

00:10.180 --> 00:12.300
the resources are created as a list,

00:12.860 --> 00:15.780
and this can have undesirable results
when updating them.

00:16.740 --> 00:18.460
One way to overcome this

00:18.460 --> 00:21.840
is to make use of the for_each argument
insert of count,

00:22.300 --> 00:22.980
like this.

00:24.220 --> 00:28.700
Next, to set the value of file name
to each element in the list,

00:28.980 --> 00:32.820
we can make use of the expression
is.value like this.

00:34.020 --> 00:35.440
However, there's a catch.

00:36.100 --> 00:37.880
If we run Terraform plan now,

00:38.280 --> 00:39.380
we will see an error.

00:40.560 --> 00:44.080
The for_each argument only works
with a map or a set.

00:44.760 --> 00:46.440
In the variables.tf file,

00:46.440 --> 00:49.840
we are currently making use of a list
containing string elements.

00:51.320 --> 00:53.060
There are a couple of ways to fix this.

00:53.780 --> 00:57.420
Either change the variable called filename
to the type set.

00:58.360 --> 00:59.600
In the variables lecture,

00:59.600 --> 01:01.940
we learned that a set is similar to a list,

01:02.160 --> 01:04.660
but it cannot contain duplicate elements.

01:05.640 --> 01:08.720
Once we change the variable type
and then run Terraform plan,

01:09.260 --> 01:11.700
we should see that there are three files
to be created.

01:13.220 --> 01:14.920
Another way to fix this error

01:15.140 --> 01:17.640
while retaining the variable type as a list

01:17.820 --> 01:19.980
is to make use of another built-in function.

01:20.820 --> 01:23.600
This time, we'll make use of the toset function,

01:23.760 --> 01:26.760
which will convert the variables
from a list to a set.

01:28.120 --> 01:29.240
Once this is done,

01:29.240 --> 01:31.700
Terraform plan command should now work
as expected.

01:33.400 --> 01:35.460
Now let us replicate the same scenario

01:35.460 --> 01:38.420
as the one we did earlier
with the count meta argument

01:38.720 --> 01:43.760
and delete the first element with
the value/slash/root pets.txt from the list.

01:44.980 --> 01:46.700
When we run Terraform plan now,

01:47.020 --> 01:49.860
we can see that only one resource
is said to be destroyed,

01:50.160 --> 01:52.760
the file with the name pets.txt.

01:54.000 --> 01:56.080
The other resources will be untouched.

01:57.240 --> 01:58.700
To see how this is working,

01:58.700 --> 02:01.300
let us create an output variable called pets

02:01.420 --> 02:05.140
to print the resource details like we did
with the example using count.

02:06.480 --> 02:08.040
From the Terraform output command,

02:08.040 --> 02:12.420
we can now see that the resources
are stored as a map and not a list.

02:13.240 --> 02:16.080
When we use for_each instead of count,

02:16.280 --> 02:19.080
the resources are created as a map
and not a list.

02:19.800 --> 02:23.460
This means that the resources
are no longer identified by the index,

02:23.720 --> 02:27.120
thereby bypassing the issues
that we saw when we use count.

02:28.120 --> 02:30.520
They are now identified by the keys

02:30.520 --> 02:33.780
which are file names/root/..txt

02:33.960 --> 02:36.300
and /root/cat.txt,

02:36.480 --> 02:39.940
which are used by the for_each argument
in the configuration file.

02:41.180 --> 02:43.700
If we compare this to the earlier output

02:43.780 --> 02:45.660
that we got when we use count,

02:45.760 --> 02:48.580
we can see the difference
in how the resources are created.

02:49.220 --> 02:54.360
The count option created it as a list
and the for_each created it as a map.

02:55.320 --> 02:57.880
There are a few other meta arguments
in Terraform

02:57.880 --> 03:01.300
such as provisioners, providers, backends,
et cetera.

03:01.880 --> 03:03.880
We will see that later in this course.

03:05.260 --> 03:07.780
Now let's head over to the hands-on labs

03:07.780 --> 03:11.820
and practice working
with the for_each meta argument in Terraform.

