2017-08-17 19:21:59 +02:00
|
|
|
---
|
|
|
|
layout: "docs"
|
2018-12-20 05:34:34 +01:00
|
|
|
page_title: "Local Values - Configuration Language"
|
2017-08-17 19:21:59 +02:00
|
|
|
sidebar_current: "docs-config-locals"
|
|
|
|
description: |-
|
|
|
|
Local values assign a name to an expression that can then be used multiple times
|
|
|
|
within a module.
|
|
|
|
---
|
|
|
|
|
2018-05-06 05:06:18 +02:00
|
|
|
# Local Values
|
2017-08-17 19:21:59 +02:00
|
|
|
|
2019-01-17 01:30:43 +01:00
|
|
|
-> **Note:** This page is about Terraform 0.12 and later. For Terraform 0.11 and
|
|
|
|
earlier, see
|
|
|
|
[0.11 Configuration Language: Local Values](../configuration-0-11/locals.html).
|
|
|
|
|
2018-12-11 01:14:33 +01:00
|
|
|
A local value assigns a name to an [expression](./expressions.html),
|
|
|
|
allowing it to be used multiple times within a module without repeating
|
2018-05-06 05:06:18 +02:00
|
|
|
it.
|
2017-08-17 19:21:59 +02:00
|
|
|
|
2018-12-11 01:14:33 +01:00
|
|
|
Comparing modules to functions in a traditional programming language:
|
2018-05-06 05:06:18 +02:00
|
|
|
if [input variables](./variables.html) are analogous to function arguments and
|
2018-12-11 01:14:33 +01:00
|
|
|
[outputs values](./outputs.html) are analogous to function return values, then
|
2018-05-06 05:06:18 +02:00
|
|
|
_local values_ are comparable to a function's local temporary symbols.
|
2017-08-17 19:21:59 +02:00
|
|
|
|
2018-12-11 01:14:33 +01:00
|
|
|
-> **Note:** For brevity, local values are often referred to as just "locals"
|
|
|
|
when the meaning is clear from context.
|
|
|
|
|
2018-05-06 05:06:18 +02:00
|
|
|
## Declaring a Local Value
|
2017-08-17 19:21:59 +02:00
|
|
|
|
2019-03-13 17:48:54 +01:00
|
|
|
A set of related local values can be declared together in a single `locals`
|
2018-05-06 05:06:18 +02:00
|
|
|
block:
|
2017-08-17 19:21:59 +02:00
|
|
|
|
|
|
|
```hcl
|
|
|
|
locals {
|
2018-05-06 05:06:18 +02:00
|
|
|
service_name = "forum"
|
|
|
|
owner = "Community Team"
|
2017-09-07 19:49:33 +02:00
|
|
|
}
|
2017-08-17 19:21:59 +02:00
|
|
|
```
|
|
|
|
|
2018-05-06 05:06:18 +02:00
|
|
|
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:
|
2017-09-08 18:44:16 +02:00
|
|
|
|
|
|
|
```hcl
|
|
|
|
locals {
|
2018-05-06 05:06:18 +02:00
|
|
|
# Ids for multiple sets of EC2 instances, merged together
|
|
|
|
instance_ids = "${concat(aws_instance.blue.*.id, aws_instance.green.*.id)}"
|
2017-09-08 18:44:16 +02:00
|
|
|
}
|
|
|
|
|
2018-05-06 05:06:18 +02:00
|
|
|
locals {
|
|
|
|
# Common tags to be assigned to all resources
|
|
|
|
common_tags = {
|
|
|
|
Service = local.service_name
|
|
|
|
Owner = local.owner
|
|
|
|
}
|
2017-09-08 18:44:16 +02:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2018-05-06 05:06:18 +02:00
|
|
|
As shown above, local values can be referenced from elsewhere in the module
|
2018-12-11 01:14:33 +01:00
|
|
|
with an expression like `local.common_tags`, and locals can reference
|
2018-05-06 05:06:18 +02:00
|
|
|
each other in order to build more complex values from simpler ones.
|
2017-08-17 19:21:59 +02:00
|
|
|
|
2018-05-06 05:06:18 +02:00
|
|
|
```
|
|
|
|
resource "aws_instance" "example" {
|
|
|
|
# ...
|
|
|
|
|
|
|
|
tags = local.common_tags
|
|
|
|
}
|
|
|
|
```
|
2017-08-17 19:21:59 +02:00
|
|
|
|
2018-05-06 05:06:18 +02:00
|
|
|
## When To Use Local Values
|
2017-08-17 19:21:59 +02:00
|
|
|
|
2018-12-11 01:14:33 +01:00
|
|
|
Local values can be helpful to avoid repeating the same values or expressions
|
2018-05-06 05:06:18 +02:00
|
|
|
multiple times in a configuration, but if overused they can also make a
|
|
|
|
configuration hard to read by future maintainers by hiding the actual values
|
|
|
|
used.
|
2017-08-17 19:21:59 +02:00
|
|
|
|
2018-12-11 01:14:33 +01:00
|
|
|
Use local values only in moderation, in situations where a single value or
|
2018-05-06 05:06:18 +02:00
|
|
|
result is used in many places _and_ that value is likely to be changed in
|
|
|
|
future. The ability to easily change the value in a central place is the key
|
|
|
|
advantage of local values.
|