Skip to content

Commit 17911f6

Browse files
committed
lifecycle m-a ref draft
1 parent 8f914ae commit 17911f6

File tree

5 files changed

+311
-411
lines changed

5 files changed

+311
-411
lines changed

content/terraform/v1.13.x/docs/language/block/data.mdx

Lines changed: 3 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -162,12 +162,8 @@ You can specify the following lifecycle rules to manage how Terraform performs o
162162

163163
Configurations defined in the `lifecycle` block affect how Terraform constructs and traverses the dependency graph. You can only use literal values in the lifecycle block because Terraform processes them before it evaluates arbitrary expressions for a run.
164164

165-
The `lifecycle` block is a meta-argument. Meta-arguments are built-in arguments that control how Terraform creates data sources. Refer to [Meta-arguments](/terraform/language/meta-arguments) for more information.
165+
`lifecycle` is a **meta-argument**. Meta-arguments are built into the Terraform language and control how Terraform creates resources. Refer to the [`lifecycle` reference](/terraform/language/meta-arguments/lifecycle) for details about how the argument works.
166166

167-
#### Summary
168-
169-
- Data type: Block.
170-
- Default: None.
171167

172168
### `precondition`
173169

@@ -184,26 +180,7 @@ resource {
184180
}
185181
```
186182

187-
The following arguments in the `precondition` block are required:
188-
189-
| Argument | Description | Data type |
190-
| --- | --- | --- |
191-
| `condition` | Expression that must return `true` for Terraform to proceed with an operation. You can refer to any other object in the same configuration scope unless the reference creates a cyclic dependency. | Expression that can include references, strings, and operators. |
192-
| `error_message` | Message that Terraform prints to the console if the `condition` returns `false`. | String |
193-
194-
Terraform evaluates `precondition` blocks before evaluating the resource's configuration arguments. The `precondition` can take precedence over argument evaluation errors.
195-
196-
Terraform evaluates precondition blocks after evaluating [`count`](#count) and [`for_each`](#for_each) meta-arguments. As a result, Terraform can evaluate the `precondition` separately for each instance and makes the `each.key` and `count.index` objects available in the conditions.
197-
198-
You can include a `precondition` and [`postcondition` block](#postcondition) in the same resource. Do not add `precondition` blocks to a `resource` block and a `data` block that represent the same object in the same configuration. Doing so may cause Terraform to ignore changes to the `data` block that result from changes in the `resource` block.
199-
200-
Refer to [Validate your configuration](/terraform/language/validate) for information about adding validations to your Terraform configuration.
201-
202-
#### Summary
203-
204-
- Data type: Block.
205-
- Default: None.
206-
- Example: [Apply custom conditions](#apply-custom-conditions).
183+
`precondition` is a directive available in the `lifecycle` **meta-argument**. Meta-arguments are built into the Terraform language and control how Terraform creates resources. Refer to the [`lifecycle` reference](/terraform/language/meta-arguments/lifecycle) for details about how the argument works.
207184

208185
### `postcondition`
209186

@@ -220,83 +197,4 @@ data "<TYPE>" "<LABEL>" {
220197
}
221198
```
222199

