2015-04-01 01:48:54 +02:00
|
|
|
---
|
2021-12-15 03:41:17 +01:00
|
|
|
page_title: 'Internals: Resource Address'
|
2015-04-01 01:48:54 +02:00
|
|
|
description: |-
|
2021-04-07 21:25:59 +02:00
|
|
|
A resource address is a string that identifies zero or more resource
|
|
|
|
instances in your overall configuration.
|
2015-04-01 01:48:54 +02:00
|
|
|
---
|
|
|
|
|
|
|
|
# Resource Addressing
|
|
|
|
|
2021-04-07 21:25:59 +02:00
|
|
|
A _resource address_ is a string that identifies zero or more resource
|
|
|
|
instances in your overall configuration.
|
|
|
|
|
|
|
|
An address is made up of two parts:
|
2015-04-01 01:48:54 +02:00
|
|
|
|
|
|
|
```
|
2015-05-01 18:01:49 +02:00
|
|
|
[module path][resource spec]
|
2015-04-01 01:48:54 +02:00
|
|
|
```
|
|
|
|
|
2021-04-07 21:25:59 +02:00
|
|
|
In some contexts Terraform might allow for an incomplete resource address that
|
|
|
|
only refers to a module as a whole, or that omits the index for a
|
|
|
|
multi-instance resource. In those cases, the meaning depends on the context,
|
|
|
|
so you'll need to refer to the documentation for the specific feature you
|
|
|
|
are using which parses resource addresses.
|
|
|
|
|
2020-04-13 18:13:18 +02:00
|
|
|
## Module path
|
2015-04-01 01:48:54 +02:00
|
|
|
|
2015-05-01 18:01:49 +02:00
|
|
|
A module path addresses a module within the tree of modules. It takes the form:
|
|
|
|
|
|
|
|
```
|
2020-04-10 21:11:27 +02:00
|
|
|
module.module_name[module index]
|
2015-05-01 18:01:49 +02:00
|
|
|
```
|
|
|
|
|
2021-12-15 03:41:17 +01:00
|
|
|
- `module` - Module keyword indicating a child module (non-root). Multiple `module`
|
|
|
|
keywords in a path indicate nesting.
|
|
|
|
- `module_name` - User-defined name of the module.
|
|
|
|
- `[module index]` - (Optional) [Index](#index-values-for-modules-and-resources)
|
|
|
|
to select an instance from a module call that has multiple instances,
|
|
|
|
surrounded by square bracket characters (`[` and `]`).
|
2020-04-10 21:11:27 +02:00
|
|
|
|
|
|
|
An address without a resource spec, i.e. `module.foo` applies to every resource within
|
|
|
|
the module if a single module, or all instances of a module if a module has multiple instances.
|
|
|
|
To address all resources of a particular module instance, include the module index in the address,
|
|
|
|
such as `module.foo[0]`.
|
|
|
|
|
|
|
|
If the module path is omitted, the address applies to the root module.
|
|
|
|
|
2020-04-13 18:13:18 +02:00
|
|
|
An example of the `module` keyword delineating between two modules that have multiple instances:
|
|
|
|
|
|
|
|
```
|
|
|
|
module.foo[0].module.bar["a"]
|
|
|
|
```
|
|
|
|
|
2021-04-07 21:25:59 +02:00
|
|
|
-> Module index only applies to modules in Terraform v0.13 or later. In earlier
|
2020-04-10 21:11:27 +02:00
|
|
|
versions of Terraform, a module could not have multiple instances.
|
2015-04-01 01:48:54 +02:00
|
|
|
|
2020-04-13 18:13:18 +02:00
|
|
|
## Resource spec
|
2015-04-01 01:48:54 +02:00
|
|
|
|
2021-04-07 21:25:59 +02:00
|
|
|
A resource spec addresses a specific resource instance in the selected module.
|
|
|
|
It has the following syntax:
|
2015-05-01 18:01:49 +02:00
|
|
|
|
|
|
|
```
|
2021-04-07 21:25:59 +02:00
|
|
|
resource_type.resource_name[instance index]
|
2015-05-01 18:01:49 +02:00
|
|
|
```
|
|
|
|
|
2021-12-15 03:41:17 +01:00
|
|
|
- `resource_type` - Type of the resource being addressed.
|
|
|
|
- `resource_name` - User-defined name of the resource.
|
|
|
|
- `[instance index]` - (Optional) [Index](#index-values-for-modules-and-resources)
|
|
|
|
to select an instance from a resource that has multiple instances,
|
|
|
|
surrounded by square bracket characters (`[` and `]`).
|
2015-04-01 01:48:54 +02:00
|
|
|
|
2019-03-18 19:49:30 +01:00
|
|
|
-> In Terraform v0.12 and later, a resource spec without a module path prefix
|
|
|
|
matches only resources in the root module. In earlier versions, a resource spec
|
2021-04-07 21:25:59 +02:00
|
|
|
without a module path prefix would match resources with the same type and name
|
2019-03-18 19:49:30 +01:00
|
|
|
in any descendent module.
|
2015-04-01 01:48:54 +02:00
|
|
|
|
2020-04-13 18:13:18 +02:00
|
|
|
## Index values for Modules and Resources
|
2020-04-10 21:11:27 +02:00
|
|
|
|
|
|
|
The following specifications apply to index values on modules and resources with multiple instances:
|
2019-08-03 01:38:13 +02:00
|
|
|
|
2021-12-15 03:41:17 +01:00
|
|
|
- `[N]` where `N` is a `0`-based numerical index into a resource with multiple
|
|
|
|
instances specified by the `count` meta-argument. Omitting an index when
|
|
|
|
addressing a resource where `count > 1` means that the address references
|
|
|
|
all instances.
|
|
|
|
- `["INDEX"]` where `INDEX` is a alphanumerical key index into a resource with
|
|
|
|
multiple instances specified by the `for_each` meta-argument.
|
2019-08-03 01:38:13 +02:00
|
|
|
|
2015-04-01 01:48:54 +02:00
|
|
|
## Examples
|
|
|
|
|
2019-08-03 01:38:13 +02:00
|
|
|
### count Example
|
|
|
|
|
2015-04-01 01:48:54 +02:00
|
|
|
Given a Terraform config that includes:
|
|
|
|
|
2017-04-17 12:17:54 +02:00
|
|
|
```hcl
|
2015-04-01 01:48:54 +02:00
|
|
|
resource "aws_instance" "web" {
|
|
|
|
# ...
|
|
|
|
count = 4
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
An address like this:
|
|
|
|
|
|
|
|
```
|
|
|
|
aws_instance.web[3]
|
|
|
|
```
|
|
|
|
|
|
|
|
Refers to only the last instance in the config, and an address like this:
|
|
|
|
|
|
|
|
```
|
|
|
|
aws_instance.web
|
|
|
|
```
|
|
|
|
|
|
|
|
Refers to all four "web" instances.
|
2019-08-03 01:38:13 +02:00
|
|
|
|
|
|
|
### for_each Example
|
|
|
|
|
|
|
|
Given a Terraform config that includes:
|
|
|
|
|
|
|
|
```hcl
|
|
|
|
resource "aws_instance" "web" {
|
|
|
|
# ...
|
|
|
|
for_each = {
|
|
|
|
"terraform": "value1",
|
|
|
|
"resource": "value2",
|
|
|
|
"indexing": "value3",
|
|
|
|
"example": "value4",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
An address like this:
|
|
|
|
|
|
|
|
```
|
|
|
|
aws_instance.web["example"]
|
|
|
|
```
|
|
|
|
|
2019-08-08 14:56:57 +02:00
|
|
|
Refers to only the "example" instance in the config.
|