Skip to main content
One of the biggest challenges as your Terraform codebase grows is keeping it clean and maintainable. Repeating the same values — naming conventions, environment tags, or computed expressions — leads to duplicated effort and increases the risk of mistakes. Locals let you define values once and reuse them anywhere in your configuration. That simplifies updates to shared values, reduces repetition, and makes your configuration easier to read and reason about.
A purple presentation slide titled "Using Locals to Avoid Code Duplication" with a faint "LECTURE" watermark. It also shows the HashiCorp Terraform logo and the author name "Bryan Krausen."

The problem: repeated values across resources

Imagine managing several resources and copying the same environment name or prefix into each resource. That works at first, but when you move from dev to prod you must update the same value in multiple places — a repetitive and error-prone task. Here’s an example showing duplicated tag values across two resources:
This avoids hard-coded values, but it still repeats the same tag map structure and computed expressions in multiple places. Locals let you centralize these values and expressions in one place.

What are locals?

Local values (commonly called “locals”) are named values defined in a locals block. They act as short-term memory for your configuration and are ideal for naming conventions, shared tags, environment names, or temporary computed values. Example:
Reference these with local.app_name and local.environment. Update the locals block once and every reference updates automatically.
Use locals to move repeated logic or long expressions out of resource blocks. This makes your resources concise and centralizes shared logic for easier updates.

Simple use: centralized prefix and environment

Define shared values once and reference them when naming resources:
By storing prefix and environment in locals, both resource names are generated consistently. Change the values in one place and all resources reflect that change. You can create multiple locals blocks to group related values. Locals can reference other locals (but be careful about creating cycles). Example: group common tags into a single map and reuse it across resources.
Then reuse the entire map in a resource:
Any change to local.common_tags updates every resource that references it. This centralization is the primary advantage of locals: a single source of truth for commonly repeated values.
You can reference one local from another (for example, app_team = local.app_team_base), but you must avoid circular references. Ensure locals only reference values defined earlier or computed without cycles.
A colorful infographic titled "Benefits of Locals" with three panels labeled "Centralized Logic," "Clearer Code," and "Less Risk," each featuring a Terraform logo and brief explanatory text. The footer credits "Bryan Krausen" and "HashiCorp Terraform."

When to use locals — guidance and examples

Use locals in these common scenarios:
  • Computed or derived values: Combine variables, data sources, or inputs to produce standardized formats (naming conventions, IDs, tags).
  • Reusable constants: Store prefixes, environment names, or shared tag sets so you only update them in one place.
  • Complex expressions: Move long or messy Terraform expressions into locals to keep resource blocks concise and readable.
Quick reference table: Some Terraform functions and expressions become long when embedded in resources. Calculate them in a local first, then reference the local in your resource for clearer configuration.
A purple presentation slide titled "When Should I Use Local Values?" with bullet points explaining when to use locals in code. On the right is a framed photo of a laptop on a purple background and HashiCorp Terraform branding along the bottom.

Summary

Locals reduce repetition, centralize logic, and manage complex expressions in one place. They make Terraform configurations cleaner, more maintainable, and less error-prone. Refactor repetitive code into locals, create computed values once, and make configurations more dynamic and easier to update.

Watch Video

Practice Lab