WEBVTT

00:00.000 --> 00:01.290
-In this lecture,

00:01.290 --> 00:05.860
we will take a look at the Count Meta-Argument
and its uses in Terraform.

00:06.670 --> 00:10.010
One of the easiest ways to create
multiple instances

00:10.010 --> 00:13.440
of the local file is to make use
of the count meta-argument.

00:13.670 --> 00:14.930
To do this,

00:14.930 --> 00:18.810
simply add an argument called count
with a value greater than one.

00:19.080 --> 00:22.230
Here,
we have used count is equal to three.

00:22.440 --> 00:24.950
When we try to run Terraform plan now,

00:24.950 --> 00:29.140
we can see that Terraform tries
to create three resources instead of one.

00:30.680 --> 00:35.970
In the output of the Terraform apply command,
we can see that three resources are created.

00:35.970 --> 00:41.870
The resources are identified by pet[0],
where zero is within square brackets,

00:41.870 --> 00:44.210
pet[1] and pet[2].

00:44.780 --> 00:48.580
The resource is now considered to be a list
of resources

00:48.580 --> 00:51.530
with elements at index 0, 1 and 2.

00:51.780 --> 00:55.100
However,
there is one problem with this approach.

00:55.760 --> 00:58.580
Since we have only specified the count,

00:58.580 --> 01:01.940
Terraform will try to create
the same resource three times.

01:01.940 --> 01:04.330
Since the filename is not unique,

01:04.330 --> 01:07.110
Terraform will recreate the same file
three times

01:07.110 --> 01:11.250
rather than creating three separate files
which defeats the purpose of this task.

01:12.010 --> 01:17.220
A better way to do this and make sure that all
the three resources have unique filenames

01:17.220 --> 01:20.120
is to make use of a list variable
for filename.

01:20.570 --> 01:21.830
To do this,

01:21.830 --> 01:24.950
we have used default values
with three elements,

01:24.950 --> 01:28.400
each corresponding to the name of the file
that we want to create.

01:29.070 --> 01:30.080
Next,

01:30.080 --> 01:34.050
we want Terraform to make use of each element
of this list as the value

01:34.050 --> 01:35.430
of the filename argument.

01:35.830 --> 01:37.300
In this example,

01:37.300 --> 01:41.450
Terraform should make three iterations
as the count has a value of three.

01:41.760 --> 01:47.900
The first iteration should pick up the element
at index 0 which is the file called pets.txt.

01:48.170 --> 01:52.700
This is followed by element at index 1
which is dogs.txt

01:52.700 --> 01:56.400
and finally cats.txt at index 2.

01:57.070 --> 02:02.380
To use this within the configuration file,
we can make use of count.index

02:02.380 --> 02:05.060
in the expression for filename like this.

02:05.850 --> 02:08.070
Now when we are on Terraform apply,

02:08.070 --> 02:12.220
we can see that there are three files created
inside this /root directory.

02:12.850 --> 02:17.140
What if we were to add a few more elements
to the list in the future?

02:17.140 --> 02:23.490
Say we wanted to add a few more files
by the name of /root/cows.txt

02:23.490 --> 02:25.350
and ducks.txt.

02:25.740 --> 02:28.560
If we were to apply this configuration now,

02:28.560 --> 02:31.800
we would see that it would still create
only three files,

02:31.800 --> 02:35.400
because we have set the count
to a static value of 3.

02:35.890 --> 02:40.790
We want the count to automatically pick up
the number of items that are defined

02:40.790 --> 02:42.200
within the filename variable.

02:42.200 --> 02:43.380
To do this,

02:43.380 --> 02:47.140
we can set set the value of count to use
a built-in function

02:47.140 --> 02:48.950
that would return the length of the list.

02:49.310 --> 02:54.290
This built-in function called length will set
the value of count to 5.

02:55.040 --> 02:57.810
Terraform offers several built-in functions

02:57.810 --> 03:00.760
that allow us to manipulate values
within expressions.

03:00.760 --> 03:04.840
One simple function that we can use here
is the length function.

03:05.500 --> 03:09.210
The Length function is used to calculate
the size of a list

