2017-08-17 19:21:59 +02:00
|
|
|
---
|
2021-11-23 00:57:25 +01:00
|
|
|
layout: "language"
|
|
|
|
page_title: "Local Values - Configuration Language"
|
|
|
|
sidebar_current: "docs-config-locals"
|
|
|
|
description: "Local values assign a name to an expression that can be used multiple times within a Terraform module."
|
2017-08-17 19:21:59 +02:00
|
|
|
---
|
|
|
|
|
2018-05-06 05:06:18 +02:00
|
|
|
# Local Values
|
2017-08-17 19:21:59 +02:00
|
|
|
|
2020-12-17 17:53:43 +01:00
|
|
|
> **Hands-on:** Try the [Simplify Terraform Configuration with
|
2021-11-23 00:57:25 +01:00
|
|
|
Locals](https://learn.hashicorp.com/tutorials/terraform/locals?in=terraform/configuration-language&utm_source=WEBSITE&utm_medium=WEB_IO&utm_offer=ARTICLE_PAGE&utm_content=DOCS)
|
|
|
|
tutorial on HashiCorp Learn.
|
2020-12-17 17:53:43 +01:00
|
|
|
|
2021-11-23 00:57:25 +01:00
|
|
|
A local value assigns a name to an [expression](/docs/language/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:
|
|
|
|
|
2021-11-23 00:57:25 +01:00
|
|
|
- [Input variables](./variables.html) are like function arguments.
|
|
|
|
- [Output values](./outputs.html) are like function return values.
|
2020-09-01 22:28:30 +02:00
|
|
|
- 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
|
2021-11-23 00:57:25 +01:00
|
|
|
[expressions](/docs/language/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.
|