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