Local values

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
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
locals {
  project = "webshop"
  prefix  = "${local.project}-prod"
}

resource "local_file" "app_config" {
  filename = "${local.prefix}-app.conf"
  content  = "# configuration for ${local.project}"
}

resource "local_file" "db_config" {
  filename = "${local.prefix}-db.conf"
  content  = "# configuration for ${local.project}"
}

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.

  1. Note the difference: the block is named locals (plural) but references use local. (singular). ↩︎