Skip to content

Commit c4c4c8f

Browse files
authored
Merge pull request #400 from hashicorp/docs/fix-cv-checks-example
docs: fix cv/check example that doesn't work as expected
2 parents fac796c + 07d1671 commit c4c4c8f

File tree

1 file changed

+44
-18
lines changed

1 file changed

+44
-18
lines changed

website/docs/cloud-docs/workspaces/health.mdx

+44-18
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,18 @@ You can use one of the following approaches to correct workspace drift:
109109

110110
## Continuous Validation
111111

112-
Continuous validation will evaluate preconditions, postconditions, and check blocks as part of an assessment, but we recommend using [`check` blocks](/terraform/language/checks) for post-apply monitoring. Use `check` blocks to create custom rules to validate your infrastructure's resources, data sources, and outputs.
112+
Continuous validation regularly verifies whether your configuration’s custom assertions continue to pass, validating your infrastructure. For example, you can monitor whether your website returns an expected status code, or whether an API gateway certificate is valid. Identifying failed assertions helps you resolve the failure and prevent errors during your next time Terraform operation.
113113

114-
Continuous validation lets Terraform Cloud regularly verify whether your workspace’s custom assertions continue to pass, validating your real-world infrastructure. For example, you can monitor if your website returns an expected status code, or monitor whether an API gateway certificate is valid.
114+
Continuous validation evaluates preconditions, postconditions, and check blocks as part of an assessment, but we recommend using [check blocks](https://developer.hashicorp.com/terraform/language/checks) for post-apply monitoring. Use check blocks to create custom rules to validate your infrastructure's resources, data sources, and outputs.
115+
116+
### Preventing false positives
117+
118+
Health assessments create a speculative plan to access the current state of your infrastructure. Terraform evaluates any check blocks in your configuration as the last step of creating the speculative plan. If your configuration relies on data sources and the values queried by a data source change between the time of your last run and the assessment, the speculative plan will include those changes. Terraform Cloud will not modify your infrastructure as part of an assessment, but it can use those updated values to evaluate checks. This may lead to false positive results for alerts since your infrastructure did not yet change.
119+
120+
To ensure your checks evaluate the current state of your configuration instead of against a possible future change, use nested data sources that query your actual resource configuration, rather than a computed latest value.
121+
Refer to the [AMI image
122+
scenario](#asserting-up-to-date-amis-for-compute-instances) below for an
123+
example.
115124

116125
Continuous validation alerts you whenever an assertion fails, so you can change your configuration and avoid errors the next time you update infrastructure.
117126

@@ -138,9 +147,28 @@ check "health_check" {
138147

139148
Continuous Validation alerts you if the website returns any status code besides `200` while Terraform evaluates this assertion. You can also find failures in your workspace's [Continuous Validation Results](#view-continuous-validation-results) page. You can configure continuous validation alerts in your workspace's [notification settings](/terraform/cloud-docs/workspaces/settings/notifications).
140149

150+
#### Monitoring certificate expiration
151+
152+
[Vault](https://www.vaultproject.io/) lets you secure, store, and tightly control access to tokens, passwords, certificates, encryption keys, and other sensitive data. The following example uses a `check` block to monitor for the expiration of a Vault certificate.
153+
154+
```hcl
155+
resource "vault_pki_secret_backend_cert" "app" {
156+
backend = vault_mount.intermediate.path
157+
name = vault_pki_secret_backend_role.test.name
158+
common_name = "app.my.domain"
159+
}
160+
161+
check "certificate_valid" {
162+
assert {
163+
condition = !vault_pki_secret_backend_cert.app.renew_pending
164+
error_message = "Vault cert is ready to renew."
165+
}
166+
}
167+
```
168+
141169
#### Asserting up-to-date AMIs for compute instances
142170

143-
[HCP Packer](/hcp/docs/packer) stores metadata about your [Packer](https://www.packer.io/) images. The following example postcondition fails when there is a newer AMI version available.
171+
[HCP Packer](/hcp/docs/packer) stores metadata about your [Packer](https://www.packer.io/) images. The following example check fails when there is a newer AMI version available.
144172

145173
```hcl
146174
data "hcp_packer_image" "hashiapp_image" {
@@ -157,34 +185,32 @@ resource "aws_instance" "hashiapp" {
157185
subnet_id = aws_subnet.hashiapp.id
158186
vpc_security_group_ids = [aws_security_group.hashiapp.id]
159187
key_name = aws_key_pair.generated_key.key_name
188+
189+
tags = {
190+
Name = "hashiapp"
191+
}
160192
}
161193
162194
check "ami_version_check" {
195+
data "aws_instance" "hashiapp_current" {
196+
instance_tags = {
197+
Name = "hashiapp"
198+
}
199+
}
200+
163201
assert {
164202
condition = aws_instance.hashiapp.ami == data.hcp_packer_image.hashiapp_image.cloud_image_id
165203
error_message = "Must use the latest available AMI, ${data.hcp_packer_image.hashiapp_image.cloud_image_id}."
166204
}
167205
}
168206
```
169207

170-
#### Monitoring certificate expiration
208+
In this example, the check block includes a data source that verifies the actual running instance's AMI, rather than the value of the `ami` field in the `aws_instance.hashiapp` resource. If your team publishes a new AMI between the time of the last run and an assessment, this still ensures that the check depends on the actual AMI your instance uses now.
171209

172-
[Vault](https://www.vaultproject.io/) lets you secure, store, and tightly control access to tokens, passwords, certificates, encryption keys, and other sensitive data. The following example uses a `check` block to monitor for the expiration of a Vault certificate.
210+
Check blocks execute as the last step of a plan or apply after Terraform has planned or provisioned your infrastructure. Each health assessment run performs a speculative plan in its normal course of operation. In practice, this means that check assertions are evaluated against a hypothetical future state where any planned changes have already been applied to the resources under management.
173211

174-
```hcl
175-
resource "vault_pki_secret_backend_cert" "app" {
176-
backend = vault_mount.intermediate.path
177-
name = vault_pki_secret_backend_role.test.name
178-
common_name = "app.my.domain"
179-
}
212+
To evaluate an assertion against the current state of a resource, use a nested data source that is independent from the other resources in your configuration. In the previous example, a data source is used to locate the image provisioned by the `aws_instance.hashiapp` resource. This allows the check to assert against the current image in use, rather than the image that will be used after another round of plan/apply is executed.
180213

181-
check "certificate_valid" {
182-
assert {
183-
condition = !vault_pki_secret_backend_cert.app.renew_pending
184-
error_message = "Vault cert is ready to renew."
185-
}
186-
}
187-
```
188214

189215
### View continuous validation results
190216

0 commit comments

Comments
 (0)