Skip to content

Commit dec0c46

Browse files
author
Paul Hinze
committed
docs: add example of using variables with count
closes hashicorp#861
1 parent 5dce9a9 commit dec0c46

1 file changed

Lines changed: 32 additions & 3 deletions

File tree

website/source/docs/configuration/resources.html.md

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ resource type in the
4444
There are **meta-parameters** available to all resources:
4545

4646
* `count` (int) - The number of identical resources to create.
47-
This doesn't apply to all resources. You can use the `${count.index}`
48-
[interpolation](/docs/configuration/interpolation.html) to reference
49-
the current count index in your resource.
47+
This doesn't apply to all resources. For details on using variables in
48+
conjunction with count, see [Using Variables with
49+
`count`](#using-variables-with-count) below.
5050

5151
* `depends_on` (list of strings) - Explicit dependencies that this
5252
resource has. These dependencies will be created before this
@@ -94,6 +94,35 @@ provide more specific connection info for a specific provisioner.
9494
An example use case might be to use a different user to log in
9595
for a single provisioner.
9696

97+
<a id="using-variables-with-count"></a>
98+
99+
## Using Variables With `count`
100+
101+
When declaring multiple instances of a resource using [`count`](#count), it is
102+
common to want each instance to have a different value for a given attribute.
103+
104+
You can use the `${count.index}`
105+
[interpolation](/docs/configuration/interpolation.html) along with a mapping [variable](/docs/configuration/variables.html) to accomplish this.
106+
107+
For example, here's how you could create three [AWS Instances](/docs/providers/aws/r/instance.html) each with their own static IP
108+
address:
109+
110+
```
111+
variable "instance_ips" {
112+
default = {
113+
"0" = "10.11.12.100"
114+
"1" = "10.11.12.101"
115+
"2" = "10.11.12.102"
116+
}
117+
}
118+
119+
resource "aws_instance" "app" {
120+
count = "3"
121+
private_ip = "${lookup(instance_ips, count.index)}"
122+
# ...
123+
}
124+
```
125+
97126
## Syntax
98127

99128
The full syntax is:

0 commit comments

Comments
 (0)