Skip to content

Commit c78924a

Browse files
committed
update examples and other tweaks
1 parent 3f56933 commit c78924a

File tree

4 files changed

+65
-89
lines changed

4 files changed

+65
-89
lines changed

content/terraform/v1.13.x/docs/language/meta-arguments/count.mdx

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,6 @@ You can use the `count` argument as a conditional for creating resources. For ex
1919

2020
</Tip>
2121

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.
2822

2923
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:
3024

@@ -47,6 +41,10 @@ The `count` meta-argument accepts numeric [expressions](/terraform/language/expr
4741

4842
### Referring to instances
4943

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+
5048
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`.
5149

5250
- `<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.
6260

6361
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.
6462

65-
6663
### How to choose between `count` and `for_each`
6764

6865
`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

content/terraform/v1.13.x/docs/language/meta-arguments/depends_on.mdx

Lines changed: 2 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -81,51 +81,9 @@ You can use `depends_on` in the following Terraform configuration blocks:
8181
- [`resource` blocks](/terraform/language/block/resource)
8282

8383
## Example use cases
84-
<!-- TODO: refine examples -->
85-
The following use cases describe common patterns for the `depends_on` argument.
86-
87-
### Specify a dependency on another `resource` block
88-
89-
In the following example, the `aws_iam_instance_profile` resource references the `aws_iam_role` resource, instructing Terraform to create the upstream resource first. The `aws_iam_role_policy` resource configures an IAM policy that lets the EC2 instance access the S3 API.
90-
91-
Terraform infers that it must create the instance profile before the EC2 instance, but it cannot infer that the software running in the EC2 instance needs access to the S3 API in order to boot properly. As a result, the configuration explicitly instructs Terraform to create the `aws_iam_role_policy` first:
92-
93-
```hcl
94-
resource "aws_iam_role" "example" {
95-
name = "example"
96-
assume_role_policy = "..."
97-
}
98-
99-
resource "aws_iam_instance_profile" "example" {
100-
role = aws_iam_role.example.name
101-
}
102-
103-
resource "aws_iam_role_policy" "example" {
104-
name = "example"
105-
role = aws_iam_role.example.name
106-
policy = jsonencode({
107-
"Statement" = [{
108-
"Action" = "s3:*",
109-
"Effect" = "Allow",
110-
}],
111-
})
112-
}
11384

114-
resource "aws_instance" "example" {
115-
ami = "data.aws_ami.example.id"
116-
instance_type = "t2.micro"
117-
iam_instance_profile = aws_iam_instance_profile.example
118-
depends_on = [
119-
aws_iam_role_policy.example
120-
]
121-
}
122-
123-
data "aws_ami" "example" {
124-
# AMI configuration
125-
}
126-
```
85+
The following use cases describe common patterns for the `depends_on` argument.
12786

128-
Note that the `aws_iam_role` resource is partially defined. Refer to the [AWS provider documentation](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role) for all configuration settings.
12987

13088
### Specify a dependency during validation
13189

@@ -195,7 +153,7 @@ resource "aws_instance" "api" {
195153
196154
depends_on = [aws_db_instance.api]
197155
}
198-
156+
```
199157

200158
### Specify a dependency on the parent module
201159

content/terraform/v1.13.x/docs/language/meta-arguments/for_each.mdx

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,6 @@ resource "aws_iam_user" "the-accounts" {
3737
}
3838
```
3939

40-
### `each` object
41-
42-
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-
4940
### Limitations on values
5041

5142
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
7364
- [Transform a multi-level nested structure into a flat list](/terraform/language/functions/flatten#flattening-nested-structures-for-for_each).
7465
- [Combine collections to produce a list of element combinations](/terraform/language/functions/setproduct#finding-combinations-for-for_each).
7566

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+
7690
### Chain `for_each` between resources
7791

7892
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,
126140
and typically also makes the configuration easier to understand for human
127141
maintainers.
128142

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-
146143
### Using sets
147144

148145
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:
@@ -183,8 +180,7 @@ variable "subnet_ids" {
183180
184181
resource "aws_instance" "server" {
185182
for_each = var.subnet_ids
186-
187-
# (and the other arguments as above)
183+
#...
188184
}
189185
```
190186

content/terraform/v1.13.x/docs/language/meta-arguments/lifecycle.mdx

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ In the following example, Terraform replaces `aws_appautoscaling_target` each ti
190190

191191
```hcl
192192
resource "aws_appautoscaling_target" "ecs_target" {
193-
...
193+
# ...
194194
lifecycle {
195195
replace_triggered_by = [
196196
aws_ecs_service.svc.id
@@ -199,25 +199,21 @@ resource "aws_appautoscaling_target" "ecs_target" {
199199
}
200200
```
201201

202-
### Apply custom conditions
202+
### Validate the AMI for creating instances
203203

204-
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:
205205

206-
The following `data` block instructs Terraform to retrieve the ID of the `ami-abc123` AMI:
207206

208207
```hcl
209208
data "aws_ami" "example" {
209+
most_recent= true
210210
owners = ["amazon"]
211211
filter {
212212
name = "image-id"
213-
values = ["ami-abc123"]
213+
values = ["ami-*"]
214214
}
215215
}
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:
219216
220-
```hcl
221217
resource "aws_instance" "example" {
222218
instance_type = "t3.micro"
223219
ami = data.aws_ami.example.id
@@ -235,8 +231,9 @@ resource "aws_instance" "example" {
235231
}
236232
}
237233
```
234+
### Validate that a root storage volume is encrypted
238235

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.
240237

241238
```hcl
242239
data "aws_ebs_volume" "example" {
@@ -252,11 +249,39 @@ data "aws_ebs_volume" "example" {
252249
}
253250
}
254251
}
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+
255267
output "api_base_url" {
256268
value = "https://${aws_instance.example.private_dns}:8433/"
257269
}
258270
```
259271

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+
260285
### Validate ephemeral resources
261286

262287
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

Comments
 (0)