In this lecture, we will take a close look
at the variables block in Terraform.
First, let us look at the different arguments
that a variable block uses.
The variable block in Terraform access three parameters.
The first one, which we have already used is the default parameter.
This is where we specify the default value for a variable.
The others are type and description.
Description is optional, but it is a good practice to use this argument
to describe what the variable is used for.
The type argument is optional as well but when used,
it enforces the type of variable being used.
The basic variable types that can be used are string,
number and Boolean.
String variables, as we have seen in our examples so far,
accept a single value, which can be alphanumeric, that is,
consisting of alphabets and numbers.
The number variable type accepts a single value of a number,
which can be positive or negative,
and the Boolean variable type accepts a value of true or false.
The type parameter, as mentioned previously, is optional.
If it is not specified in the variable block,
it is set to the type "any" by default.
Besides these three simple variable types,
Terraform also supports additional types such as list,
map, set, object, and tuple.
Let us now see how to use all of these in Terraform.
Let us start with list.
A list is a numbered collection of values
and it can be defined like this.
In this example, we have a variable called prefix
that uses a list of values; Mr, Mrs, and Sir.
Why do we call it a numbered collection?
That is because each value, which is also known as an element,
can be referenced by their number or index within that list.
The index of a list always begins at 0.
In this case, the first element of the list at index 0
is the word Mr, the element at index 1 is Mrs,
and the final element at index 2 is Sir.
These variables can be accessed within a configuration file like this,
with the index specified within square brackets.
Hence, the expression var.prefix with index 0 uses the value Mr,
var.prefix with the index 1 uses the value Mrs, and with index 2,
it uses the value of Sir.
Next, let us look at the type called map.
A map is data represented in the format of key-value pairs.
In the variables.tf file, let us create a new variable
called file-content with the type set to map.
In the default values, we can specify as many key-value pairs
enclosed within curly braces.
Here, the statement1 and statement2 are keys,
and the string data following them are values.
Now, to access a specific value
from the map within the Terraform configuration file,
we can make use of key matching.
In this case, we want the content of the local file resource
to be the value of the key called statement2,
and for that, we use an expression var.file-content,
which is the name of the map type variable,
followed by the matching key within square brackets.
We can also combine type constraints.
For example, if you want a list of string type elements,
we can declare it like this.
To use a list of numbers, change it like this.
If the variable values used do not match the type of constraint,
the Terraform commands will fail.
In this example, we have used the type list
where the elements should be of type number,
but the default values are all of type string.
Now, when we run Terraform commands such as a plan or an apply,
you will see an error like this that states
that the default value is not compatible
with the variable type constraint,
and that a number is required and not a string.
The same is applicable with maps as well.
We can use type constraints to make sure that the values
of a map are of a specific type.
In the first variable block, we are using a map of type string,
and in the second one,
we are making use of a map that uses numbers.
Let us now look at sets.
Set is similar to a list.
The difference between a set and a list
is that a set cannot have duplicate elements.
In these examples, we have a variable type of set of strings
or a set of numbers.
The examples on the left are good,
but the ones on the right aren't.
They have duplicate values in them that will throw an error.
The default values are declared just like you would do for a list,
but remember that there shouldn't be any
duplicate values here.
The next type of variable that we are going to look at are objects.
With objects, we can create complex data structures
by combining all the variable types that we have seen so far.
For example, let us consider a new variable called "bella",
which is the name of a cat.
This variable is used to define the different features of this cat,
such as its name which is a string; the color,
which is a string as well; age, which is a number;
the food that it eats, which is a list of strings;
and a Boolean value indicating if it's a favorite pet or not.
Let us now assign some values to this variable.
Let us use name is equal to bella, color is equal to brown,
age is equal to 7, food is "fish", "chicken", and "turkey",
and favorite_pet, which is set to true.
We can use the same default values
within our variable block like this.
The last variable type that we are going to look at is tuples.
Tuple is similar to a list and consists of a sequence of elements.
The difference between a tuple and a list is that list uses
elements of the same variable type such as string or number
but in case of tuple, we can make use of elements
of different variable types.
The type of variables to be used in a tuple is defined
within the square brackets.
In this example, we have three types of elements defined.
The first is a string, second is a number, and finally a Boolean.
The variables to be passed to this should exactly be three
in number and of that specific type for it to work.
Here, we have passed the value of cat to the string element,
the number 7 to the number element,
and true to the Boolean.
Adding additional elements or incorrect type will result
in an error as seen here.
If you add an additional value of dog to the variable,
it will fail as the tuple only expects three elements of type string,
number, and Boolean.
That's it for this lecture.
Let's head over to the hands-on labs and explore working
with variable types in Terraform.