github.com/rstandt/terraform@v0.12.32-0.20230710220336-b1063613405c/website/docs/internals/resource-addressing.html.markdown (about)

     1  ---
     2  layout: "docs"
     3  page_title: "Internals: Resource Address"
     4  sidebar_current: "docs-internals-resource-addressing"
     5  description: |-
     6    Resource addressing is used to target specific resources in a larger
     7    infrastructure.
     8  ---
     9  
    10  # Resource Addressing
    11  
    12  A __Resource Address__ is a string that references a specific resource in a
    13  larger infrastructure. An address is made up of two parts:
    14  
    15  ```
    16  [module path][resource spec]
    17  ```
    18  
    19  __Module path__:
    20  
    21  A module path addresses a module within the tree of modules. It takes the form:
    22  
    23  ```
    24  module.A.module.B.module.C...
    25  ```
    26  
    27  Multiple modules in a path indicate nesting. If a module path is specified
    28  without a resource spec, the address applies to every resource within the
    29  module. If the module path is omitted, this addresses the root module.
    30  
    31  __Resource spec__:
    32  
    33  A resource spec addresses a specific resource in the config. It takes the form:
    34  
    35  ```
    36  resource_type.resource_name[resource index]
    37  ```
    38  
    39   * `resource_type` - Type of the resource being addressed.
    40   * `resource_name` - User-defined name of the resource.
    41   * `[resource index]` - an optional index into a resource with multiple
    42     instances, surrounded by square brace characters (`[` and `]`).
    43  
    44  -> In Terraform v0.12 and later, a resource spec without a module path prefix
    45  matches only resources in the root module. In earlier versions, a resource spec
    46  without a module path prefix will match resources with the same type and name
    47  in any descendent module.
    48  
    49  __Resource index__:
    50  
    51   * `[N]` where `N` is a `0`-based numerical index into a resource with multiple
    52     instances specified by the `count` meta-argument. Omitting an index when
    53     addressing a resource where `count > 1` means that the address references
    54     all instances.
    55   * `["INDEX"]` where `INDEX` is a alphanumerical key index into a resource with
    56     multiple instances specified by the `for_each` meta-argument.
    57  
    58  ## Examples
    59  
    60  ### count Example
    61  
    62  Given a Terraform config that includes:
    63  
    64  ```hcl
    65  resource "aws_instance" "web" {
    66    # ...
    67    count = 4
    68  }
    69  ```
    70  
    71  An address like this:
    72  
    73  ```
    74  aws_instance.web[3]
    75  ```
    76  
    77  Refers to only the last instance in the config, and an address like this:
    78  
    79  ```
    80  aws_instance.web
    81  ```
    82  
    83  Refers to all four "web" instances.
    84  
    85  ### for_each Example
    86  
    87  Given a Terraform config that includes:
    88  
    89  ```hcl
    90  resource "aws_instance" "web" {
    91    # ...
    92    for_each = {
    93      "terraform": "value1",
    94      "resource":  "value2",
    95      "indexing":  "value3",
    96      "example":   "value4",
    97    }
    98  }
    99  ```
   100  
   101  An address like this:
   102  
   103  ```
   104  aws_instance.web["example"]
   105  ```
   106  
   107  Refers to only the "example" instance in the config.