github.com/hugorut/terraform@v1.1.3/website/docs/cli/state/resource-addressing.mdx (about) 1 --- 2 page_title: 'Internals: Resource Address' 3 description: |- 4 A resource address is a string that identifies zero or more resource 5 instances in your overall configuration. 6 --- 7 8 # Resource Addressing 9 10 A _resource address_ is a string that identifies zero or more resource 11 instances in your overall configuration. 12 13 An address is made up of two parts: 14 15 ``` 16 [module path][resource spec] 17 ``` 18 19 In some contexts Terraform might allow for an incomplete resource address that 20 only refers to a module as a whole, or that omits the index for a 21 multi-instance resource. In those cases, the meaning depends on the context, 22 so you'll need to refer to the documentation for the specific feature you 23 are using which parses resource addresses. 24 25 ## Module path 26 27 A module path addresses a module within the tree of modules. It takes the form: 28 29 ``` 30 module.module_name[module index] 31 ``` 32 33 - `module` - Module keyword indicating a child module (non-root). Multiple `module` 34 keywords in a path indicate nesting. 35 - `module_name` - User-defined name of the module. 36 - `[module index]` - (Optional) [Index](#index-values-for-modules-and-resources) 37 to select an instance from a module call that has multiple instances, 38 surrounded by square bracket characters (`[` and `]`). 39 40 An address without a resource spec, i.e. `module.foo` applies to every resource within 41 the module if a single module, or all instances of a module if a module has multiple instances. 42 To address all resources of a particular module instance, include the module index in the address, 43 such as `module.foo[0]`. 44 45 If the module path is omitted, the address applies to the root module. 46 47 An example of the `module` keyword delineating between two modules that have multiple instances: 48 49 ``` 50 module.foo[0].module.bar["a"] 51 ``` 52 53 -> Module index only applies to modules in Terraform v0.13 or later. In earlier 54 versions of Terraform, a module could not have multiple instances. 55 56 ## Resource spec 57 58 A resource spec addresses a specific resource instance in the selected module. 59 It has the following syntax: 60 61 ``` 62 resource_type.resource_name[instance index] 63 ``` 64 65 - `resource_type` - Type of the resource being addressed. 66 - `resource_name` - User-defined name of the resource. 67 - `[instance index]` - (Optional) [Index](#index-values-for-modules-and-resources) 68 to select an instance from a resource that has multiple instances, 69 surrounded by square bracket characters (`[` and `]`). 70 71 -> In Terraform v0.12 and later, a resource spec without a module path prefix 72 matches only resources in the root module. In earlier versions, a resource spec 73 without a module path prefix would match resources with the same type and name 74 in any descendent module. 75 76 ## Index values for Modules and Resources 77 78 The following specifications apply to index values on modules and resources with multiple instances: 79 80 - `[N]` where `N` is a `0`-based numerical index into a resource with multiple 81 instances specified by the `count` meta-argument. Omitting an index when 82 addressing a resource where `count > 1` means that the address references 83 all instances. 84 - `["INDEX"]` where `INDEX` is a alphanumerical key index into a resource with 85 multiple instances specified by the `for_each` meta-argument. 86 87 ## Examples 88 89 ### count Example 90 91 Given a Terraform config that includes: 92 93 ```hcl 94 resource "aws_instance" "web" { 95 # ... 96 count = 4 97 } 98 ``` 99 100 An address like this: 101 102 ``` 103 aws_instance.web[3] 104 ``` 105 106 Refers to only the last instance in the config, and an address like this: 107 108 ``` 109 aws_instance.web 110 ``` 111 112 Refers to all four "web" instances. 113 114 ### for_each Example 115 116 Given a Terraform config that includes: 117 118 ```hcl 119 resource "aws_instance" "web" { 120 # ... 121 for_each = { 122 "terraform": "value1", 123 "resource": "value2", 124 "indexing": "value3", 125 "example": "value4", 126 } 127 } 128 ``` 129 130 An address like this: 131 132 ``` 133 aws_instance.web["example"] 134 ``` 135 136 Refers to only the "example" instance in the config.