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
