WEBVTT

00:01.870 --> 00:04.160
In this lecture,
we will take a close look

00:04.240 --> 00:06.410
at the variables
block in Terraform.

00:07.510 --> 00:09.730
First, let us look at the
different arguments

00:09.810 --> 00:11.480
that a variable block uses.

00:12.740 --> 00:15.740
The variable block in Terraform
access three parameters.

00:16.280 --> 00:20.220
The first one, which we have
already used is the default parameter.

00:21.150 --> 00:24.240
This is where we specify the
default value for a variable.

00:25.490 --> 00:27.770
The others are type
and description.

00:29.030 --> 00:32.820
Description is optional, but it is
a good practice to use this argument

00:32.900 --> 00:35.040
to describe what
the variable is used for.

00:36.000 --> 00:39.420
The type argument is optional
as well but when used,

00:39.500 --> 00:41.980
it enforces the type
of variable being used.

00:43.010 --> 00:46.530
The basic variable types
that can be used are string,

00:46.610 --> 00:47.900
number and Boolean.

00:48.810 --> 00:52.300
String variables, as we have
seen in our examples so far,

00:52.380 --> 00:56.410
accept a single value, which
can be alphanumeric, that is,

00:56.490 --> 00:58.920
consisting of
alphabets and numbers.

01:00.030 --> 01:03.650
The number variable type accepts
a single value of a number,

01:03.730 --> 01:06.010
which can be
positive or negative,

01:06.870 --> 01:11.050
and the Boolean variable type
accepts a value of true or false.

01:12.900 --> 01:16.430
The type parameter, as mentioned
previously, is optional.

01:17.280 --> 01:19.630
If it is not specified
in the variable block,

01:19.710 --> 01:22.640
it is set to the type
"any" by default.

01:24.000 --> 01:26.370
Besides these three
simple variable types,

01:26.450 --> 01:30.180
Terraform also supports
additional types such as list,

01:30.260 --> 01:32.730
map, set, object, and tuple.

01:33.690 --> 01:36.380
Let us now see how to use
all of these in Terraform.

01:37.330 --> 01:38.590
Let us start with list.

01:39.460 --> 01:42.120
A list is a numbered
collection of values

01:42.200 --> 01:44.220
and it can be defined like this.

01:45.800 --> 01:48.860
In this example, we have a
variable called prefix

01:49.170 --> 01:52.880
that uses a list of values;
Mr, Mrs, and Sir.

01:54.590 --> 01:56.190
Why do we call it a
numbered collection?

01:58.150 --> 02:01.930
That is because each value,
which is also known as an element,

02:02.750 --> 02:06.030
can be referenced by their
number or index within that list.

02:07.360 --> 02:10.040
The index of a list
always begins at 0.

02:11.160 --> 02:15.120
In this case, the first element
of the list at index 0

02:15.200 --> 02:20.300
is the word Mr, the element
at index 1 is Mrs,

02:20.770 --> 02:24.120
and the final element
at index 2 is Sir.

02:24.810 --> 02:28.640
These variables can be accessed
within a configuration file like this,

02:29.750 --> 02:33.120
with the index specified
within square brackets.

02:33.920 --> 02:39.790
Hence, the expression var.prefix
with index 0 uses the value Mr,

02:40.590 --> 02:46.380
var.prefix with the index 1 uses
the value Mrs, and with index 2,

02:46.460 --> 02:48.320
it uses the value of Sir.

02:49.500 --> 02:52.440
Next, let us look at
the type called map.

02:53.000 --> 02:56.890
A map is data represented in
the format of key-value pairs.

02:58.010 --> 03:01.070
In the variables.tf file,
let us create a new variable

03:01.150 --> 03:04.410
called file-content with
the type set to map.

03:05.320 --> 03:09.370
In the default values, we can
specify as many key-value pairs

03:09.450 --> 03:11.150
enclosed within curly braces.

03:12.160 --> 03:15.130
Here, the statement1
and statement2 are keys,

03:15.530 --> 03:18.240
and the string data
following them are values.

03:19.060 --> 03:21.420
Now, to access a specific value

03:21.500 --> 03:24.490
from the map within the
Terraform configuration file,

03:24.570 --> 03:26.120
we can make use of key matching.

03:27.110 --> 03:30.560
In this case, we want the
content of the local file resource

03:30.640 --> 03:33.280
to be the value of
the key called statement2,

03:34.480 --> 03:38.770
and for that, we use an
expression var.file-content,

03:38.850 --> 03:41.270
which is the name of
the map type variable,

03:41.350 --> 03:44.220
followed by the matching
key within square brackets.

03:44.990 --> 03:46.880
We can also combine
type constraints.

03:47.690 --> 03:50.750
For example, if you want a
list of string type elements,

03:50.830 --> 03:52.350
we can declare it like this.

03:53.030 --> 03:55.880
To use a list of numbers,
change it like this.

03:56.740 --> 04:00.130
If the variable values used
do not match the type of constraint,

