github.com/wikibal01/hashicorp-terraform@v0.11.12-beta1/website/docs/configuration/locals.html.md (about)

     1  ---
     2  layout: "docs"
     3  page_title: "Configuring Local Values"
     4  sidebar_current: "docs-config-locals"
     5  description: |-
     6    Local values assign a name to an expression that can then be used multiple times
     7    within a module.
     8  ---
     9  
    10  # Local Value Configuration
    11  
    12  Local values assign a name to an expression, that can then be used multiple
    13  times within a module.
    14  
    15  Comparing modules to functions in a traditional programming language,
    16  if [variables](./variables.html) are analogous to function arguments and
    17  [outputs](./outputs.html) are analogous to function return values then
    18  _local values_ are comparable to a function's local variables.
    19  
    20  This page assumes you're already familiar with
    21  [the configuration syntax](/docs/configuration/syntax.html).
    22  
    23  ## Examples
    24  
    25  Local values are defined in `locals` blocks:
    26  
    27  ```hcl
    28  # Ids for multiple sets of EC2 instances, merged together
    29  locals {
    30    instance_ids = "${concat(aws_instance.blue.*.id, aws_instance.green.*.id)}"
    31  }
    32  
    33  # A computed default name prefix
    34  locals {
    35    default_name_prefix = "${var.project_name}-web"
    36    name_prefix         = "${var.name_prefix != "" ? var.name_prefix : local.default_name_prefix}"
    37  }
    38  
    39  # Local values can be interpolated elsewhere using the "local." prefix.
    40  resource "aws_s3_bucket" "files" {
    41    bucket = "${local.name_prefix}-files"
    42    # ...
    43  }
    44  ```
    45  
    46  Named local maps can be merged with local maps to implement common or default
    47  values:
    48  
    49  ```hcl
    50  # Define the common tags for all resources
    51  locals {
    52    common_tags = {
    53      Component   = "awesome-app"
    54      Environment = "production"
    55    }
    56  }
    57  
    58  # Create a resource that blends the common tags with instance-specific tags.
    59  resource "aws_instance" "server" {
    60    ami           = "ami-123456"
    61    instance_type = "t2.micro"
    62  
    63    tags = "${merge(
    64      local.common_tags,
    65      map(
    66        "Name", "awesome-app-server",
    67        "Role", "server"
    68      )
    69    )}"
    70  }
    71  ```
    72  
    73  ## Description
    74  
    75  The `locals` block defines one or more local variables within a module.
    76  Each `locals` block can have as many locals as needed, and there can be any
    77  number of `locals` blocks within a module.
    78  
    79  The names given for the items in the `locals` block must be unique throughout
    80  a module. The given value can be any expression that is valid within
    81  the current module.
    82  
    83  The expression of a local value can refer to other locals, but as usual
    84  reference cycles are not allowed. That is, a local cannot refer to itself
    85  or to a variable that refers (directly or indirectly) back to it.
    86  
    87  It's recommended to group together logically-related local values into
    88  a single block, particularly if they depend on each other. This will help
    89  the reader understand the relationships between variables. Conversely,
    90  prefer to define _unrelated_ local values in _separate_ blocks, and consider
    91  annotating each block with a comment describing any context common to all
    92  of the enclosed locals.