Lesson 3 of 3 Custom Conditions and Checks

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.

Select all that apply

Inside a check block

Exam skills 4.7.3

Identify what a check block can contain and how it is used.

Log in to try this activity Interactive activities are free with an account.

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.

MechanismWhereWhen evaluatedFailure result
Variable validationvariable blockWhen the value is known, normally before planError
Preconditionlifecycle block or outputBefore the objectError
Postconditionlifecycle blockAfter the objectError
Check assertionTop-level check blockEnd of plan and applyWarning

The following diagram compares a condition that gates runs with a check block.

A gate compared with a monitor for certificate expiry. A precondition or postcondition would fail every run until the certificate is renewed, blocking unrelated deployments. A check block reports a warning on each plan and apply, and HCP Terraform continuous validation can notify the team between runs, while deployments continue.
The 30-day threshold is illustrative.
Match the pairs

When conditions fail

Exam skills 4.7.1 · 4.7.2 · 4.7.3

Match each mechanism with when it is evaluated and what a failure does.

Log in to try this activity Interactive activities are free with an account.
Worked example

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.

Check yourself

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.