Variables
So far we have not created many configuration elements. In the previous exercise, you created a DNS record with a configuration similar to below:
Notice how the zone_id
is a hardcoded value - this can become tedious and untidy with a lot of DNS records, not to mention the many other Terraform resources which rely on the Zone ID. And what if you wanted to reuse the configuration for a different zone? You'd need to manually update each occurence.
Task
In this section, you will convert the configuration to use variables instead of hardcoded values.
Why
In Terraform, variables allow you to define dynamic values that can be used across your configuration files. Instead of hardcoding values, you can reference variables, making your infrastructure code reusable and adaptable for different environments (development, production, etc.).
Types of Variables in Terraform:
- Input Variables: Used to pass values into Terraform configurations, defined using the
variable
keyword. - Output Variables: Used to output information, like IP addresses or other attributes, after the Terraform configuration is applied.
Benefits of Using Variables in Terraform:
- Reusability: Variables make your code reusable across multiple environments by allowing different configurations (e.g., region, instance type) without duplicating code.
- Maintainability: Storing variables separately keeps your main configuration files clean, easier to read, and maintain.
- Security: Sensitive information (e.g., API keys, passwords) can be stored in variables, reducing the risk of hardcoding secrets in the main files.
- Environment Flexibility: Variables can adapt configurations for different environments or contexts without changing core logic.
Defining Variables:
Variables are typically declared in variables.tf
(but this is more a naming convention as really they can be inside any .tf
file) with the following structure:
Once declared, variables can be referenced using var.<variable_name>
Another file terraform.tfvars
is used to actually assign the values, in a list-like format.
Steps
Now that we've covered the basics, you will change the configuration you have already created to leverage variables.
1) Create terraform.tfvars
Create a new file called terraform.tfvars
and assign a value to cloudflare_zone_id:
eg:
2) Update DNS Configuration
Update dns.tf to reference the variable, instead of the hardcoded value. To ensure there's something new, also add a second record:
3) Run a Plan
Now run terraform plan
. Oops! There's an error:
As noted, we've referenced an undeclared variable. Remember, you need both the terraform.tfvars
AND variables.tf
files. This is an important lesson to remember.
Differences Between variables.tf
and terraform.tfvars
- Purpose:
variables.tf
: This file is used to define variables and their default values. You use it to declare what inputs your configuration needs.terraform.tfvars
: This file is used to assign specific values to those variables. It overrides the default values for variables when you runterraform apply
orterraform plan
.
- Content:
variables.tf
: Contains the variable definitions along with optional descriptions, types, and default values.terraform.tfvars
: Contains actual values for those variables that are supplied during the Terraform run.
- Use Case:
variables.tf
: Helps in defining reusable configurations across different environments.terraform.tfvars
: Helps in assigning environment-specific or deployment-specific values (e.g., different instance types or regions for production and development environments).
Now let's fix it.
4) Create variables.tf
Create a new file called variables.tf
and add Zone ID as a variable, using the below format:
We're not assigning the value here, as we already did that (and values do not live in this file)
5) Plan Again
The plan should now work without any errors and show us a new record is to be added. Note from the output how the Zone ID Value is visible:
6) Apply the Change
Go ahead and apply the change so the new DNS record is created. Feel free to check it is visible from the Dashboard to prove the variable worked!
Summary
In this exercise, you have learned the basics of variables in Terraform along with the use cases and examples. You updated your existing configuration to use variables instead of hardcoded values, and learned the importance of using the two different files to avoid errors.
Please proceed to the next task.