Hey everyone 👋
If you’re getting into Terraform, infrastructure as code (IaC), or building cloud environments that don’t fall apart every time you sneeze — you need to know about Terraform variables.
At first, variables felt like just another “programming” thing I had to learn. But once I started working with real-world Terraform code — and especially when dealing with environments like dev, staging, and production — I realized that variables are the glue that holds it all together.
Let me break it down the way I wish someone had for me 👇
🧸 Think of Terraform Variables Like Fill-in-the-Blank Templates
Imagine you’re ordering pizza for your office every Friday. But instead of writing the full order from scratch every week, you use a reusable note like:
„Order one {size} pizza with {toppings} for {team_name}.“
That’s what Terraform variables let you do — define a flexible template, and just plug in new values.
⚙️ Why Use Terraform Variables?
Variables help you:
✅ Avoid Hardcoding
If you repeat the same IP or port number 20 times in your code, and it changes… well, good luck.
With variables, change it in one place — done.
🌍 Support Multiple Environments
You can deploy the same infrastructure to dev, staging, or prod — just swap out the values.
🧹 Keep Your Code Clean & Reusable
Terraform code is easier to read when it’s not cluttered with random literal values everywhere.
🧠 The Three Parts of Terraform Variable Usage
- Declare the Variable
variable "instance_type" {
description = "Type of EC2 instance"
}
- Use the Variable in Your Code
resource "aws_instance" "web" {
ami = "ami-123456"
instance_type = var.instance_type
}
-
Assign a Value to the Variable
There are multiple ways to do this — let’s explore them 👇
🧪 5 Ways to Assign Variable Values (And Which One Wins)
Let’s say you have a variable called instance_type
. Where can you set its value?
⚙️ Method | 📄 Example | 🔼 Priority |
---|---|---|
Default | Inside variables.tf
|
Lowest |
.tfvars file |
terraform.tfvars |
Medium |
Environment variable | TF_VAR_instance_type |
Medium |
CLI flag | -var="instance_type=m5.large" |
Highest |
Interactive prompt | If nothing is provided | Only if needed |
💡 Analogy:
It’s like choosing your pizza size — the last person to yell out their preference wins 🍕
📁 The .tfvars
File: Your Environment-Specific Config
Let’s say your team has multiple environments — dev, staging, and prod. You don’t want to manually change values every time.
Instead, you can create:
dev.tfvars
prod.tfvars
Then run:
terraform apply -var-file="dev.tfvars"
Or just name it terraform.tfvars
and Terraform will load it automatically.
🧪 Setting Variables in the CLI or Environment
✅ CLI Example:
terraform apply -var="instance_type=t3.medium"
✅ Environment Variable (Linux/macOS):
export TF_VAR_instance_type="t3.small"
✅ On Windows:
Set the environment variable in System Properties as TF_VAR_instance_type
Just make sure to open a new terminal window for it to take effect.
🧪 Which Value Does Terraform Use If There Are Conflicts?
Terraform follows a strict precedence order — the last one wins.
💾 Where Value Comes From | 🔼 Priority |
---|---|
default in variables.tf
|
Low |
terraform.tfvars |
Medium |
TF_VAR_ environment variable |
Medium |
-var or -var-file CLI flag |
✅ Highest |
💡 Example:
# default: t2.micro
# terraform.tfvars: t2.small
# CLI: -var="instance_type=m5.large"
🟢 Terraform will use m5.large
🧪 Bonus: Variable Description & Validation
Want to make life easier for your teammates?
Add a description to your variables:
variable "vpn_ip" {
description = "VPN server IP address used for firewall rules"
}
Want to ensure the value is valid? Add a validation block:
variable "env" {
type = string
validation {
condition = contains(["dev", "prod"], var.env)
error_message = "Environment must be dev or prod"
}
}
🧩 Final Thoughts
Terraform variables might feel small at first, but they make a huge difference when your codebase starts to grow.
They help you:
- Avoid messy copy-paste values
- Cleanly separate logic from configuration
- Reuse the same code across environments
- Minimize errors and debugging time
If you’re building with Terraform, learn to wield variables early — your future self (and your teammates) will thank you.
Let me know if this helped, or if you’ve got your own Terraform tips! Would love to connect on LinkedIn and chat cloud/IaC stuff with other builders 🚀