0
0
Terraformcloud~10 mins

Check blocks for assertions in Terraform - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Check blocks for assertions
Start Terraform Plan
Evaluate Check Block
Run Assertion Condition
Pass Check
Continue
Apply Infrastructure
Terraform runs check blocks during plan to verify conditions. If assertions pass, plan continues; if not, it stops.
Execution Sample
Terraform
check "example_check" {
  condition = length(var.names) > 0
  error_message = "names list must not be empty"
}
This check block asserts that the variable 'names' list is not empty during terraform plan.
Process Table
StepActionCondition EvaluatedResultPlan Behavior
1Start terraform planN/AN/APlan starts
2Evaluate check block 'example_check'length(var.names) > 0TruePlan continues
3Apply infrastructureN/AN/AResources created
4End planN/AN/APlan successful
💡 Check block condition is true, so plan proceeds and applies resources
Status Tracker
VariableStartAfter Check EvaluationFinal
var.names["app", "db"]["app", "db"]["app", "db"]
check.conditionN/ATrueTrue
Key Moments - 2 Insights
What happens if the check block condition is false?
If the condition is false (see execution_table step 2), Terraform stops the plan and shows the error_message.
Does the check block run during apply?
No, check blocks run only during terraform plan to validate conditions before applying.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the result of the check condition at step 2?
ATrue
BFalse
CError
DNot evaluated
💡 Hint
Refer to the 'Condition Evaluated' and 'Result' columns in execution_table row 2
At which step does Terraform stop the plan if the check fails?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Check the 'Plan Behavior' column in execution_table for when the plan stops
If var.names was empty, how would the execution_table change at step 2?
AResult would be True and plan continues
BCondition would not be evaluated
CResult would be False and plan stops
DPlan would skip check block
💡 Hint
Refer to the key_moments explanation about false condition behavior
Concept Snapshot
Terraform check blocks run during plan to assert conditions.
Syntax: check "name" { condition = <expr> error_message = "msg" }
If condition is false, plan stops with error.
If true, plan continues and applies resources.
Check blocks help catch config errors early.
Full Transcript
Terraform check blocks are special blocks that run during the terraform plan phase. They evaluate a condition expression. If the condition is true, the plan continues normally. If the condition is false, Terraform stops the plan and shows the error message defined in the check block. This helps catch configuration mistakes before applying infrastructure. Check blocks do not run during apply, only during plan. In the example, the check asserts that the variable 'names' list is not empty. Since it is not empty, the plan passes and resources are created.