Add references to custom conditions on related pages

This commit is contained in:
Laura Pacilio 2022-04-06 16:11:36 -04:00
parent 5803963be8
commit 04d329a9e1
6 changed files with 117 additions and 1 deletions

View File

@ -122,6 +122,30 @@ referencing the managed resource values through a `local` value.
~> **NOTE:** **In Terraform 0.12 and earlier**, due to the data resource behavior of deferring the read until the apply phase when depending on values that are not yet known, using `depends_on` with `data` resources will force the read to always be deferred to the apply phase, and therefore a configuration that uses `depends_on` with a `data` resource can never converge. Due to this behavior, we do not recommend using `depends_on` with data resources. ~> **NOTE:** **In Terraform 0.12 and earlier**, due to the data resource behavior of deferring the read until the apply phase when depending on values that are not yet known, using `depends_on` with `data` resources will force the read to always be deferred to the apply phase, and therefore a configuration that uses `depends_on` with a `data` resource can never converge. Due to this behavior, we do not recommend using `depends_on` with data resources.
## Custom Conditions
You can use `precondition` and `postcondition` blocks to specify assumptions and guarantees about how the data source operates. The following examples creates a postcondition that checks whether the AMI has the correct tags.
``` hcl
data "aws_ami" "example" {
id = var.aws_ami_id
lifecycle {
# The AMI ID must refer to an existing AMI that has the tag "nomad-server".
postcondition {
condition = self.tags["Component"] == "nomad-server"
error_message = "The selected AMI must be tagged with the
Component value \"nomad-server\"."
}
}
}
```
Custom conditions can help capture assumptions that might be only implied, helping future maintainers understand the configuration design and intent. They also return useful information about errors earlier and in context, helping consumers more easily diagnose issues in their configurations.
Refer to [Custom Conditions](/language/expressions/custom-conditions#preconditions-and-postconditions) for more details.
## Multiple Resource Instances ## Multiple Resource Instances
Data resources support [`count`](/language/meta-arguments/count) Data resources support [`count`](/language/meta-arguments/count)

View File

@ -110,6 +110,30 @@ The following arguments can be used within a `lifecycle` block:
Only attributes defined by the resource type can be ignored. Only attributes defined by the resource type can be ignored.
`ignore_changes` cannot be applied to itself or to any other meta-arguments. `ignore_changes` cannot be applied to itself or to any other meta-arguments.
## Custom Conditions
You can add `precondition` and `postcondition` blocks with a lifecycle block to specify assumptions and guarantees about how resources and data sources operate. The following examples creates a postcondition that checks whether the AMI is properly configured.
```hcl
resource "aws_instance" "example" {
instance_type = "t2.micro"
ami = "ami-abc123"
lifecycle {
# The AMI ID must refer to an AMI that contains an operating system
# for the `x86_64` architecture.
precondition {
condition = data.aws_ami.example.architecture == "x86_64"
error_message = "The selected AMI must be for the x86_64 architecture."
}
}
}
```
Custom conditions can help capture assumptions that might be only implied, helping future maintainers understand the configuration design and intent. They also return useful information about errors earlier and in context, helping consumers more easily diagnose issues in their configurations.
Refer to [Custom Conditions](/language/expressions/custom-conditions#preconditions-and-postconditions) for more details.
## Literal Values Only ## Literal Values Only
The `lifecycle` settings all affect how Terraform constructs and traverses The `lifecycle` settings all affect how Terraform constructs and traverses

View File

@ -186,6 +186,29 @@ be given inline as a single resource, but we can also compose together multiple
modules as described elsewhere on this page in situations where the modules as described elsewhere on this page in situations where the
dependencies themselves are complicated enough to benefit from abstractions. dependencies themselves are complicated enough to benefit from abstractions.
## Assumptions and Guarantees
Every module has implicit assumptions and guarantees that define what data it expects and what data it produces for consumers.
- **Assumption:** A condition that must be true in order for the configuration of a particular resource to be usable. For example, an `aws_instance` configuration can have the assumption that the given AMI will always be configured for the `x86_64` CPU architecture.
- **Guarantee:** A characteristic or behavior of an object that the rest of the configuration should be able to rely on. For example, an `aws_instance` configuration can have the guarantee that an EC2 instance will be running in a network that assigns it a private DNS record.
We recommend using [custom conditions](/language/expressions/custom-conditions) help capture and test for assumptions and guarantees. This helps future maintainers understand the configuration design and intent. They also return useful information about errors earlier and in context, helping consumers more easily diagnose issues in their configurations.
The following examples creates a precondition that checks whether the EC2 instance has an encrypted root volume.
```hcl
output "api_base_url" {
value = "https://${aws_instance.example.private_dns}:8433/"
# The EC2 instance must have an encrypted root volume.
precondition {
condition = data.aws_ebs_volume.example.encrypted
error_message = "The server's root volume is not encrypted."
}
}
```
## Multi-cloud Abstractions ## Multi-cloud Abstractions
Terraform itself intentionally does not attempt to abstract over similar Terraform itself intentionally does not attempt to abstract over similar

View File

@ -131,6 +131,30 @@ The following meta-arguments are documented on separate pages:
- [`lifecycle`, for lifecycle customizations](/language/meta-arguments/lifecycle) - [`lifecycle`, for lifecycle customizations](/language/meta-arguments/lifecycle)
- [`provisioner`, for taking extra actions after resource creation](/language/resources/provisioners/syntax) - [`provisioner`, for taking extra actions after resource creation](/language/resources/provisioners/syntax)
## Custom Conditions
You can use `precondition` and `postcondition` blocks to specify assumptions and guarantees about how the resource operates. The following examples creates a postcondition that checks whether the AMI is properly configured.
```hcl
resource "aws_instance" "example" {
instance_type = "t2.micro"
ami = "ami-abc123"
lifecycle {
# The AMI ID must refer to an AMI that contains an operating system
# for the `x86_64` architecture.
precondition {
condition = data.aws_ami.example.architecture == "x86_64"
error_message = "The selected AMI must be for the x86_64 architecture."
}
}
}
```
Custom conditions can help capture assumptions that might be only implied, helping future maintainers understand the configuration design and intent. They also return useful information about errors earlier and in context, helping consumers more easily diagnose issues in their configurations.
Refer to [Custom Conditions](/language/expressions/custom-conditions#preconditions-and-postconditions) for more details.
## Operation Timeouts ## Operation Timeouts
Some resource types provide a special `timeouts` nested block argument that Some resource types provide a special `timeouts` nested block argument that

View File

@ -62,6 +62,27 @@ In a parent module, outputs of child modules are available in expressions as
`web_server` declared an output named `instance_ip_addr`, you could access that `web_server` declared an output named `instance_ip_addr`, you could access that
value as `module.web_server.instance_ip_addr`. value as `module.web_server.instance_ip_addr`.
## Custom Conditions
You can use `precondition` blocks to specify guarantees about output data. The following examples creates a precondition that checks whether the EC2 instance has an encrypted root volume.
```hcl
output "api_base_url" {
value = "https://${aws_instance.example.private_dns}:8433/"
# The EC2 instance must have an encrypted root volume.
precondition {
condition = data.aws_ebs_volume.example.encrypted
error_message = "The server's root volume is not encrypted."
}
}
```
Custom conditions can help capture assumptions that might be only implied, helping future maintainers understand the configuration design and intent. They also return useful information about errors earlier and in context, helping consumers more easily diagnose issues in their configurations.
Refer to [Custom Conditions](/language/expressions/custom-conditions#preconditions-and-postconditions) for more details.
## Optional Arguments ## Optional Arguments
`output` blocks can optionally include `description`, `sensitive`, and `depends_on` arguments, which are described in the following sections. `output` blocks can optionally include `description`, `sensitive`, and `depends_on` arguments, which are described in the following sections.

View File

@ -173,7 +173,7 @@ variable "image_id" {
} }
} }
``` ```
Refer to [Custom Data Validation](/language/expressions/custom-validation-rules) for more details. Refer to [Custom Conditions](/language/expressions/custom-conditions#input-variable-validation) for more details.
### Suppressing Values in CLI Output ### Suppressing Values in CLI Output