-In this lecture,
we will take a look at the Count Meta-Argument and its uses in Terraform.
One of the easiest ways to create multiple instances
of the local file is to make use of the count meta-argument.
To do this,
simply add an argument called count with a value greater than one.
Here, we have used count is equal to three.
When we try to run Terraform plan now,
we can see that Terraform tries to create three resources instead of one.
In the output of the Terraform apply command, we can see that three resources are created.
The resources are identified by pet[0], where zero is within square brackets,
pet[1] and pet[2].
The resource is now considered to be a list of resources
with elements at index 0, 1 and 2.
However, there is one problem with this approach.
Since we have only specified the count,
Terraform will try to create the same resource three times.
Since the filename is not unique,
Terraform will recreate the same file three times
rather than creating three separate files which defeats the purpose of this task.
A better way to do this and make sure that all the three resources have unique filenames
is to make use of a list variable for filename.
To do this,
we have used default values with three elements,
each corresponding to the name of the file that we want to create.
Next,
we want Terraform to make use of each element of this list as the value
of the filename argument.
In this example,
Terraform should make three iterations as the count has a value of three.
The first iteration should pick up the element at index 0 which is the file called pets.txt.
This is followed by element at index 1 which is dogs.txt
and finally cats.txt at index 2.
To use this within the configuration file, we can make use of count.index
in the expression for filename like this.
Now when we are on Terraform apply,
we can see that there are three files created inside this /root directory.
What if we were to add a few more elements to the list in the future?
Say we wanted to add a few more files by the name of /root/cows.txt
and ducks.txt.
If we were to apply this configuration now,
we would see that it would still create only three files,
because we have set the count to a static value of 3.
We want the count to automatically pick up the number of items that are defined
within the filename variable.
To do this,
we can set set the value of count to use a built-in function
that would return the length of the list.
This built-in function called length will set the value of count to 5.
Terraform offers several built-in functions
that allow us to manipulate values within expressions.
One simple function that we can use here is the length function.
The Length function is used to calculate the size of a list
and we can use this function in the count meta-argument
to dynamically determine the size of the filename variable.
We are now ready to run Terraform apply.
Before we do that,
let’s change the default value for the filename variable
back to three elements.
That is it.
If we run Terraform plan and apply now,
we should see that three resources with distinct files names have been created.
There is however a significant drawback when using the count meta-argument
to loop through variables this way.
To illustrate this, lets see the same example but this time,
let us remove the element /root/pets.txt from the list.
If we run Terraform plan now,
we see that instead of deleting just one resource
with the filename /root/pets.txt,
Terraform is replacing two resources and deleting one resource.
Why does it do that?
We only want to remove the first element in this list
and it looks like all elements are going to be replaced by this operation.
To see why this is happening, let us first understand how count works.
As we saw before, when we use count, the resources become a list of resources.
To see this using Terraform,
let us add an output variable to the main.tf file
to print all details of this resource.
Using the Terraform output command,
we can see that the resource is now in the format of a list.
Originally, the resource called pet is a list with three resources,
each identified by its index.
The first resource in the list creates the file by the name pets.txt
and it is identified by pet with the index [0],
zero enclosed in square brackets since it's a list.
The second resource element is pet[1] which creates a file called dogs.txt
and the third resource is pet[2], which creates a file called cats.txt.
The first element in a list is always zero.
As a result, when we deleted the element called /root/pets.txt,
which was at index 0 to begin with, the element with value /root/dogs.txt
shifts up and takes its place at index 0.
Likewise, /root/cats.txt becomes the element at index 1 and the list
now has only two elements in it.
When we run Terraform plan now,
Terraform can see that that the resources at index pet[0] and pet[1]
have to be destroyed and replaced.
This is owing to the change in their filenames.
There is no resource pet[2], so it will delete this resource entirely.
Although after the apply operation we will have the resources created
as per our intended end state, this is not an ideal approach.
We may not want the resources to be destroyed and recreated
just because we removed an unrelated element from the list.
We will see how to do fix this in the next lecture.
Now, let’s head over to the hands-on labs and practice working
with the count meta-arguments in Terraform.