03:09.210 --> 03:12.530
and we can use this function
in the count meta-argument

03:12.530 --> 03:15.590
to dynamically determine
the size of the filename variable.

03:16.470 --> 03:19.100
We are now ready to run Terraform apply.

03:19.100 --> 03:20.660
Before we do that,

03:20.660 --> 03:23.750
let’s change the default value
for the filename variable

03:23.750 --> 03:25.210
back to three elements.

03:26.230 --> 03:27.420
That is it.

03:27.420 --> 03:30.190
If we run Terraform plan and apply now,

03:30.190 --> 03:34.320
we should see that three resources
with distinct files names have been created.

03:35.390 --> 03:40.200
There is however a significant drawback
when using the count meta-argument

03:40.200 --> 03:41.900
to loop through variables this way.

03:41.900 --> 03:46.140
To illustrate this,
lets see the same example but this time,

03:46.140 --> 03:50.370
let us remove the element /root/pets.txt
from the list.

03:51.410 --> 03:53.620
If we run Terraform plan now,

03:53.620 --> 03:57.050
we see that instead of deleting
just one resource

03:57.050 --> 04:00.390
with the filename /root/pets.txt,

04:00.390 --> 04:04.300
Terraform is replacing two resources
and deleting one resource.

04:04.690 --> 04:06.180
Why does it do that?

04:06.520 --> 04:10.360
We only want to remove the first element
in this list

04:10.360 --> 04:14.510
and it looks like all elements are going
to be replaced by this operation.

04:15.300 --> 04:19.590
To see why this is happening,
let us first understand how count works.

04:19.960 --> 04:25.750
As we saw before, when we use count,
the resources become a list of resources.

04:26.160 --> 04:28.120
To see this using Terraform,

04:28.120 --> 04:31.220
let us add an output variable
to the main.tf file

04:31.220 --> 04:33.220
to print all details of this resource.

04:33.470 --> 04:35.720
Using the Terraform output command,

04:35.720 --> 04:38.950
we can see that the resource is now
in the format of a list.

04:39.740 --> 04:44.480
Originally, the resource called pet
is a list with three resources,

04:44.480 --> 04:46.830
each identified by its index.

04:46.830 --> 04:51.880
The first resource in the list creates
the file by the name pets.txt

04:51.880 --> 04:55.090
and it is identified by pet with the index [0],

04:55.090 --> 04:58.200
zero enclosed in square brackets
since it's a list.

04:58.590 --> 05:04.120
The second resource element is pet[1]
which creates a file called dogs.txt

05:04.120 --> 05:09.230
and the third resource is pet[2],
which creates a file called cats.txt.

05:09.920 --> 05:12.820
The first element in a list is always zero.

05:12.820 --> 05:18.280
As a result, when we deleted the element
called /root/pets.txt,

05:18.280 --> 05:24.620
which was at index 0 to begin with,
the element with value /root/dogs.txt

05:24.620 --> 05:27.350
shifts up and takes its place at index 0.

05:27.350 --> 05:33.720
Likewise, /root/cats.txt becomes the element
at index 1 and the list

05:33.720 --> 05:35.590
now has only two elements in it.

05:36.460 --> 05:38.870
When we run Terraform plan now,

05:38.870 --> 05:42.990
Terraform can see that that the resources
at index pet[0] and pet[1]

05:42.990 --> 05:44.740
have to be destroyed and replaced.

05:44.740 --> 05:47.080
This is owing to the change
in their filenames.

05:47.080 --> 05:51.610
There is no resource pet[2],
so it will delete this resource entirely.

05:52.240 --> 05:56.300
Although after the apply operation
we will have the resources created

05:56.300 --> 05:59.760
as per our intended end state,
this is not an ideal approach.

05:59.760 --> 06:03.080
We may not want the resources to be destroyed
and recreated

06:03.080 --> 06:06.420
just because we removed an unrelated element
from the list.

06:06.420 --> 06:09.150
We will see how to do fix this
in the next lecture.

06:09.770 --> 06:13.600
Now, let’s head over to the hands-on labs
and practice working

06:13.600 --> 06:15.780
with the count meta-arguments in Terraform.

