@ -14,13 +14,15 @@ earlier, see
[0.11 Configuration Language: Local Values ](../configuration-0-11/locals.html ).
A local value assigns a name to an [expression ](./expressions.html ),
allowing it to be used multiple times within a module without repeating
so you can use it multiple times within a module without repeating
it.
Comparing modules to functions in a traditional programming language:
if [input variables ](./variables.html ) are analogous to function arguments and
[outputs values ](./outputs.html ) are analogous to function return values, then
_local values_ are comparable to a function's local temporary symbols.
If you're familiar with traditional programming languages, it can be useful to
compare Terraform modules to function definitions:
- [Input variables ](./variables.html ) are like function arguments.
- [Output values ](./outputs.html ) are like function return values.
- Local values are like a function's temporary local variables.
-> **Note:** For brevity, local values are often referred to as just "locals"
when the meaning is clear from context.
@ -37,10 +39,9 @@ locals {
}
```
The expressions assigned to local value names can either be simple constants
like the above, allowing these values to be defined only once but used many
times, or they can be more complex expressions that transform or combine
values from elsewhere in the module:
The expressions in local values are not limited to literal constants; they can
also reference other values in the module in order to transform or combine them,
including variables, resource attributes, or other local values:
```hcl
locals {
@ -51,24 +52,32 @@ locals {
locals {
# Common tags to be assigned to all resources
common_tags = {
Service = local.service_name #notice it's "local," no "s."
Service = local.service_name
Owner = local.owner
}
}
```
As shown above, local values can be referenced from elsewhere in the module
with an expression like `local.common_tags` , and locals can reference
each other in order to build more complex values from simpler ones.
## Using Local Values
Once a local value is declared, you can reference it in
[expressions ](./expressions.html ) as `local.<NAME>` .
-> **Note:** Local values are _created_ by a `locals` block (plural), but you
_reference_ them as attributes on an object named `local` (singular). Make sure
to leave off the "s" when referencing a local value!
```
resource "aws_instance" "example" {
# ...
tags = local.common_tags #notice it's "local," no "s."
tags = local.common_tags
}
```
A local value can only be accessed in expressions within the module where it
was declared.
## When To Use Local Values
Local values can be helpful to avoid repeating the same values or expressions