WEBVTT

00:01.120 --> 00:04.320
In this lecture, we will take a look
at the different ways

00:04.320 --> 00:08.000
in which we can make use of
input variables in Terraform.

00:09.100 --> 00:12.740
So far, we have created
input variables in Terraform,

00:12.740 --> 00:16.860
and assigned default values to it
based on the variable type.

00:17.400 --> 00:20.940
This is just one of the ways to pass
in values to the variable.

00:21.620 --> 00:26.940
Earlier, we learned that the default parameter
in a variable block is optional.

00:27.780 --> 00:31.960
This means that we can very well have
our variable block look like this.

00:33.360 --> 00:36.420
What would happen if we run
Terraform commands now?

00:36.740 --> 00:38.560
When we run Terraform apply,

00:38.920 --> 00:43.940
we will be prompted to enter values for
each variable used in an interactive mode.

00:44.600 --> 00:47.780
If we do not want to supply values
in an interactive mode,

00:47.780 --> 00:51.600
we can also make use of
command line flags like this.

00:52.600 --> 00:56.980
With the Terraform command,
we can make use of the -var option

00:56.980 --> 01:00.040
with the variable name equals
to the value format.

01:00.680 --> 01:04.340
We can pass in as many variables
as we want with this method

01:04.340 --> 01:07.480
by making use of the -var flag multiple times.

01:08.200 --> 01:10.940
We can also make use of
environment variables

01:10.940 --> 01:17.520
with the TF_VAR_ followed by
the name of a declared variable like this.

01:18.440 --> 01:22.660
In this example, the TF_VAR_ filename

01:22.660 --> 01:29.180
sets the value of the variable called "filename"
to the value "/root/pets.txt".

01:29.700 --> 01:34.120
Similarly, the variable called length
now has the value of 2.

01:34.860 --> 01:38.240
Finally, when we are dealing
with a lot of variables,

01:38.240 --> 01:43.000
we can load values by making use of
variable definition files like this.

01:43.720 --> 01:46.660
These variable definition files
can be named anything

01:46.800 --> 01:52.020
but should always end
in either .tfvars or .tfvars.json.

01:52.780 --> 01:54.800
Here, we have declared variables

01:54.800 --> 01:58.540
and their values in a file
called terraform.tfvars.

01:59.320 --> 02:01.880
If you look at the syntax
used to create this file,

02:02.140 --> 02:05.700
you'll observe that this is using
the same syntax of an HCL file,

02:05.820 --> 02:08.500
but it only consists of variable assignments.

02:09.500 --> 02:15.900
The variable definition file is called
terraform.tfvars or terraform.tfvars.json

02:15.900 --> 02:22.700
or by any other name ending with
.auto.tfvars or .auto.tfvars.json

02:22.700 --> 02:25.300
will be automatically loaded by Terraform.

02:26.120 --> 02:31.480
If you use any other filename
such as variable.tfvars, for example,

02:31.780 --> 02:37.600
you will have to parse it along with
a command line flag called -var-file like this.

02:38.860 --> 02:43.100
Finally, it is important to note that
we can use any of the options

02:43.100 --> 02:46.420
that we have seen in this lecture
to assign values to the variables.

02:47.080 --> 02:51.420
If we use multiple ways to assign values
for the same variable,

02:51.640 --> 02:54.240
Terraform follows the variable
definition precedence

02:54.240 --> 02:56.480
to understand which value it should accept.

02:57.180 --> 03:00.320
To illustrate this, let us make use
of a simple example.

03:00.960 --> 03:04.960
In this case, we have a main configuration file
with a single resource.

03:05.300 --> 03:11.040
A local file which will create file at a path
declared in a variable called filename.

03:11.780 --> 03:17.220
In the variables.tf file, we have not
specified a default value for this variable,

03:18.240 --> 03:22.060
and we have assigned different values
to this variable in multiple ways.

03:22.920 --> 03:27.900
We have exported the environment
variable called TF_VAR_ filename

03:27.900 --> 03:31.980
with the value of /root/cats.txt.

03:32.940 --> 03:39.960
The terraform.tfvar file has a value
of /root/pets.txt for the same variable.

03:40.760 --> 03:43.700
We have also made use of
a variable definition file

03:43.700 --> 03:51.000
with the name variable.auto.tfvars
with the value of /root/mypet.txt,

03:51.120 --> 03:55.460
and finally, we are also making use
of the -var option

03:55.460 --> 04:00.920
while running the Terraform apply command
with a value of /root/best-pet.txt.

04:01.300 --> 04:05.160
In this case, which one of these values
would be accepted?

04:05.800 --> 04:09.820
Terraform follows a variable definition
precedence order to determine this.

04:10.640 --> 04:13.600
First, it loads the environment variables.

04:14.440 --> 04:18.280
Next, the value in the terraform.tfvars file.

04:19.360 --> 04:24.080
This is followed by any file
that ends with the .auto.tfvars

04:24.080 --> 04:28.600
or .auto.tfvars.json in an alphabetical order.

04:29.880 --> 04:36.040
Finally, Terraform considers
the command line flag of -var or -var-file,

04:36.040 --> 04:40.920
which takes the highest priority
and will overwrite any of the previous values.

04:41.580 --> 04:48.540
In this case, the variable filename will be
assigned the value of /root/best-pet.txt.

04:49.600 --> 04:51.000
That's it for this lecture.

04:51.080 --> 04:53.320
Let's head over to the hands-on lab

04:53.320 --> 04:56.840
and practice working with the concepts
that we learned in this lecture.

