From ae33b68e6896df39523d24db12544ff66e9ff469 Mon Sep 17 00:00:00 2001 From: Cameron Childress Date: Mon, 4 Dec 2017 12:43:40 -0500 Subject: [PATCH] website: An example of referencing resources with "count" --- website/docs/configuration/resources.html.md | 32 ++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/website/docs/configuration/resources.html.md b/website/docs/configuration/resources.html.md index f14b3854b..748237167 100644 --- a/website/docs/configuration/resources.html.md +++ b/website/docs/configuration/resources.html.md @@ -229,6 +229,38 @@ resource "aws_instance" "app" { } ``` +To reference a particular instance of a resource you can use `resource.foo.*.id[#]` where `#` is the index number of the instance. + +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: + +```hcl +resource "aws_vpc" "foo" { + cidr_block = "198.18.0.0/16" +} + +resource "aws_subnet" "bar" { + count = 2 + vpc_id = "${aws_vpc.foo.id}" + cidr_block = "${cidrsubnet(aws_vpc.foo.cidr_block, 8, count.index)}" +} + +output "vpc_id" { + value = "${aws_vpc.foo.id}" +} + +output "all_subnet_ids" { + value = "${aws_subnet.bar.*.id}" +} + +output "subnet_id_0" { + value = "${aws_subnet.bar.*.id[0]}" +} + +output "subnet_id_1" { + value = "${aws_subnet.bar.*.id[1]}" +} +``` + ## Multiple Provider Instances By default, a resource targets the provider based on its type. For example