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:

  1. Input Variables: Used to pass values into Terraform configurations, defined using the variable keyword.
  2. Output Variables: Used to output information, like IP addresses or other attributes, after the Terraform configuration is applied.

Benefits of Using Variables in Terraform:

  1. Reusability: Variables make your code reusable across multiple environments by allowing different configurations (e.g., region, instance type) without duplicating code.
  2. Maintainability: Storing variables separately keeps your main configuration files clean, easier to read, and maintain.
  3. Security: Sensitive information (e.g., API keys, passwords) can be stored in variables, reducing the risk of hardcoding secrets in the main files.
  4. 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:

variable "cloudflare_zone_id" {
  description = "The zone ID for the Cloudflare domain"
  type        = string
}

variables.tf

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:

cloudflare_zone_id = "<your zone ID>"

terraform.tfvars

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:

# Add a record to the domain
resource "cloudflare_record" "terraform1" {
  zone_id = var.cloudflare_zone_id
  name    = "terraform"
  content = "20.88.188.200"
  type    = "A"
  proxied = true
}

resource "cloudflare_record" "variable-test" {
  zone_id = var.cloudflare_zone_id
  name    = "variable-test"
  content = "20.88.188.200"
  type    = "A"
  proxied = true
}

terraform.tfvars

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

  1. 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 run terraform apply or terraform plan.
  2. 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.
  3. 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:

variable "cloudflare_zone_id" {
  description = "The zone ID for the Cloudflare domain"
  type        = string
}

variables.tf

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.