You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/terraform/v1.13.x/docs/language/meta-arguments/count.mdx
+4-7Lines changed: 4 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -19,12 +19,6 @@ You can use the `count` argument as a conditional for creating resources. For ex
19
19
20
20
</Tip>
21
21
22
-
### `count` object
23
-
24
-
In blocks where `count` is set, an additional `count` object is available in expressions, so you can modify the configuration of each instance. This object has the following attribute:
25
-
26
-
-`count.index`: The distinct index number starting with `0` corresponding
27
-
to this instance.
28
22
29
23
In the folllowing example, Terraform creates four `aws_instance.server` resources that use the same AMI and instance type. The `count.index` attribute gives each `aws_instance` a unique `Name` tag:
30
24
@@ -47,6 +41,10 @@ The `count` meta-argument accepts numeric [expressions](/terraform/language/expr
47
41
48
42
### Referring to instances
49
43
44
+
In blocks where `count` is set, Terraform creates an additional `count` object that you can use in expressions to modify the configuration of each instance. This object has the following attribute:
45
+
46
+
-`count.index`: The distinct index number starting with `0` corresponding to this instance.
47
+
50
48
Terraform makes a distinction between the block containing the `count` argument and the instances associated with it. Terraform identifies instances index number starting at `0`.
51
49
52
50
-`<TYPE>.<NAME>` or `module.<NAME>`, for example, `aws_instance.server` refers to the resource block.
@@ -62,7 +60,6 @@ the module index as the module's name suffices to reference the module.
62
60
63
61
Within nested [`provisioner`](/terraform/language/block/resource#provisioner) or [`connection`](/terraform/language/block/resource#connection) blocks, the special `self` object refers to the current resource instance, not the `resource` block as a whole.
64
62
65
-
66
63
### How to choose between `count` and `for_each`
67
64
68
65
`count` and [`for_each`](/terraform/language/meta-arguments/for_each) perform a similar function. Use the `count` argument when you want to create nearly identical instances. Use `for_each` when some instance arguments must have distinct values that can't be directly derived from an
In blocks where `for_each` is set, an additional `each` object is
43
-
available in expressions, so you can modify the configuration of each instance.
44
-
This object has the following attributes:
45
-
46
-
-`each.key`: The map key or set member corresponding to this instance.
47
-
-`each.value`: The map value corresponding to this instance. If a set is provided, this is the same as `each.key`.
48
-
49
40
### Limitations on values
50
41
51
42
The keys of the map or all values in a set of strings must be known values. Otherwise, Terraform returns an error message that `for_each` has dependencies that cannot be determined before apply and that a `-target` may be needed.
@@ -73,6 +64,29 @@ To prevent unexpected behavior during conversion, the `for_each` argument does n
73
64
-[Transform a multi-level nested structure into a flat list](/terraform/language/functions/flatten#flattening-nested-structures-for-for_each).
74
65
-[Combine collections to produce a list of element combinations](/terraform/language/functions/setproduct#finding-combinations-for-for_each).
75
66
67
+
### Referring to instances
68
+
69
+
In blocks where `for_each` is set, Terraform creates an additional `each` object that you can use in expressions to modify the configuration of each instance.
70
+
This object has the following attributes:
71
+
72
+
-`each.key`: The map key or set member corresponding to this instance.
73
+
-`each.value`: The map value corresponding to this instance. If a set is provided, this is the same as `each.key`.
74
+
75
+
Terraform makes a distinction between the block containing the `for_each` argument and the instances associated with it. Terraform identifies instances by the map key or set member using the value provided to `for_each`.
76
+
77
+
-`<TYPE>.<NAME>` or `module.<NAME>`: For example, `azurerm_resource_group.rg` refers to the block.
78
+
-`<TYPE>.<NAME>[<KEY>]` or `module.<NAME>[<KEY>]`: For example, `azurerm_resource_group.rg["a_group"]` and `azurerm_resource_group.rg["another_group"]` refer to individual instances.
79
+
80
+
This is different from resources and modules without [`count`](/terraform/language/meta-arguments/count) or `for_each`, which can be
81
+
referenced without an index or key.
82
+
83
+
Similarly, resources from child modules with multiple instances are prefixed
84
+
with `module.<NAME>[<KEY>]` when displayed in plan output and elsewhere in the UI.
85
+
For a module without `count` or `for_each`, the address does not contain
86
+
the module index as the module's name suffices to reference the module.
87
+
88
+
Within nested [`provisioner`](/terraform/language/block/resource#provisioner) or [`connection`](/terraform/language/block/resource#connection) blocks, the special `self` object refers to the current resource instance, not the resource block as a whole.
89
+
76
90
### Chain `for_each` between resources
77
91
78
92
Because a resource using `for_each` appears as a map of objects when used in
@@ -126,23 +140,6 @@ Terraform to expect the instance keys for both to always change together,
126
140
and typically also makes the configuration easier to understand for human
127
141
maintainers.
128
142
129
-
### Referring to instances
130
-
131
-
Terraform makes a distinction between the block containing the `for_each` argument and the instances associated with it. Terraform identifies instances by the map key or set member using the value provided to `for_each`.
132
-
133
-
-`<TYPE>.<NAME>` or `module.<NAME>`: For example, `azurerm_resource_group.rg` refers to the block.
134
-
-`<TYPE>.<NAME>[<KEY>]` or `module.<NAME>[<KEY>]`: For example, `azurerm_resource_group.rg["a_group"]` and `azurerm_resource_group.rg["another_group"]` refer to individual instances.
135
-
136
-
This is different from resources and modules without [`count`](/terraform/language/meta-arguments/count) or `for_each`, which can be
137
-
referenced without an index or key.
138
-
139
-
Similarly, resources from child modules with multiple instances are prefixed
140
-
with `module.<NAME>[<KEY>]` when displayed in plan output and elsewhere in the UI.
141
-
For a module without `count` or `for_each`, the address does not contain
142
-
the module index as the module's name suffices to reference the module.
143
-
144
-
Within nested [`provisioner`](/terraform/language/block/resource#provisioner) or [`connection`](/terraform/language/block/resource#connection) blocks, the special `self` object refers to the current resource instance, not the resource block as a whole.
145
-
146
143
### Using sets
147
144
148
145
The Terraform language doesn't have a literal syntax for [set values](/terraform/language/expressions/type-constraints#collection-types), but you can use the [`toset` function](/terraform/language/functions/toset) to explicitly convert a list of strings to a set:
The following example includes several configurations that illustrate how to define `precondition` and `postcondition`arguments in the `lifecycle` meta-argument.
204
+
In the following example, the `precondition` block ensures that the AMI ID retrieved from the `data` block includes `x86_64` as its `architecture` attribute. The `postcondition`block specifies that the EC2 instance must be allocated a public DNS hostname. When either condition is not met, Terraform returns the `error_message` for the failed condition:
205
205
206
-
The following `data` block instructs Terraform to retrieve the ID of the `ami-abc123` AMI:
207
206
208
207
```hcl
209
208
data "aws_ami" "example" {
209
+
most_recent= true
210
210
owners = ["amazon"]
211
211
filter {
212
212
name = "image-id"
213
-
values = ["ami-abc123"]
213
+
values = ["ami-*"]
214
214
}
215
215
}
216
-
```
217
-
218
-
In the following code, the `precondition` block specifies that the AMI ID retrieved from the `data` block must include `x86_64` as its `architecture` attribute. The `postcondition` block specifies that the EC2 instance must be allocated a public DNS hostname. When either condition is not met, Terraform returns the `error_message` for the failed condition:
### Validate that a root storage volume is encrypted
238
235
239
-
The following `data` block retrieves the root storage volume connected to the `aws_instance.example` EC2 instance using the `volume_id` attribute. When a `data` resource verifies the result of a managed resource declared in the same configuration, you must define the check in a `postcondition` block in the resource so that Terraform waits for changes to the managed resource to complete before reading the data resource.
236
+
In the following example, the`data` block retrieves the root storage volume connected to the `aws_instance.example` EC2 instance using the `volume_id` attribute. When a `data` resource verifies the result of a managed resource declared in the same configuration, you must define the check in a `postcondition` block in the resource so that Terraform waits for changes to the managed resource to complete before reading the data resource.
240
237
241
238
```hcl
242
239
data "aws_ebs_volume" "example" {
@@ -252,11 +249,39 @@ data "aws_ebs_volume" "example" {
252
249
}
253
250
}
254
251
}
252
+
253
+
data "aws_ami" "example" {
254
+
most_recent= true
255
+
owners = ["amazon"]
256
+
filter {
257
+
name = "image-id"
258
+
values = ["ami-*"]
259
+
}
260
+
}
261
+
262
+
resource "aws_instance" "example" {
263
+
instance_type = "t3.micro"
264
+
ami = data.aws_ami.example.id
265
+
}
266
+
255
267
output "api_base_url" {
256
268
value = "https://${aws_instance.example.private_dns}:8433/"
257
269
}
258
270
```
259
271
272
+
The validation fails in this example, so Terraform prints the following message to the console:
273
+
274
+
```shell-session
275
+
│ Error: Resource postcondition failed
276
+
│
277
+
│ on main.tf line 31, in data "aws_ebs_volume" "example":
278
+
│ 31: condition = self.encrypted
279
+
│ ├────────────────
280
+
│ │ self.encrypted is false
281
+
│
282
+
│ The server's root volume is not encrypted.
283
+
```
284
+
260
285
### Validate ephemeral resources
261
286
262
287
In the following example, the `aws_ssm_parameter` ephemeral resource has a `precondition` to ensure that compliance mode is enabled to secure production secrets, and a `postcondition` to ensure the generated password meets password requirements:
0 commit comments