Terraform Locals
Terraform Locals are named values which can be assigned and used in your code. It mainly serves the purpose of reducing duplication within the Terraform code.
1 2 3 |
|
How to Use Terraform Locals?
- First, declare the local along and assign a value
1 2 3 4 5 6 |
|
- Then, use the local name anywhere in the code where that value is needed
1 2 3 4 5 6 7 8 9 |
|
Terraform Locals vs Variables
Variables
- Terraform variable can be scoped globally
- A variable value can be manipulated via expressions
- A variable value can be set from an input or .tfvars
file
- Can not use dynamic expressions
Locals
- A Local is only accessible within the local module
- A local in Terraform doesn’t change its value once assigned
- A local value can not be set from and input or .tfvars
file
- Can use dynamic expressions
Examples
Using locals to name resources
1 2 3 4 5 6 7 8 |
|
Using locals to set resource tags
1 2 3 4 5 6 7 8 |
|