Using Input Variables in Terraform is a key technique to make your Infrastructure as Code (IaC) flexible and reusable. Whether you’re creating resources on a local machine or provisioning cloud infrastructure, variables allow you to separate configuration from implementation, making your Terraform code cleaner and easier to maintain.
Table of Contents
Why Input Variables Matter in Terraform
Hardcoding values directly into Terraform configuration files limits the portability and flexibility of your infrastructure code. This approach works for quick experiments but becomes problematic in larger, production-grade environments.
Instead, using input variables in Terraform helps you:
- Reuse code across multiple environments (dev, staging, prod).
- Manage changes efficiently with minimal updates.
- Improve collaboration by separating values from logic.
Defining Variables in Terraform
To begin using input variables in Terraform, create a separate file—usually named variables.tf
. This file declares variables that your main Terraform files can reference.
variable "filename" {
description = "Name of the file to create"
default = "/root/pets.txt"
}
variable "file_content" {
description = "Text content inside the file"
default = "We love pets."
}
Here, each variable block begins with the variable
keyword, followed by a unique name. You can add optional attributes like description
, type
, and default
.
Referencing Variables in Configuration
Once variables are defined, you can reference them in your main configuration file (typically main.tf
) using the var.<variable_name>
syntax.
resource "local_file" "pet_file" {
filename = var.filename
content = var.file_content
}
This makes your configuration dynamic and allows different values to be supplied at runtime—via CLI, files, or environment variables.
Different Ways to Assign Variable Values
Terraform provides multiple methods for supplying values to variables:
1. Default Values
These are defined within the variables.tf
file. If no other input is provided, Terraform uses these defaults.
2. Command-Line Input
You can provide values using the -var
flag:
terraform apply -var="filename=/tmp/output.txt"
3. Variable Definition Files
Create a .tfvars
file and supply it using -var-file
:
filename = "/tmp/output.txt"
file_content = "Updated content here."
terraform apply -var-file="custom.tfvars"
This is useful for managing multiple environments.
Updating Resources with Variables
When you change a variable’s value, Terraform detects the difference during the planning phase and shows the required changes. This allows you to update your infrastructure predictably.
For example, changing the file_content
variable from “We love pets.” to “My favorite pet is Mrs. Whiskers.” results in Terraform planning to replace or update the resource accordingly.
Managing Multiple Resources with Variables
Variables are particularly useful when working with several resources that share common values. Instead of repeating yourself, define those values once and reuse them wherever needed.
variable "prefix" {
default = "pet"
}
resource "random_pet" "my_pet" {
prefix = var.prefix
separator = "-"
length = 2
}
This makes your code modular, DRY (Don’t Repeat Yourself), and easier to scale.
Real-World Example: AWS EC2 with Input Variables
While we’ve focused on local resources, variables play a crucial role in cloud deployments too. For example, deploying an EC2 instance with variable-based AMI and instance type:
variable "ami_id" {
description = "AMI ID for the EC2 instance"
default = "ami-0c55b159cbfafe1f0"
}
variable "instance_type" {
description = "EC2 instance type"
default = "t2.micro"
}
resource "aws_instance" "web_server" {
ami = var.ami_id
instance_type = var.instance_type
}
You can then deploy infrastructure to different environments by just tweaking the .tfvars
file—without touching the actual resource logic.
Conclusion
Using input variables in Terraform is essential for creating scalable, repeatable, and modular infrastructure. It separates configuration from logic, improves maintainability, and streamlines workflows across teams and environments.
By mastering variables, you’re taking one big step toward writing professional-grade Terraform code that aligns with DevOps best practices.
Frequently Asked Questions (FAQs)
1. What is the use of input variables in Terraform?
Input variables let you parameterize Terraform configurations, making your code reusable and more flexible.
2. How do I pass variables in Terraform?
You can pass variables using default values, CLI -var
flag, environment variables, or .tfvars
files.
3. Can I override default variable values?
Yes, values defined at the command line or in .tfvars
files override the default values declared in the variables.tf
file.
4. Are input variables required in Terraform?
No, they are optional. But using them is a best practice for managing dynamic infrastructure at scale.
5. What’s the difference between input and output variables?
Input variables are used to pass data into Terraform configurations, while output variables expose data from configurations.