WEBVTT

00:02.270 --> 00:06.060
In this lecture, we will see
how to make use of variables

00:06.140 --> 00:07.210
in Terraform.

00:08.440 --> 00:11.740
We have used several arguments
in our Terraform blocks so far.

00:12.580 --> 00:16.010
For the local file, we have
a filename and content.

00:16.820 --> 00:20.940
For the random pet resource, we
have used the prefix, separator,

00:21.020 --> 00:22.700
and length as the arguments.

00:23.990 --> 00:25.680
Since these values
are directly defined

00:25.760 --> 00:27.950
within the main configuration files,

00:28.030 --> 00:30.540
they are considered
to be hardcoded values.

00:31.630 --> 00:34.060
Hardcoding values
is not a good idea.

00:34.970 --> 00:38.140
For one, this limits the
reusability of the code,

00:38.800 --> 00:41.370
which defeats the
purpose of using IAC.

00:42.670 --> 00:46.470
We want to make sure that the
same code can be used again and again

00:46.550 --> 00:49.800
to deploy resources based
on a set of input variables

00:49.880 --> 00:52.560
that can be provided
during the execution.

00:53.850 --> 00:56.460
That is where input variables
come in to the picture.

00:57.690 --> 01:00.810
Just as in any general-purpose
programming language,

01:00.890 --> 01:03.470
such as Bash Scripting
or PowerShell,

01:04.160 --> 01:06.810
we can make use of input
variables in Terraform.

01:07.770 --> 01:11.520
To assign variables, let us
create a new configuration file

01:11.770 --> 01:15.930
called variables.tf and
define the values like this.

01:17.480 --> 01:21.280
The variables.tf file,
just like the main.tf file,

01:21.450 --> 01:23.610
consists of blocks
and arguments.

01:24.620 --> 01:28.280
To create a variable, use
the keyword called variable.

01:29.250 --> 01:31.320
This is followed by
the variable name.

01:32.420 --> 01:35.580
This can be named anything
but as a standard,

01:35.660 --> 01:38.550
use an appropriate name,
such as the argument name

01:38.630 --> 01:40.190
for which we are using the variable.

01:40.920 --> 01:44.640
Within this block, we can provide
a default value for the variable.

01:45.600 --> 01:49.630
This is an optional parameter,
but it is a quick and simple way

01:49.710 --> 01:51.500
to assign values
to the variables.

01:52.340 --> 01:55.690
We will see the other methods
to do so in a later lecture.

01:56.680 --> 02:00.220
Great. Now we have our
variable configuration file,

02:00.750 --> 02:04.000
but how do we use it
within the main.tf file?

02:05.520 --> 02:08.580
To do this, we can replace
the argument values

02:08.660 --> 02:12.620
with the variable names
prefixed with a var like this.

02:13.670 --> 02:17.220
When using variables, you do
not have to enclose the values

02:17.300 --> 02:20.990
inside double quotes as you
would when providing actual values.

02:22.770 --> 02:26.560
Using the same execution flow
that we have seen many times by now,

02:27.380 --> 02:29.400
we can create the
resources using Terraform

02:29.480 --> 02:31.850
plan followed by the Terraform apply.

02:32.720 --> 02:35.640
The resources have now
been created as expected.

02:37.090 --> 02:40.610
Now, if you want to make an
update to the resources

02:40.690 --> 02:43.160
by making changes to the
existing arguments,

02:43.530 --> 02:46.910
we can do that by just
updating the variables.tf file.

02:47.730 --> 02:50.030
The main.tf may not be modified.

02:51.200 --> 02:54.590
For example, let us update
the local file resource

02:54.670 --> 02:58.760
to create the file at the same
location but with an updated content

02:58.840 --> 03:02.480
that reads, "My favorite
pet is Mrs. hiskers",

03:03.950 --> 03:06.440
and for the random pet resource,
let's change the length

03:06.520 --> 03:08.110
of the pet name to two.

03:09.220 --> 03:10.970
We can do that like this.

03:12.490 --> 03:15.320
As expected, when we
run Terraform apply,

03:15.630 --> 03:17.240
it will recreate the resources.

03:18.060 --> 03:21.290
The content of the file has been
changed and the pet name now

03:21.370 --> 03:23.320
has two words following the prefix.

03:25.000 --> 03:26.570
Before we conclude this lecture,

03:27.410 --> 03:31.030
here is an example of what our
configuration files would look like

03:31.110 --> 03:34.530
when creating an EC2
instance in AWS with Terraform

03:34.610 --> 03:36.410
while making use
of input variables.

03:37.380 --> 03:40.620
Don't worry if the resource block
and the arguments are unfamiliar.

03:41.150 --> 03:42.440
We have a separate lecture

03:42.540 --> 03:46.240
where we will be making use of
AWS resources later in the course.

