Local values
A local value assigns a name to an expression, so you can compute a value once and reuse it throughout your configuration.
Declare local values using a locals block and reference them using the local.<name> syntax1:
main.tf
| |
If the naming convention changes you only update the locals block, and every resource that uses local.prefix picks up the change.
Local values differ from variables: a variable is an input provided from outside the configuration, while a local value is computed inside the configuration — often from variables:
flowchart LR
caller["Caller<br/>(CLI, tfvars, module)"] -- "input" --> var["var.environment"]
var --> expr["locals block:<br/>name = #quot;app-${var.environment}#quot;"]
expr --> ref["local.name used<br/>by resources"]
You can have any number of locals blocks, and locals can reference variables, resources, functions, and other locals.
Best practices
- Use local values to avoid repeating the same expression in multiple places.
- Use variables for anything the caller should control; use locals for values derived inside the configuration.
- Don’t overuse locals — a name for a value used only once adds indirection without benefit.
Note the difference: the block is named
locals(plural) but references uselocal.(singular). ↩︎