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