Terraform Variables, Outputs, and Locals Explained Simply
Table of Contents
Introduction #
Variables, outputs, and locals appear in almost every Terraform project. They are simple once you know their roles, but early on they can feel like three different ways to store the same value.
They are not the same.
Each one solves a different problem:
- Variables accept input
- Locals simplify internal reuse
- Outputs expose useful results
Once you think about them that way, Terraform configurations become much easier to read.
Variables: Input to the Configuration #
Variables let a Terraform configuration receive values from outside.
That means you can reuse the same code with different environments, names, locations, or sizes without hardcoding everything into the files.
For example:
|
|
This allows the configuration to use var.location anywhere that region is needed.
Variables are useful for values that may change between:
- Dev and production
- Different customers
- Different regions
- Different subscriptions
Locals: Internal Helpers #
Locals are values created inside the configuration to make expressions easier to reuse.
For example:
|
|
You can then reference local.naming_prefix throughout the configuration.
Locals are helpful when:
- A value is derived from several inputs
- The same expression is repeated many times
- You want more readable resource blocks
They do not accept external input directly. They are for internal organization, not external customization.
Outputs: Useful Values After Apply #
Outputs expose values from the Terraform configuration after resources are created or updated.
For example:
|
|
This lets Terraform print a result that is useful to the person running the deployment or to another system that consumes the state.
Outputs are commonly used for:
- Resource names
- Public IP addresses
- DNS names
- Connection details
- IDs needed by another module or pipeline step
How They Work Together #
A simple flow looks like this:
- A variable receives an input value.
- A local combines or reshapes values for internal reuse.
- A resource uses that value.
- An output exposes the result.
That flow is one reason Terraform feels structured. Inputs come in, the configuration processes them, and important results come back out.
A Practical Example #
Suppose you want to deploy a resource group with a consistent naming pattern.
- A variable supplies the environment name
- A local builds the final resource name
- An output returns the created resource group name
That keeps the configuration flexible without repeating the same string-building logic everywhere.
Common Beginner Mistakes #
A few mistakes show up often:
- Using variables for values that never need to come from outside
- Repeating long expressions instead of moving them into locals
- Forgetting outputs when a deployment produces values that will be needed later
Over time, clean Terraform usually uses all three in a balanced way.
Conclusion #
The easiest way to remember the difference is:
- Variables bring data in
- Locals organize data inside
- Outputs send useful data out
Once you understand those roles, many Terraform examples become much easier to follow and maintain.