Terraform Variables: Simplifying Infrastructure Configuration and Reusability

terraform variables explained

In the world of Infrastructure as Code (IaC), Terraform has gained widespread popularity for its ability to manage cloud resources efficiently. When it comes to creating reusable and flexible infrastructure configurations, Terraform variables play a crucial role. In this blog article, we will explore what Terraform variables are, why they are important, and how to use them effectively to simplify your infrastructure configuration.

What are Terraform Variables?

Terraform variables are placeholders that allow you to parameterize your infrastructure configurations. They act as input parameters that can be dynamically set when you run Terraform commands. Variables enable you to make your configurations more flexible, reusable, and easier to manage across different environments.

Why are Terraform Variables Important?

  • Flexibility: With variables, you can easily customize your infrastructure configurations without modifying the underlying code. This flexibility allows you to adapt to different environments or use cases effortlessly.
  • Reusability: Variables promote reusability of your Terraform modules and configurations. You can define a module once and use it in multiple projects with different variable values.
  • Separation of Concerns: By using variables, you separate the values that change between deployments from the infrastructure configuration itself. This enhances the maintainability of your codebase.
  • Sensitive Data Handling: Variables can be marked as sensitive, allowing you to handle sensitive data (like API keys or passwords) securely. Sensitive variables are not displayed in the Terraform output or stored in the Terraform state file.

How to Use Terraform Variables

1. Variable Declaration

To declare variables in Terraform, create a .tf file (e.g., variables.tf) in your project directory and define your variables using the variable block. For example:

In this example, we define two variables: aws_region and instance_type, each with a description, type, and default value.

2. Variable Values

To set values for your variables, you have several options:

Command-Line Flags: You can use the -var flag when running Terraform commands. For example:

terraform variables values

Environment Variables: You can use environment variables with the TF_VAR_ prefix. For example:

environment variables

Variable Files: You can create a .tfvars file (e.g., terraform.tfvars) and set variable values in it. Terraform will automatically load values from this file. For example:

variable files

3. Using Variables in Configurations

To reference variables in your Terraform configurations, use the syntax ${var.VARIABLE_NAME}. For example:

terraform variables configuration

In this example, we use the variables var.aws_region and var.instance_type in the provider and aws_instance blocks, respectively.

4. Validation and Constraints

You can add validation rules and constraints to your variables. For instance, you can define allowed values, ranges, or regex patterns to ensure that variable values meet specific criteria.

Visualize your Terraform attributes with Holori

Getting visibility over your whole infra, its resources and the associated variables can be challenging when your infra grows in size and complexity.

At Holori, we advocate for the use of infrastructure diagrams to ensure full visibility over an infra.

terraform variables in Holori software

There are two ways to visualize your Terraform attributes:

  • Import your Terraform files directly in Holori software and have your infra mapped as a Terraform graph automatically.
  • Link Holori to your preferred CICD such as GitHub, Bitbucket, Gitlab, Holori will then monitor for new pull requests containing an infra modification and directly provide the corresponding Terraform graph.

Either way, once your infra is displayed in Holori software, for any resource there is a direct access to its Terraform attributes. Rather than going through script lines, you can easily see which resources belong to which group, region, VPC…, and identify dependencies. By selecting a resource, it’s easy to visualize all of its Terraform variables, and settings such as IP, constraints, rules etc. By using Holori, it becomes much easier to use Terraform, visualize your infra and document it.

You can sign up here to try the tool : https://app.holori.com/register

Wrap up

Terraform variables are a powerful mechanism for parameterizing your infrastructure configurations, making them more flexible, reusable, and easy to manage. By mastering Terraform variables, you can create highly dynamic and adaptable infrastructure as code solutions that scale effortlessly across different environments and use cases. By combining Terraform and Holori, embrace the flexibility and maintainability that Terraform variables offer, and elevate your infrastructure management to new heights. Happy Terraforming!

Share This Post

Subscribe To Our Newsletter

Get updates and learn from the best

More To Explore

GPU available within GCP

List of GPU available in the different GCP Zones

As artificial intelligence (AI) and machine learning (ML) continue to advance, the demand for powerful computing resources grows. Graphics Processing Units (GPUs) have become essential


List of all GCP regions and zones

As of June 2024 GCP has more than 40 regions available and 121 Zones worldwide. Typically there are 3 zones for each regions and 1