Cert EspressoLearn. Practise. Find your flow.

A LITTLE FOCUS.
A BIG NEXT STEP.

Your pace. Your space.
HashiCorp · Terraform 004

Custom conditions and check blocks

Terraform 004 · Lesson 10 of 18 · 1:08:29

YouTube loads only after you press play. This chapter opens at its timestamp in the full course. Watch on YouTube ↗

Learning with these videos? Subscribe for new courses and exam-style questions.

In this lesson

Prepare for the HashiCorp Terraform Associate (004) exam. In this lesson: input variable validation, preconditions and postconditions on resources and data sources, preconditions on outputs, and check blocks that report warnings without stopping a run. Includes three original practice questions, with Bluefin Logistics, a fictional parcel delivery company, as the example.

Study notes

The text of this lesson, to read at your own pace. Information as of November 2026.

Catching mistakes early

A wrong region, an image built for the wrong processor, or a tracking site that stops answering: Bluefin wants Terraform to catch these problems. Terraform lets you write your own conditions, each with a message that explains the problem. This lesson covers variable validation, preconditions, postconditions, and check blocks.

Input variable validation

An input variable can hold one or more validation blocks. Each one has a condition, an expression that must be true, and an error message, which is required. Here, the region must start with the letters E U and a dash, because Bluefin runs in Europe only. Terraform checks the value before it builds the plan, so a wrong value never reaches the provider. When the condition is false, Terraform stops with an error, and shows your message.

Preconditions

Resources and data sources can hold custom conditions too, inside their lifecycle block. A precondition is an assumption that Terraform checks before it creates or changes the object: here, the image must be built for the arm sixty four architecture. Terraform evaluates it while it builds the plan when the values are known, and otherwise during apply. When it fails, Terraform stops with an error, before it touches the object.

Postconditions

A postcondition is a guarantee that Terraform checks after it plans, applies, or reads the object, and it refers to the object itself through the self keyword. Here, the image that the data source finds must carry the tag of the tracking API, so the resources that depend on it never get the wrong one. A failed postcondition stops the operation with an error, but it does not undo what Terraform has already done. And an output block can hold a precondition, to check a value before Terraform returns it.

Check blocks

A check block validates the infrastructure as a whole, outside the life cycle of any one resource. It holds one or more assert blocks, and it can hold a data source of its own, which only that check can use, like a request to the tracking site. Terraform runs checks as the last step of every plan and apply, and a failed assertion is only a warning: the operation continues. That makes checks the only custom conditions that never block an operation. And when health checks are turned on in HCP Terraform, the workspace evaluates its checks, preconditions, and postconditions regularly, between runs.

Recap

Let's recap. Validation blocks check input variables before the plan, and a failure is an error. Preconditions check assumptions before Terraform creates an object, and postconditions check guarantees afterward, through self. Check blocks run at the end of every plan and apply, and a failure is only a warning. Every condition has an error message, and Terraform runs them in order: variable validations, preconditions, postconditions, then checks.

Quick checks for this lesson

The 3 questions of this lesson, as plain text. The button above runs them with shuffled choices and the reason behind every choice.

  1. The region variable must start with eu- (validation block). An engineer runs terraform plan with region set to us-east-1. What happens?

    • Terraform stops with an error, before planning
    • Terraform plans, and shows a warning at the end
    • The provider rejects the region during apply
    • Terraform uses the default value of the variable
    Show the answer

    Terraform stops with an error, before planning

    A failed validation is an error, and Terraform stops before it builds the plan, with your error message. “Terraform plans, and shows a warning at the end” is what a failed check block does, and checks come later in this lesson. “The provider rejects the region during apply” would come much later, and validation exists precisely to catch the value first. And “Terraform uses the default value of the variable” is wrong, because Terraform never swaps a value that you gave for the default.

  2. After the load balancer is created, Bluefin wants to verify that its DNS name ends with the expected domain, and to stop everything that depends on it if not. What should the team add?

    • A validation block in a variable
    • A precondition in its lifecycle block
    • A check block with an assert
    • A postcondition that refers to self
    Show the answer

    A postcondition that refers to self

    A postcondition checks the object after Terraform creates it, through self, and a failure stops what depends on it. “A validation block in a variable” only checks input values, before the plan. “A precondition in its lifecycle block” is for assumptions checked before Terraform creates the object, when the name does not exist yet. And “A check block with an assert” would only report a warning, without stopping anything.

  3. During terraform apply, every resource is created, but the check block that requests the tracking site gets an error code. What does Terraform do?

    • It destroys the resources it just created
    • It reports a warning, and the apply completes
    • It stops with an error, and rolls back the apply
    • It taints every resource in the state
    Show the answer

    It reports a warning, and the apply completes

    A failed assertion in a check block is only a warning, so the apply completes, and the message tells the team to look at the site. “It destroys the resources it just created” is wrong, because Terraform never destroys resources because of a check. “It stops with an error, and rolls back the apply” is wrong twice: checks never stop an operation, and Terraform has no rollback. And “It taints every resource in the state” is wrong, because a check changes nothing in the state.