Skip to content

Commit ae33b68

Browse files
cchildressapparentlymart
authored andcommitted
website: An example of referencing resources with "count"
1 parent 9d863fe commit ae33b68

1 file changed

Lines changed: 32 additions & 0 deletions

File tree

website/docs/configuration/resources.html.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,38 @@ resource "aws_instance" "app" {
229229
}
230230
```
231231

232+
To reference a particular instance of a resource you can use `resource.foo.*.id[#]` where `#` is the index number of the instance.
233+
234+
For example, to create a list of all [AWS subnet](/docs/providers/aws/r/subnet.html) ids vs referencing a specific subnet in the list you can use this syntax:
235+
236+
```hcl
237+
resource "aws_vpc" "foo" {
238+
cidr_block = "198.18.0.0/16"
239+
}
240+
241+
resource "aws_subnet" "bar" {
242+
count = 2
243+
vpc_id = "${aws_vpc.foo.id}"
244+
cidr_block = "${cidrsubnet(aws_vpc.foo.cidr_block, 8, count.index)}"
245+
}
246+
247+
output "vpc_id" {
248+
value = "${aws_vpc.foo.id}"
249+
}
250+
251+
output "all_subnet_ids" {
252+
value = "${aws_subnet.bar.*.id}"
253+
}
254+
255+
output "subnet_id_0" {
256+
value = "${aws_subnet.bar.*.id[0]}"
257+
}
258+
259+
output "subnet_id_1" {
260+
value = "${aws_subnet.bar.*.id[1]}"
261+
}
262+
```
263+
232264
## Multiple Provider Instances
233265

234266
By default, a resource targets the provider based on its type. For example

0 commit comments

Comments
 (0)