Variables are one of the most important concepts in Terraform.
Day 5 of the 30 Days AWS Terraform challenge focuses entirely on understanding what variables are, why we need them, and how they make Terraform configurations reusable, maintainable, and safe.
This lesson finally answers why hardcoding must be avoided and shows how variables help control environments like dev, stage, and production without rewriting code.
๐ฑ Why Do We Need Variables?
Hardcoding values such as:
AWS region
VPC CIDR
Tags
Bucket names
AMI IDs
โฆcauses repetition and errors.
A single typo can break your infrastructure.
Variables solve this by:
Centralizing values in one place
Reducing duplication
Allowing environment switching
Preventing human error
Improving long-term maintainability
๐งฉ Types of Variables in Terraform
Terraform supports 3 major types of variables:
#๐ธ 1. Input Variables
Provide user-defined values to Terraform.
Example:
variable "env" {
type = string
default = "dev"
}
Use it:
tags = {
Environment = var.env
}
Input variables help eliminate repeated hardcoded values.
๐ธ 2. Local Values (locals)
Used for computed or reusable internal expressions.
Example:
locals {
bucket_name = "${var.env}-myapp-bucket"
}
Then use it:
bucket = local.bucket_name
Locals help maintain consistent naming conventions and avoid long expressions.
๐ธ 3. Output Variables
Display useful values after applying infrastructure.
Example:
output "vpc_id" {
value = aws_vpc.main.id
}
Useful for:
Debugging
Passing values to other modules
Automation
๐ง Variable Types
Terraform supports:
string
number
bool
list()
set()
map()
object()
tuple()
Special: any, null
Type constraints improve safety and catch errors early.
๐งฎ String Interpolation
Old syntax:
${var.env}-vpc
New recommended syntax:
"${var.env}-vpc"
Interpolation allows dynamic naming, concatenation, and more.
๐ Variable Precedence โ Which Value Wins?
Terraform chooses variable values in the following order (highest โ lowest):
1๏ธโฃ Command line
terraform apply -var="env=stage"
2๏ธโฃ .tfvars file
Example: dev.tfvars
env = "dev"
3๏ธโฃ Environment variables
export TF_VAR_env="prod"
4๏ธโฃ Default values (inside variable block)
Understanding precedence allows flexible environment switching without editing code.
๐ Hands-On: Using Variables While Deploying Resources
Day 5 showed real provisioning with variables:
Creating resources (e.g., VPC, S3 buckets)
Fixing AMI and permission errors
Updating variable-driven names
Running:
terraform plan
terraform apply
terraform output
Outputs displayed meaningful resource attributes, such as IDs.
๐งฐ Helpful Terraform State & Output Commands
View outputs:
terraform output
Useful for referencing values externally (pipelines, scripts, modules).
๐ Conclusion
Day 5 introduced the core of writing clean Terraform code: Variables.
By mastering input variables, locals, outputs, and precedence rules, Terraform configurations become:
โ Reusable
โ Scalable
โ Easier to maintain
โ Environment-ready
โ Less error-prone
_
Variables turn Terraform from โworking codeโ into professional Infrastructure-as-Code._
Top comments (0)