04:00.210 --> 04:01.640
the Terraform
commands will fail.

04:02.210 --> 04:05.400
In this example,
we have used the type list

04:05.480 --> 04:07.820
where the elements
should be of type number,

04:08.450 --> 04:11.100
but the default values
are all of type string.

04:12.150 --> 04:16.540
Now, when we run Terraform
commands such as a plan or an apply,

04:16.620 --> 04:19.010
you will see an error
like this that states

04:19.090 --> 04:21.310
that the default value
is not compatible

04:21.390 --> 04:22.910
with the variable type constraint,

04:23.760 --> 04:26.320
and that a number
is required and not a string.

04:27.180 --> 04:29.390
The same is applicable
with maps as well.

04:29.970 --> 04:32.590
We can use type constraints to
make sure that the values

04:32.670 --> 04:34.880
of a map are of a specific type.

04:35.870 --> 04:40.160
In the first variable block, we
are using a map of type string,

04:40.550 --> 04:41.700
and in the second one,

04:41.780 --> 04:44.510
we are making use of
a map that uses numbers.

04:45.880 --> 04:47.420
Let us now look at sets.

04:48.190 --> 04:49.880
Set is similar to a list.

04:50.420 --> 04:52.080
The difference between
a set and a list

04:52.160 --> 04:55.180
is that a set cannot have
duplicate elements.

04:56.490 --> 05:00.590
In these examples, we have a
variable type of set of strings

05:00.670 --> 05:01.760
or a set of numbers.

05:02.430 --> 05:04.860
The examples on
the left are good,

05:04.940 --> 05:06.570
but the ones on
the right aren't.

05:07.340 --> 05:10.410
They have duplicate values in
them that will throw an error.

05:11.110 --> 05:14.670
The default values are declared
just like you would do for a list,

05:14.750 --> 05:16.530
but remember that there
shouldn't be any

05:16.610 --> 05:17.880
duplicate values here.

05:19.450 --> 05:22.910
The next type of variable that
we are going to look at are objects.

05:23.660 --> 05:26.440
With objects, we can create
complex data structures

05:26.520 --> 05:29.870
by combining all the variable
types that we have seen so far.

05:30.700 --> 05:34.650
For example, let us consider
a new variable called "bella",

05:34.730 --> 05:36.160
which is the name of a cat.

05:37.000 --> 05:40.230
This variable is used to define
the different features of this cat,

05:40.310 --> 05:43.960
such as its name which
is a string; the color,

05:44.040 --> 05:48.130
which is a string as well;
age, which is a number;

05:48.210 --> 05:50.810
the food that it eats,
which is a list of strings;

05:51.480 --> 05:54.830
and a Boolean value indicating
if it's a favorite pet or not.

05:55.550 --> 05:58.380
Let us now assign some
values to this variable.

05:58.460 --> 06:03.120
Let us use name is equal to
bella, color is equal to brown,

06:03.630 --> 06:07.920
age is equal to 7, food is
"fish", "chicken", and "turkey",

06:08.700 --> 06:10.540
and favorite_pet,
which is set to true.

06:12.200 --> 06:13.990
We can use
the same default values

06:14.070 --> 06:16.480
within our variable block like this.

06:18.240 --> 06:21.710
The last variable type that we
are going to look at is tuples.

06:22.680 --> 06:26.590
Tuple is similar to a list and
consists of a sequence of elements.

06:27.420 --> 06:31.040
The difference between a tuple
and a list is that list uses

06:31.120 --> 06:34.760
elements of the same variable
type such as string or number

06:35.290 --> 06:38.220
but in case of tuple, we
can make use of elements

06:38.300 --> 06:39.880
of different variable types.

06:40.630 --> 06:43.520
The type of variables to be used
in a tuple is defined

06:43.600 --> 06:45.080
within the square brackets.

06:45.670 --> 06:49.050
In this example, we have three
types of elements defined.

06:49.470 --> 06:54.250
The first is a string, second is
a number, and finally a Boolean.

06:55.310 --> 06:58.140
The variables to be passed to
this should exactly be three

06:58.220 --> 07:01.680
in number and of that specific
type for it to work.

07:02.320 --> 07:05.640
Here, we have passed the value
of cat to the string element,

07:05.960 --> 07:08.080
the number 7 to
the number element,

07:08.390 --> 07:09.820
and true to the Boolean.

07:10.600 --> 07:13.390
Adding additional elements or
incorrect type will result

07:13.470 --> 07:14.880
in an error as seen here.

07:15.800 --> 07:18.990
If you add an additional
value of dog to the variable,

07:19.070 --> 07:23.290
it will fail as the tuple only
expects three elements of type string,

07:23.370 --> 07:24.600
number, and Boolean.

07:25.840 --> 07:27.100
That's it for this lecture.

07:27.480 --> 07:30.510
Let's head over to the hands-on
labs and explore working

07:30.590 --> 07:32.380
with variable types in Terraform.

