
If your Terraform configuration is full of repeated expressions, hard-to-read resource arguments, or values you keep copying from one block to another — locals are the fix.
Terraform locals let you define a value once, give it a clear name, and reuse it anywhere in your configuration. They are one of the simplest features in Terraform, but also one of the most impactful for keeping your codebase clean, consistent, and easy to maintain.
This guide explains what locals are, when to use them, how they compare to variables and outputs, and how to use them effectively — with real examples throughout.
What Are Terraform Locals?
A local value in Terraform is a named expression you define inside a locals block. Once defined, you reference it using local.<name> anywhere in the same module.
Here is the simplest possible example:
That is it. You define the value once in the locals block, and reference it with local.environment wherever you need it.
Of course, locals become much more powerful when you use them to compute values rather than just store static strings:
Locals are evaluated lazily — Terraform only computes a local value when it is actually referenced. You can define as many as you like without any performance cost.
Locals vs Variables: What Is the Difference?
This is one of the most common points of confusion for teams new to Terraform. Both locals and variables let you name a value and reuse it — but they serve very different purposes.
The simple rule is: use variables for values that should come from outside the module, and use locals for values you compute or derive inside the module.
A Common Mistake: Using Variables Where Locals Belong
Teams sometimes reach for a variable when they actually need a local. Here is an example of the problem and the fix:
Using a local here keeps the module interface clean. Callers only need to supply app_name and environment — the derived values are handled internally.
Locals vs Outputs: When to Use Each
Locals and outputs can look similar at first glance — both give names to values. But they have completely different audiences.
.webp)