Check Blocks
Check blocks validate infrastructure as a whole without stopping Terraform. This lesson covers their syntax, scoped data sources and how they compare with other custom conditions.
Learning outcomes
After completing this lesson, you will be able to:
- Describe check blocks and scoped data sources.
- Explain how failed checks differ from other failed conditions.
Declaring a check
A check block is a top-level block, introduced in Terraform 1.5, that holds one or more assert blocks, each with a condition and an error message. It can also hold one scoped data source, a data block that exists only inside the check.
check "website_health" {
data "http" "home" {
url = "https://${aws_lb.web.dns_name}/health"
}
assert {
condition = data.http.home.status_code == 200
error_message = "${data.http.home.url} returned status ${data.http.home.status_code}."
}
}This check uses the http provider's data source to request the load balancer's health endpoint and asserts that the status code is 200. The scoped data source cannot be referenced from outside the check, and assertions can refer to any object in the module.
Inside a check block
How checks behave
Terraform evaluates check blocks as the last step of every plan and apply. A failed assertion produces a warning, not an error: the plan or apply completes, and the warning tells the operator that something needs attention. If the scoped data source itself fails, for example because the endpoint does not respond, that error is also reported as a warning.
This makes checks suited to ongoing monitoring rather than gating. In HCP Terraform, continuous validation, part of health assessments, re-evaluates check blocks on a schedule after apply and reports failures, so a website that goes down days after deployment is detected without a new run.
| Mechanism | Where | When evaluated | Failure result |
|---|---|---|---|
| Variable validation | variable block | When the value is known, normally before plan | Error |
| Precondition | lifecycle block or output | Before the object | Error |
| Postcondition | lifecycle block | After the object | Error |
| Check assertion | Top-level check block | End of plan and apply | Warning |
The following diagram compares a condition that gates runs with a check block.

When conditions fail
Gate or monitor
A team wants Terraform to tell them if their application's TLS certificate will expire within 30 days, but they must still be able to deploy unrelated changes while the renewal is in progress.
The decisive requirement is that the condition must not block runs.
A precondition or postcondition would make every run fail until the certificate is renewed, blocking unrelated deployments. A check block with an assertion on the certificate's expiry reports a warning on each plan and apply, and in HCP Terraform continuous validation can notify the team between runs, while deployments continue.
What happens to an apply when a check block assertion fails?
Show answer
The apply completes; Terraform reports the failed assertion as a warning.
Exam Tips
Remember for the exam
- check blocks hold assert blocks and at most one scoped data source.
- Checks run at the end of plan and apply.
- Failed checks warn; they never block the run.
- Continuous validation in HCP Terraform re-runs checks on a schedule.