github.com/eliastor/durgaform@v0.0.0-20220816172711-d0ab2d17673e/website/docs/language/values/locals.mdx (about) 1 --- 2 page_title: Local Values - Configuration Language 3 description: >- 4 Local values assign a name to an expression that can be used multiple times 5 within a Terraform module. 6 --- 7 8 # Local Values 9 10 > **Hands-on:** Try the [Simplify Terraform Configuration with 11 > 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) 12 > tutorial on HashiCorp Learn. 13 14 A local value assigns a name to an [expression](/language/expressions), 15 so you can use the name multiple times within a module instead of repeating 16 the expression. 17 18 If you're familiar with traditional programming languages, it can be useful to 19 compare Terraform modules to function definitions: 20 21 - [Input variables](/language/values/variables) are like function arguments. 22 - [Output values](/language/values/outputs) are like function return values. 23 - Local values are like a function's temporary local variables. 24 25 -> **Note:** For brevity, local values are often referred to as just "locals" 26 when the meaning is clear from context. 27 28 ## Declaring a Local Value 29 30 A set of related local values can be declared together in a single `locals` 31 block: 32 33 ```hcl 34 locals { 35 service_name = "forum" 36 owner = "Community Team" 37 } 38 ``` 39 40 The expressions in local values are not limited to literal constants; they can 41 also reference other values in the module in order to transform or combine them, 42 including variables, resource attributes, or other local values: 43 44 ```hcl 45 locals { 46 # Ids for multiple sets of EC2 instances, merged together 47 instance_ids = concat(aws_instance.blue.*.id, aws_instance.green.*.id) 48 } 49 50 locals { 51 # Common tags to be assigned to all resources 52 common_tags = { 53 Service = local.service_name 54 Owner = local.owner 55 } 56 } 57 ``` 58 59 ## Using Local Values 60 61 Once a local value is declared, you can reference it in 62 [expressions](/language/expressions) as `local.<NAME>`. 63 64 -> **Note:** Local values are _created_ by a `locals` block (plural), but you 65 _reference_ them as attributes on an object named `local` (singular). Make sure 66 to leave off the "s" when referencing a local value! 67 68 ``` 69 resource "aws_instance" "example" { 70 # ... 71 72 tags = local.common_tags 73 } 74 ``` 75 76 A local value can only be accessed in expressions within the module where it 77 was declared. 78 79 ## When To Use Local Values 80 81 Local values can be helpful to avoid repeating the same values or expressions 82 multiple times in a configuration, but if overused they can also make a 83 configuration hard to read by future maintainers by hiding the actual values 84 used. 85 86 Use local values only in moderation, in situations where a single value or 87 result is used in many places _and_ that value is likely to be changed in 88 future. The ability to easily change the value in a central place is the key 89 advantage of local values.