As organizations scale their infrastructure, managing dynamic configurations becomes crucial. Terraform, the Infrastructure as Code (IaC) tool, simplifies the process of provisioning and managing cloud resources. One of Terraform’s core features is its use of variables, enabling dynamic infrastructure configurations that enhance reusability and flexibility.

In this blog, we’ll explore Terraform variables in depth, discussing how to declare and use them, different types and default values, input and output variables, and advanced techniques with .tfvars files and workspaces.

Introduction to Terraform Variables

Terraform variables allow us to define dynamic input values without hardcoding them within the configuration. This flexibility enables you to reuse modules, customize infrastructure setups for different environments, and streamline deployments.

Terraform variables can be utilized across modules, resources, and configurations, ensuring that infrastructure is adaptable and maintainable.

Declaring and Using Variables in Terraform

Declaring a variable in Terraform is straightforward. Variables are declared in a .tf file using the variable block. The most basic declaration might look like this:

variable “instance_type” {

  description = “Type of instance to be created”

  type        = string

  default     = “t2.micro”

}

To use this variable, simply reference it using the var keyword:

resource “aws_instance” “example” {

  instance_type = var.instance_type

  ami           = “ami-12345678”

}

In this example, the instance type is dynamically set using the instance_type variable.

Types and Default Values for Terraform Variables

Terraform supports various kinds of variables, including:

  • string: Represents a text value
  • number: Represents numeric values
  • bool: Represents Boolean values (true or false)
  • list: A collection of values in a specific order
  • map: A key-value pair collection

When declaring a variable, you can assign a default value, which will be used if no other value is provided:

variable “environment” {

  description = “Deployment environment”

  type        = string

  default     = “development”

}

If no default is defined and no value is provided during execution, Terraform will prompt for input when running terraform apply.

Utilizing Input Variables in Terraform

Input variables allow users to pass values into Terraform configurations. These values can be provided in several ways:

Directly in the command line, using the -var flag:
terraform apply -var=”instance_type=t3.medium”

In a .tfvars file, which we’ll cover in more detail later.

Input variables enable configuration changes without modifying the underlying code, making your deployments more flexible and scalable across environments.

Generating Output Variables in Terraform

Output variables are useful when you need to return information from your Terraform configuration, such as resource IDs or IP addresses of provisioned resources. They are declared using the output block:

output “instance_id” {

  description = “ID of the EC2 instance”

  value       = aws_instance.example.id

}

Once applied, Terraform will print the value of the output variable:

Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

Outputs:

instance_id = “i-1234567890abcdef0”

These outputs can be referenced in other modules or external systems for further automation.

Advanced Variable Management with .tfvars and Workspaces

Using .tfvars Files

A .tfvars file defines variable values in bulk, enabling environment-specific configurations. Instead of specifying variables individually, create a terraform.tfvars file:

instance_type = “t3.medium”

environment   = “production”

Terraform automatically loads the .tfvars file during execution. For environment-specific files, you can name them accordingly (e.g., production.tfvars, development.tfvars) and load them explicitly:

terraform apply -var-file=”production.tfvars”

Leveraging Terraform Workspaces

Workspaces provide a more advanced way to manage variables across multiple environments within a single configuration. By default, Terraform operates in the “default” workspace. You can create new workspaces to handle different configurations without duplicating files:

terraform workspace new production

Each workspace maintains separate state files, allowing isolated management of different environments (e.g., development, staging, production).

Conclusion

Understanding and mastering Terraform variables is critical to managing dynamic infrastructure efficiently. You can ensure your infrastructure is scalable, reusable, and maintainable by leveraging input, output, and advanced variable techniques with .tfvars files and workspaces.

References

Best practices for using the Terraform AWS Provider

Terraform: Beyond the Basics with AWS