2017-08-17 19:21:59 +02:00
|
|
|
---
|
2020-08-15 03:51:06 +02:00
|
|
|
layout: "language"
|
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).
|
|
|
|
|
2020-11-13 03:30:52 +01:00
|
|
|
A local value assigns a name to an [expression](/docs/configuration/expressions/index.html),
|
2020-09-01 22:28:30 +02:00
|
|
|
so you can use it multiple times within a module without repeating
|
2018-05-06 05:06:18 +02:00
|
|
|
it.
|
2017-08-17 19:21:59 +02:00
|
|
|
|
2020-09-01 22:28:30 +02:00
|
|
|
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.
|
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
|
|
|
```
|
|
|
|
|
2020-09-01 22:28:30 +02:00
|
|
|
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:
|
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
|
2019-04-18 18:03:34 +02:00
|
|
|
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 = {
|
2020-09-01 22:28:30 +02:00
|
|
|
Service = local.service_name
|
2018-05-06 05:06:18 +02:00
|
|
|
Owner = local.owner
|
|
|
|
}
|
2017-09-08 18:44:16 +02:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2020-09-01 22:28:30 +02:00
|
|
|
## Using Local Values
|
|
|
|
|
|
|
|
Once a local value is declared, you can reference it in
|
2020-11-13 03:30:52 +01:00
|
|
|
[expressions](/docs/configuration/expressions/index.html) as `local.<NAME>`.
|
2020-09-01 22:28:30 +02:00
|
|
|
|
|
|
|
-> **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!
|
2017-08-17 19:21:59 +02:00
|
|
|
|
2018-05-06 05:06:18 +02:00
|
|
|
```
|
|
|
|
resource "aws_instance" "example" {
|
|
|
|
# ...
|
|
|
|
|
2020-09-01 22:28:30 +02:00
|
|
|
tags = local.common_tags
|
2018-05-06 05:06:18 +02:00
|
|
|
}
|
|
|
|
```
|
2017-08-17 19:21:59 +02:00
|
|
|
|
2020-09-01 22:28:30 +02:00
|
|
|
A local value can only be accessed in expressions within the module where it
|
|
|
|
was declared.
|
|
|
|
|
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.
|