In this lecture, we will take a look at the different ways
in which we can make use of input variables in Terraform.
So far, we have created input variables in Terraform,
and assigned default values to it based on the variable type.
This is just one of the ways to pass in values to the variable.
Earlier, we learned that the default parameter in a variable block is optional.
This means that we can very well have our variable block look like this.
What would happen if we run Terraform commands now?
When we run Terraform apply,
we will be prompted to enter values for each variable used in an interactive mode.
If we do not want to supply values in an interactive mode,
we can also make use of command line flags like this.
With the Terraform command, we can make use of the -var option
with the variable name equals to the value format.
We can pass in as many variables as we want with this method
by making use of the -var flag multiple times.
We can also make use of environment variables
with the TF_VAR_ followed by the name of a declared variable like this.
In this example, the TF_VAR_ filename
sets the value of the variable called "filename" to the value "/root/pets.txt".
Similarly, the variable called length now has the value of 2.
Finally, when we are dealing with a lot of variables,
we can load values by making use of variable definition files like this.
These variable definition files can be named anything
but should always end in either .tfvars or .tfvars.json.
Here, we have declared variables
and their values in a file called terraform.tfvars.
If you look at the syntax used to create this file,
you'll observe that this is using the same syntax of an HCL file,
but it only consists of variable assignments.
The variable definition file is called terraform.tfvars or terraform.tfvars.json
or by any other name ending with .auto.tfvars or .auto.tfvars.json
will be automatically loaded by Terraform.
If you use any other filename such as variable.tfvars, for example,
you will have to parse it along with a command line flag called -var-file like this.
Finally, it is important to note that we can use any of the options
that we have seen in this lecture to assign values to the variables.
If we use multiple ways to assign values for the same variable,
Terraform follows the variable definition precedence
to understand which value it should accept.
To illustrate this, let us make use of a simple example.
In this case, we have a main configuration file with a single resource.
A local file which will create file at a path declared in a variable called filename.
In the variables.tf file, we have not specified a default value for this variable,
and we have assigned different values to this variable in multiple ways.
We have exported the environment variable called TF_VAR_ filename
with the value of /root/cats.txt.
The terraform.tfvar file has a value of /root/pets.txt for the same variable.
We have also made use of a variable definition file
with the name variable.auto.tfvars with the value of /root/mypet.txt,
and finally, we are also making use of the -var option
while running the Terraform apply command with a value of /root/best-pet.txt.
In this case, which one of these values would be accepted?
Terraform follows a variable definition precedence order to determine this.
First, it loads the environment variables.
Next, the value in the terraform.tfvars file.
This is followed by any file that ends with the .auto.tfvars
or .auto.tfvars.json in an alphabetical order.
Finally, Terraform considers the command line flag of -var or -var-file,
which takes the highest priority and will overwrite any of the previous values.
In this case, the variable filename will be assigned the value of /root/best-pet.txt.
That's it for this lecture.
Let's head over to the hands-on lab
and practice working with the concepts that we learned in this lecture.