Locals and derived values
Calculate a name once, reuse it everywhere
A local value gives a name to an expression inside one Terraform module. If a team names several objects using a project and environment, repeating the naming formula in each resource invites inconsistent edits. Put the formula in one place instead. The following complete configuration computes a summary without creating any resources. Expected results throughout this chapter are predictions, not execution logs.
variable "project" { type = string default = "catalog"}
variable "environment" { type = string default = "dev"}
locals { service_name = "${var.project}-${var.environment}" tags = { project = var.project environment = var.environment service = local.service_name }}
output "summary" { value = { name = local.service_name, tags = local.tags }}With the default inputs, the summary name is catalog-dev. Its tags contain project catalog, environment dev, and service catalog-dev. Setting the environment to test changes the name and the corresponding tags together. The interpolation between ${ and } inserts values into a string; the hyphen is literal. The output block lets you inspect the result; its full interface is taught in the next lesson.
Choose the correct namespace
Use var.project for an input declared with a variable block, and local.service_name for a value declared within locals. Notice that the block name is plural and the reference prefix is singular. Inputs are supplied by callers; locals are computed from configuration. A tfvars file or CLI variable flag can override a declared input, but cannot directly assign a local value. Outputs expose selected results to a caller. Locals remain internal to the module unless explicitly exposed through an output.
Read dependencies rather than line order
Terraform resolves references between expressions. These two blocks are an illustration of a forward reference, not a standalone lab with a displayed result:
locals { message = "service-${local.short_name}"}
locals { short_name = "billing"}The message is service-billing even though short_name appears later. Multiple locals blocks share the same module namespace; defining a name twice is an error, not an override. A local may depend on another local, but a cycle cannot be resolved. For instance, this deliberately invalid configuration makes each value depend on the other:
locals { left = local.right right = local.left}Terraform should report a dependency-cycle diagnostic. Do not repair a cycle by rearranging lines: remove the circular dependency by choosing an independent input or expression.
Understand evaluation and boundaries
A local is not a mutable slot that can be incremented as a script runs. It is an expression in the configuration graph. It can refer to resource attributes, so a value might remain unknown during planning and become known during apply. A local does not make an unknown value known earlier. It can also carry sensitive information from a referenced value; it is not a way to remove sensitivity or guarantee that downstream state contains no sensitive data. Use meaningful locals for reusable calculations, not a second alias for every input.
Practice a naming rule
Use the derived-values/ fixture and its parent README. Predict the summary for project orders and environment qa. Then design a second naming rule for a fictional worker using team and stage inputs and a fixed worker suffix. Explain which inputs a caller can change and which expression remains internal. Next: outputs and module interfaces.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Locals and derived values — knowledge check
Check derived values and output interfaces.
Which expression reads a local called service_name?
What happens to the example name if environment becomes test?
Can a caller set local.service_name directly with a -var flag?
What happens when two locals depend on each other?
A local uses an attribute unknown at plan time. What can happen?