Hey everyone 👋
If you’ve started using Terraform to build infrastructure, you’ve probably come across variables — those flexible inputs that help you avoid hardcoding stuff. But what about locals?
At first, I didn’t quite get them. I figured: “Aren’t variables enough?” But once I ran into repeating values, dynamic logic, and tagging issues, locals started making a lot more sense.
Let me break it down the way I wish someone had explained it to me 👇
🧸 Think of Locals Like Sticky Notes (for Your Terraform Project)
Imagine you're planning a party. You write "🎉 Party at 7 PM" on five separate invitations — but later realize you want to change it to 8 PM. Now you have to hunt down all five and edit each one. Annoying, right?
Wouldn’t it be better to write it once on a sticky note and reference that? That way, if plans change, you only update one spot.
That’s what Terraform locals do. They centralize static or computed values — especially ones you reuse across resources — so you don’t repeat yourself.
⚙️ Why Use Local Values?
Variables are great when you want to input values from the outside — like via CLI, tfvars
, or environment variables.
But locals shine when:
✅ You have repeated static values across multiple resources (e.g., common tags).
✅ You want to compute a value using functions (e.g., today's date, list merging).
✅ You want to clean up complex expressions and give them a name.
💬 Variables vs Locals — What’s the Difference?
📦 Feature | 🧪 Variables (var ) |
🗒️ Locals (local ) |
---|---|---|
Input from CLI/tfvars? | ✅ Yes | ❌ No — fixed inside your .tf code |
Can use functions? | ❌ Not directly | ✅ Yes — use formatdate() , concat() , etc. |
Overridable? | ✅ Yes | ❌ No — must edit the code |
Best for | Dynamic inputs across environments | Static values or computed logic |
🧱 Real-World Example: Common Tags
Without locals:
tags = {
Team = "Security"
}
Copied in multiple places. If it changes? You’re updating every block.
With locals:
locals {
common_tags = {
Team = "Security"
}
}
resource "aws_security_group" "web" {
tags = local.common_tags
}
Now your team name is defined once — and reused everywhere 🧼
🔮 Where Locals Really Shine: Expressions
Locals can do math, string manipulation, date formatting, list merging… and more.
Example: auto-tagging a resource with the creation date
locals {
tags = {
Team = "DevOps"
CreationDate = formatdate("DD-MM-YYYY", timestamp())
}
}
Variables can’t do this. If you try to use a function like timestamp()
inside a variable? Terraform throws an error 🚫
⚠️ Quick Gotchas
🧩 Gotcha | ✅ What to Remember |
---|---|
Locals block = locals
|
But you reference them with local.something (singular!) |
Not for dynamic input | You can’t pass values into locals from the outside |
Edit-only values | Any change = direct code edit, not CLI or pipeline-friendly |
🧠 When to Use Locals
Use locals when:
- You have repeated hardcoded values (e.g., common tags, names)
- You want to compute values with logic or functions
- You want to simplify repeated expressions (DRY: Don’t Repeat Yourself)
Avoid locals if:
- You need flexibility from the CLI, CI/CD, or
tfvars
- The value should change across environments
🧩 Final Thoughts
Locals are not a replacement for variables — they’re a companion tool. They help keep your Terraform clean, consistent, and DRY.
If you’re working with repetitive blocks or starting to use more complex logic in your .tf
files, try reaching for locals
next time.
Want to chat about Terraform best practices or walk through examples together? Hit me up on LinkedIn — I’m always down to learn from fellow DevOps explorers 🚀☁️
Top comments (0)