Problem: repeated values across resources
Imagine multiple resources referencing the same environment name or prefix. Copy-pasting strings across resource blocks might work at first, but moving fromdev to prod forces edits in many places and can introduce inconsistencies.
Example of repetitive, error-prone code:
environment name or a common tag) gets repeated in multiple places.
What locals are and how they help
Local values (locals) are named expressions you define once and reference anywhere in a Terraform configuration withlocal.<name>. Locals are ideal for:
- Naming conventions (prefixes, suffixes)
- Standard tag maps
- Derived or computed values that combine variables and data sources
- Hiding complex expressions behind a descriptive name
locals block and reference them across resources — change the value in one place and Terraform will apply it everywhere it’s used.

Defining and referencing locals
A basiclocals block is straightforward:
local.app_name or local.environment in resource blocks, outputs, and other expressions. Because locals are evaluated during planning/applying, updating the definition updates every dependent expression.
Example: centralizing naming across resources
Move duplicated naming logic into alocals block to build consistent resource names:
local.prefix and local.environment are the single source of truth for these names. Change them once to propagate updates.
Grouping locals and referencing locals from locals
You can have multiplelocals blocks for readability. Locals may reference other locals (avoid circular references).
Example: group common tags and reference a local inside another locals block:
local.common_tags map to any resource that supports tags:
local.common_tags will receive the same tag set; updating the tags in one place affects every resource.

When to use locals
Locals are particularly useful for:- Computed or derived values — combine variables, data sources, or inputs into a consistent format (naming conventions, tag values).
- Reusing constants — store project prefixes, environment names, or common tag maps so you only update one place.
- Complex expressions — move long or nested Terraform functions into named locals to keep resource blocks readable.
| Use case | Why use locals | Example |
|---|---|---|
| Naming conventions | Ensures consistent names across resources | local.prefix = "acme" |
| Tag standardization | Apply consistent tags to many resources | local.common_tags = { Owner = var.owner } |
| Derived values | Combine multiple inputs into one expression | local.service = "${var.team}-${var.app}-${var.environment}" |
| Complex expressions | Shorten resource blocks and improve readability | local.subnet_ids = compact(var.subnets) |
Use locals to centralize logic and keep resources focused on resource-specific configuration. Avoid placing heavy computation directly inside resource arguments; compute it once in a local and reference it.
Practical considerations and best practices
- Locals can reference variables, data sources, and other locals — but do not create circular references.
- Locals are evaluated during plan/apply and are not stored in state.
- Give locals descriptive names and group related locals together to make them discoverable.
- Prefer maps or objects (e.g., a
common_tagsmap) for shared sets of attributes rather than repeating keys across resources. - Use locals to hide complexity, but don’t overuse them to obscure obvious values.

Summary
Locals reduce repetition, centralize logic, and make complex expressions easier to manage. They serve as a single source of truth for commonly repeated values—change once, and every reference updates. Hands-on exercise: refactor a small module by extracting duplicated names, tag maps, and repeated computed expressions intolocals, then run terraform plan to verify the consolidated changes.