locals are a mechanism for giving a name to expressions that appear repeatedly inside a module and reusing them. You can consolidate "rules that rarely change" — naming conventions, shared tags, and the like — in a single place.
This article focuses on the stable concepts a Terraform Associate candidate should understand, and the high-impact uses that pay off in practice.
locals are named expressions scoped to a single module. They do not represent input values; they represent the result of transformations, joins, and default-value calculations. Reference them with local.<name>.
They are not evaluated in textual definition order; Terraform evaluates them based on the dependency graph, at the moment they are needed. They cannot be overridden from outside.
| Concept | Meaning | Scope | Source |
|---|---|---|---|
| locals | Pre-computed named expression | Current module | Variables, constants, other expressions |
| variable | Input from outside | Current module | tfvars / CLI / environment variables / parent module |
| output | Module output | Current module | Values inside the module |
| data source | External read | Current module | Provider / API |
Value flow centered on locals
Naming constraints differ across clouds and providers, but the universal advice is to "centralize the rules in one place and reuse them across every resource." Consolidating name assembly inside locals localizes the blast radius of any convention change.
The recommended shape: put org, environment, app, and component into an array, combine with join, lowercase with lower, and use substr for length control when needed. Use regexreplace when you need to substitute disallowed characters.
The example below uses locals to centralize naming and tags, and derives names for several components. To stay provider-agnostic, it is used through module calls.
The key is to finish the convention entirely within locals and pass "already-assembled names/tags" to modules as much as possible.
Centralizing naming and tags via locals (example)
variable "org" { type = string }
variable "environment" { type = string }
variable "app" { type = string }
variable "region" { type = string }
variable "extra_tags" { type = map(string) default = {} }
locals {
env = var.environment
app = var.app
component = "web"
name_parts = [var.org, local.env, local.app, local.component]
name = lower(join("-", compact(local.name_parts)))
name63 = substr(local.name, 0, 63)
tags_base = {
org = var.org
env = local.env
app = local.app
}
tags_common = merge(local.tags_base, var.extra_tags)
component_names = toset(["web", "api", "db"]) # adjust as needed
component_map = { for c in local.component_names : c => lower(join("-", [var.org, local.env, local.app, c])) }
}
module "web" {
source = "./modules/web"
name_prefix = local.name
tags = local.tags_common
}
module "components" {
source = "./modules/component"
for_each = local.component_map
name = each.value
common_tags = local.tags_common
}Push environment differences into variables and supply their values through per-environment tfvars. locals take those differences in and stay focused on producing "derived values" — names, tags, thresholds, and so on.
You can embed terraform.workspace in names, but leaning on workspaces to separate prod from staging is not recommended. Manage environments with explicit variables and tfvars to keep things reproducible.
locals are valid only within a module; parents and children cannot override or reference them. To share, expose values through outputs and pass them from parent to child through variables.
Evaluation follows the dependency graph, not declaration order. locals do not become known any earlier than their dependencies, so values that are unknown at plan time propagate as unknown. Cycles are errors.
At the Associate level, expect questions on the role of locals, how they differ from variables/outputs/data, their scope, and the canonical use cases (naming, tags, pre-computed constants).
The common real-world pitfall is misusing locals as if they were "configuration." Keep the basic pattern: input goes in variables, conventions and derivations go in locals, exposure goes in outputs.
Associate
問題 1
Organizational policy requires every resource name to include org-env-app-component in lowercase, hyphen-separated. Which Terraform implementation is the most appropriate?
正解: A
Centralizing derivation and fixed logic like naming conventions in locals and reusing it is optimal. B explodes duplication, C misuses outputs, and D encourages a workspace-dependent design — all unsuitable.
What is the difference between locals and variables?
variables are input values supplied from outside, while locals are named, pre-computed expressions derived from those values or constants. locals cannot be overridden and cannot be passed directly from outside the module.
Can a parent module override a child module's locals?
No. locals are an internal implementation detail of the module. Expose values you want to share via the child module's output, and pass them from the parent to another child through variables.
Can locals reference resource attributes? What about evaluation order?
Yes, they can. Terraform evaluates based on the dependency graph, so the order in which you write them does not matter. However, values that are unknown at plan time propagate as unknown into locals as well; locals cannot force them to become known.
Practice with certification-focused question sets
無料で問題を解いてみるNicheeLab Editorial Team
NicheeLab editorial team focused on data engineering and cloud certification learning. Content is structured around practical study needs and official exam domains.
HCL Syntax: Terraform's Configuration Language (2026)
HCL2 fundamentals for Terraform — blocks, attributes, expres...
Terraform Authoring & Operations Pro: Complete Guide (2026)
Tactics for the Terraform Pro exam — module authoring, works...
Terraform Providers: Plugin Management Fundamentals (2026)
Provider mechanics — required_providers, versions, mirrors, ...
Terraform Resource Blocks: Declarative Infra Units (2026)
Resource block fundamentals — addresses, references, common ...
Terraform Data Sources: Read-Only External Data (2026)
Data source basics — declaration, refresh behavior, dependen...