liveBook · Manning

Local value

As explained in Terraform in Depth: Infrastructure as Code with Terraform and OpenTofu

A Terraform local variable is a module-scoped value that stores reusable or derived configuration data without exposing it externally.

When several resources need the same tags, define them once as a Terraform local value and reference them wherever needed. Locals also hold intermediate values used by a module’s internal logic. Unlike input variables, they do not accept values from module users; unlike outputs, they are not exposed to those users. section 3.5.1 section 3.2

Listing 3.13 locals usage
locals {
  tags = { "Author" : "Rob", "ManagedBy" : "Terraform" }       #1
}
 
resource "aws_instance" "first_instance" {
  subnet_id     = var.subnet_id
  instance_type = "t3.micro"
  tags          = local.tags                                   #2
}
 
resource "aws_instance" "second_instance" {
  subnet_id     = var.subnet_id
  instance_type = "t3.small"
  tags          = local.tags                                   #3
}

The `locals` block defines one `tags` value. Both instance resources use `local.tags`, so the shared configuration has one definition. section 3.5.1

A `locals` block has no label. Each assignment inside it defines a separate local, referenced with `local.` followed by its name—for example, `local.tags`. A module can contain multiple `locals` blocks, even across files, but it cannot define the same local name twice. A local can refer to another local; circular dependencies are not allowed. section 3.5.1 section 3.2

Listing 3.11 locals blocks
locals {
  alpha   = 1                                                  #1
  bravo   = "two"                                              #1
  charlie = false                                              #1
}
 
locals {                                                       #2
  delta = ["four", "five"]                                     #2
  echo = {                                                     #2
    "foxtrot" = "six",                                         #2
    "golf"    = "seven"                                        #2
  }
}
 
locals {
  hotel = local.bravo                                          #3
}

Separate blocks define independent locals, while `hotel = local.bravo` shows one local using another. section 3.5.1

Keep a value local when it serves only internal processing or reuse. To make its value available outside the module, assign it to an output. Like other Terraform variables, a local’s value is set once rather than changed during a run. section 3.5 section 3.2

How Terraform in Depth: Infrastructure as Code with Terraform and OpenTofu covers it

How deep it goes
Defines block syntax, references, scope, and duplicate-name errors; defers fuller treatment of dependency rules.
Examples
Shows two AWS instances sharing tags and separate locals blocks with one local referencing another.
What it assumes
Assumes basic familiarity with modules and resources while explaining how to define and reference locals.
What you can do after
Apply it via Terraform to reuse module-internal values and expose one through an output when needed.
How it connects
Relates locals to Terraform modules, input variables, output variables, resource blocks, and dependency rules.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Terraform in Depth: Infrastructure as Code with Terraform and OpenTofu. The section links point to the parts of the book it draws on.