223-
The following arguments in the `precondition` block are required:
224-
225-
| Argument | Description | Data type |
226-
| --- | --- | --- |
227-
| `condition` | Expression that must return `true` for Terraform to perform operations on downstream resources. You can refer to any other object in the same configuration scope unless the reference creates a cyclic dependency. | Expression that can include references, strings, and operators. |
228-
| `error_message` | Message that Terraform prints to the console if the `condition` returns `false`. | String |
229-
230-
Terraform evaluates `postcondition` blocks after planning and applying changes to the data source. Postcondition failures prevent changes to other resources that depend on the failing resource.
231-
232-
You can include a `postcondition` and [`precondition` block](#precondition) in the same resource. Do not add `postcondition` blocks to a `resource` block and a `data` block that represent the same object in the same configuration. Doing so may cause Terraform to ignore changes to the `data` block that result from changes in the `resource` block.
233-
234-
Refer to [Validate your configuration](/terraform/language/validate) for information about adding validations to your Terraform configuration.
235-
236-
#### Summary
237-
238-
- Data type: Block.
239-
- Default: None.
240-
- Example: [Apply custom conditions](#apply-custom-conditions).
241-
242-
## Examples
243-
244-
The following examples show how to write configuration for common use cases.
245-
246-
### Apply custom conditions
247-
248-
The following example includes several configurations that illustrate how to define `precondition` and `postcondition` arguments in the `lifecycle` meta-argument.
249-
250-
The following `data` block instructs Terraform to retrieve the ID of the `ami-abc123` AMI:
251-
252-
```hcl
253-
data "aws_ami" "example" {
254-
owners = ["amazon"]
255-
filter {
256-
name = "image-id"
257-
values = ["ami-abc123"]
258-
}
259-
}
260-
```
261-
262-
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:
263-
264-
```hcl
265-
resource "aws_instance" "example" {
266-
instance_type = "t3.micro"
267-
ami = data.aws_ami.example.id
268-
269-
lifecycle {
270-
precondition {
271-
condition = data.aws_ami.example.architecture == "x86_64"
272-
error_message = "The selected AMI must be for the x86_64 architecture."
273-
}
274-
275-
postcondition {
276-
condition = self.public_dns != ""
277-
error_message = "EC2 instance must be in a VPC that has public DNS hostnames enabled."
278-
}
279-
}
280-
}
281-
```
282-
283-
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.
284-
285-
```hcl
286-
data "aws_ebs_volume" "example" {
287-
filter {
288-
name = "volume-id"
289-
values = [aws_instance.example.root_block_device[0].volume_id]
290-
}
291-
lifecycle {
292-
# The EC2 instance will have an encrypted root volume.
293-
postcondition {
294-
condition = self.encrypted
295-
error_message = "The server's root volume is not encrypted."
296-
}
297-
}
298-
}
299-
output "api_base_url" {
300-
value = "https://${aws_instance.example.private_dns}:8433/"
301-
}
302-
```
200+
`postcondition` is a directive available in the `lifecycle` **meta-argument**. Meta-arguments are built into the Terraform language and control how Terraform creates resources. Refer to the [`lifecycle` reference](/terraform/language/meta-arguments/lifecycle) for details about how the argument works.

content/terraform/v1.13.x/docs/language/block/ephemeral.mdx

Lines changed: 4 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -223,12 +223,7 @@ You can specify the following lifecycle rules to manage how Terraform performs o
223223

224224
You can include both `precondition` and `postcondition` blocks in the same `lifecycle` block, and you can define multiple `precondition` and `postcondition` blocks in the same `lifecycle` block.
225225

226-
The `lifecycle` block is a meta-argument. Meta-arguments are built-in arguments that control how Terraform creates resources. Refer to [Meta-arguments](/terraform/language/meta-arguments) for more information.
227-
228-
#### Summary
229-
230-
- Data type: Block
231-
- Default: None
226+
`lifecycle` is a **meta-argument**. Meta-arguments are built into the Terraform language and control how Terraform creates resources. Refer to the [`lifecycle` reference](/terraform/language/meta-arguments/lifecycle) for details about how the argument works.
232227

233228
### `precondition`
234229

@@ -245,26 +240,7 @@ ephemeral "<TYPE>" "<LABEL>" {
245240
}
246241
```
247242

248-
The following arguments in the `precondition` block are required:
249-
250-
| Argument | Description | Data type |
251-
| --- | --- | --- |
252-
| `condition` | Expression that must return `true` for Terraform to proceed with an operation. You can refer to any other object in the same configuration scope unless the reference creates a cyclic dependency. | Expression that can include references, strings, and operators. |
253-
| `error_message` | Message that Terraform prints to the console if the `condition` returns `false`. | String |
254-
255-
Terraform evaluates `precondition` blocks before evaluating the ephemeral resource's configuration arguments. The `precondition` block errors can take precedence over argument evaluation errors.
256-
257-
Terraform evaluates precondition blocks after evaluating `count` and `for_each` meta-arguments. As a result, Terraform can evaluate the `precondition` separately for each instance and makes the `each.key` and `count.index` objects available in the conditions.
258-
259-
You can include a `precondition` and [`postcondition` block](#postcondition) in the same ephemeral resource.
260-
261-
Refer to [Test and validate](/terraform/language/validate) for information about adding validations to your Terraform configuration.
262-
263-
#### Summary
264-
265-
- Data type: Block
266-
- Default: None
267-
- Example: [Validate ephemeral resources](#validate-ephemeral-resources)
243+
`precondition` is a directive available in the `lifecycle` **meta-argument**. Meta-arguments are built into the Terraform language and control how Terraform creates resources. Refer to the [`lifecycle` reference](/terraform/language/meta-arguments/lifecycle) for details about how the argument works.
268244

269245
### `postcondition`
270246

@@ -281,24 +257,7 @@ ephemeral "<TYPE>" "<LABEL>" {
281257
}
282258
```
283259

284-
The following arguments in the `postcondition` block are required:
285-
286-
| Argument | Description | Data type |
287-
| --- | --- | --- |
288-
| `condition` | Expression that must return `true` for Terraform to perform operations on downstream resources. You can refer to any other object in the same configuration scope unless the reference creates a cyclic dependency. | Expression that can include references, strings, and operators. |
289-
| `error_message` | Message that Terraform prints to the console if the `condition` returns `false`. | String |
290-
291-
Terraform evaluates `postcondition` blocks after planning and applying changes to the ephemeral resource. Postcondition failures prevent changes to other resources that depend on the failing ephemeral resource.
292-
293-
You can include a `postcondition` and [`precondition` block](#precondition) in the same ephemeral resource.
294-
295-
Refer to [Test and validate](/terraform/language/validate) for information about adding validations to your Terraform configuration.
296-
297-
#### Summary
298-
299-
- Data type: Block
300-
- Default: None
301-
- Example: [Validate ephemeral resources](#validate-ephemeral-resources)
260+
`postcondition` is a directive available in the `lifecycle` **meta-argument**. Meta-arguments are built into the Terraform language and control how Terraform creates resources. Refer to the [`lifecycle` reference](/terraform/language/meta-arguments/lifecycle) for details about how the argument works.
302261

303262
## Examples
304263

@@ -363,42 +322,4 @@ resource "aws_db_instance" "example" {
363322

364323
Neither write-only arguments nor `ephemeral` resources are persisted outside of the current Terraform run, ensuring that the `ephemeral.random_password.db_password.result` value is completely omitted from state and plan files.
365324

366-
Terraform does not store the generated value of `ephemeral.random_password.db_password.result`, but you can capture it in another resource to ensure the value is not lost. For an example of generating, storing, retrieving, and using an ephemeral password, refer to [write-only arguments](/terraform/language/manage-sensitive-data/write-only#set-and-store-an-ephemeral-password-in-aws-secrets-manager).
367-
368-
369-
### Validate ephemeral resources
370-
371-
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:
372-
373-
<CodeBlockConfig highlight="15-25">
374-
375-
```hcl
376-
variable "environment" {
377-
description = "Deployment environment"
378-
type = string
379-
}
380-
381-
variable "compliance_mode" {
382-
description = "Enable compliance requirements for production"
383-
type = bool
384-
default = false
385-
}
386-
387-
ephemeral "aws_ssm_parameter" "database_password" {
388-
name = "/secrets/${var.environment}/database/password"
389-
390-
lifecycle {
391-
precondition {
392-
condition = var.environment != "prod" || var.compliance_mode == true
393-
error_message = "Enable compliance mode to assess production secrets."
394-
}
395-
396-
postcondition {
397-
condition = can(regex("^[A-Za-z0-9!@#$%^&*()_+=-]{16,}$", self.value))
398-
error_message = "Password from external source must meet security requirements."
399-
}
400-
}
401-
}
402-
```
403-
404-
</CodeBlockConfig>
325+
Terraform does not store the generated value of `ephemeral.random_password.db_password.result`, but you can capture it in another resource to ensure the value is not lost. For an example of generating, storing, retrieving, and using an ephemeral password, refer to [write-only arguments](/terraform/language/manage-sensitive-data/write-only#set-and-store-an-ephemeral-password-in-aws-secrets-manager).

0 commit comments

Comments
 (0)