Why Use Variables in Terraform?
Hardcoding values across multiple files makes updates risky and inefficient. Variables eliminate this problem.
You define a value once, reference it wherever needed, and update it centrally. This keeps your infrastructure code consistent and easier to manage.
Three Types of Terraform Variables
1. Input Variables (variables.tf)
Input variables enable customization of Terraform configurations. They behave like parameters in a function—values can be passed from CLI, tfvars files, or environment variables.
Example:
variable "environment" {
description = "Environment name"
type = string
default = "staging"
}
These variables are later referenced using the var. prefix.
2. Local Variables (locals.tf)
Local variables store computed or reusable values within Terraform. They help avoid repetition and simplify expressions.
Example:
locals {
common_tags = {
Environment = var.environment
Project = "Terraform-Demo"
}
full_bucket_name = "${var.environment}-${var.bucket_name}-${random_string.suffix.result}"
}
These values are internal to Terraform and cannot be passed from outside.
3. Output Variables (output.tf)
Output variables return values after resources are created. They behave like function return values and are useful during automation pipelines or when passing data between Terraform modules.
Example:
output "bucket_name" {
description = "Name of the S3 bucket"
value = aws_s3_bucket.demo.bucket
}
The values appear only after running terraform apply.
Input Variables in Detail
Structure of an Input Variable
variable "variable_name" {
description = "Purpose of the variable"
type = string
default = "default_value" # Optional
}
Defining Input Variables
variables.tf
variable "environment" {
description = "Environment name"
type = string
default = "staging"
}
variable "bucket_name" {
description = "S3 bucket name"
type = string
default = "my-terraform-bucket"
}
Using Input Variables
main.tf
resource "aws_s3_bucket" "demo" {
bucket = var.bucket_name
tags = {
Environment = var.environment
}
}
How to Provide Values for Input Variables
1. Default Values
If no other values are supplied, Terraform uses defaults defined in variables.tf.
2. terraform.tfvars
Automatically loaded when present.
environment = "demo"
bucket_name = "terraform-demo-bucket"
3. Command Line
terraform plan -var="environment=production"
4. Environment Variables
export TF_VAR_environment="development"
terraform plan
Output Variables in Detail
Structure
output "output_name" {
description = "Details"
value = resource.resource_name.attribute
}
Useful Outputs
output "bucket_name" {
value = aws_s3_bucket.demo.bucket
}
output "bucket_arn" {
value = aws_s3_bucket.demo.arn
}
output "environment" {
value = var.environment
}
output "tags" {
value = local.common_tags
}
Viewing Outputs
terraform output
terraform output bucket_name
terraform output -json
What This Configuration Builds
This example deploys a single S3 bucket and demonstrates:
- Input variables controlling bucket name and environment
- Local variables computing final names and tags
- Output variables exposing bucket name, ARN, and environment after creation
Variable Precedence (Lowest → Highest)
- Default values in variables.tf
- Environment variables (TF_VAR_)
- terraform.tfvars
- terraform.tfvars.json
- CLI flags:
-varor-var-file
This ensures complete control over variable inputs depending on your workflow.
Testing Variable Precedence
Hide terraform.tfvars and test defaults
mv terraform.tfvars terraform.tfvars.backup
terraform plan
Restore tfvars
mv terraform.tfvars.backup terraform.tfvars
terraform plan
Command line override
terraform plan -var="environment=production"
Environment variable
export TF_VAR_environment="stage"
terraform plan
Specific tfvars file
terraform plan -var-file="dev.tfvars"
Recommended File Structure
├── main.tf
├── variables.tf
├── locals.tf
├── output.tf
├── provider.tf
├── terraform.tfvars
└── README.md
Practical Examples
Example 1: Testing Input Values
mv terraform.tfvars terraform.tfvars.backup
terraform plan
mv terraform.tfvars.backup terraform.tfvars
terraform plan
terraform plan -var="environment=test" -var="bucket_name=my-test-bucket"
Example 2: Viewing All Variable Types
terraform apply -auto-approve
terraform output
echo "Environment: $(terraform output -raw environment)"
echo "Full bucket name: $(terraform output -raw bucket_name)"
Example 3: Precedence in Action
terraform plan | grep Environment
export TF_VAR_environment="from-env-var"
terraform plan | grep Environment
terraform plan -var="environment=from-command-line" | grep Environment
Useful Commands
terraform init
terraform plan
terraform plan -var="environment=test"
terraform plan -var-file="dev.tfvars"
terraform apply
terraform output
terraform destroy
Key Takeaways
- Input variables parameterize configurations and increase flexibility.
- Local variables compute reusable values inside Terraform.
- Output variables expose important resource information after deployment.
- Precedence rules ensure predictable overrides during automation and testing.
Reference Video
This video demonstrates the same concept visually and guided this learning:
https://youtu.be/V-2yC39BONc?si=DgCJjBKUKvqFjFq7
Top comments (0)