In this lecture, we will see how to make use of variables
in Terraform.
We have used several arguments in our Terraform blocks so far.
For the local file, we have a filename and content.
For the random pet resource, we have used the prefix, separator,
and length as the arguments.
Since these values are directly defined
within the main configuration files,
they are considered to be hardcoded values.
Hardcoding values is not a good idea.
For one, this limits the reusability of the code,
which defeats the purpose of using IAC.
We want to make sure that the same code can be used again and again
to deploy resources based on a set of input variables
that can be provided during the execution.
That is where input variables come in to the picture.
Just as in any general-purpose programming language,
such as Bash Scripting or PowerShell,
we can make use of input variables in Terraform.
To assign variables, let us create a new configuration file
called variables.tf and define the values like this.
The variables.tf file, just like the main.tf file,
consists of blocks and arguments.
To create a variable, use the keyword called variable.
This is followed by the variable name.
This can be named anything but as a standard,
use an appropriate name, such as the argument name
for which we are using the variable.
Within this block, we can provide a default value for the variable.
This is an optional parameter, but it is a quick and simple way
to assign values to the variables.
We will see the other methods to do so in a later lecture.
Great. Now we have our variable configuration file,
but how do we use it within the main.tf file?
To do this, we can replace the argument values
with the variable names prefixed with a var like this.
When using variables, you do not have to enclose the values
inside double quotes as you would when providing actual values.
Using the same execution flow that we have seen many times by now,
we can create the resources using Terraform
plan followed by the Terraform apply.
The resources have now been created as expected.
Now, if you want to make an update to the resources
by making changes to the existing arguments,
we can do that by just updating the variables.tf file.
The main.tf may not be modified.
For example, let us update the local file resource
to create the file at the same location but with an updated content
that reads, "My favorite pet is Mrs. hiskers",
and for the random pet resource, let's change the length
of the pet name to two.
We can do that like this.
As expected, when we run Terraform apply,
it will recreate the resources.
The content of the file has been changed and the pet name now
has two words following the prefix.
Before we conclude this lecture,
here is an example of what our configuration files would look like
when creating an EC2 instance in AWS with Terraform
while making use of input variables.
Don't worry if the resource block and the arguments are unfamiliar.
We have a separate lecture
where we will be making use of AWS resources later in the course.