Bird
Raised Fist0
Terraformcloud~10 mins

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

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.

Practice

(1/5)
1. What is the main purpose of a check block in Terraform?
easy
A. To define variables for resource configuration
B. To verify conditions before resource creation and prevent errors
C. To output resource attributes after deployment
D. To create loops for multiple resource instances

Solution

  1. Step 1: Understand the role of check blocks

    Check blocks are used to verify conditions before Terraform creates resources to avoid invalid configurations.
  2. Step 2: Differentiate from other blocks

    Variables define inputs, outputs show results, and loops create multiple resources; none verify conditions before creation.
  3. Final Answer:

    To verify conditions before resource creation and prevent errors -> Option B
  4. Quick Check:

    Check blocks = pre-creation validation [OK]
Hint: Check blocks catch errors before deployment [OK]
Common Mistakes:
  • Confusing check blocks with variable declarations
  • Thinking check blocks output values
  • Assuming check blocks create resources
2. Which of the following is the correct syntax for a check block in Terraform?
easy
A. check "valid_region" { condition var.region == "us-east-1" error_message "Region must be us-east-1" }
B. check "valid_region" { assert = var.region == "us-east-1" message = "Region must be us-east-1" }
C. check "valid_region" { condition = var.region = "us-east-1" error = "Region must be us-east-1" }
D. check "valid_region" { condition = var.region == "us-east-1" error_message = "Region must be us-east-1" }

Solution

  1. Step 1: Identify correct attribute names

    The correct syntax uses condition for the boolean check and error_message for the error text.
  2. Step 2: Check syntax correctness

    check "valid_region" { condition = var.region == "us-east-1" error_message = "Region must be us-east-1" } correctly uses condition = and error_message = with proper equality ==. Others use wrong attribute names or syntax errors.
  3. Final Answer:

    check "valid_region" { condition = var.region == "us-east-1" error_message = "Region must be us-east-1" } -> Option D
  4. Quick Check:

    Use condition and error_message with equals signs [OK]
Hint: Use 'condition =' and 'error_message =' inside check blocks [OK]
Common Mistakes:
  • Using single equals (=) instead of double equals (==) for condition
  • Using wrong attribute names like assert or error
  • Missing equals signs between keys and values
3. Given this Terraform snippet:
variable "count" { type = number default = 3 }
check "positive_count" { condition = var.count > 0 error_message = "Count must be positive" }

What happens if you set count = 0 and run terraform apply?
medium
A. Terraform fails with error: Count must be positive
B. Terraform applies resources with count 0, creating none
C. Terraform ignores the check block and applies resources
D. Terraform applies resources but warns about count

Solution

  1. Step 1: Understand the check block condition

    The check block requires var.count > 0. Setting count = 0 violates this condition.
  2. Step 2: Predict Terraform behavior on violation

    Terraform stops and shows the error message from the check block instead of applying resources.
  3. Final Answer:

    Terraform fails with error: Count must be positive -> Option A
  4. Quick Check:

    Check blocks stop apply if condition false [OK]
Hint: Check blocks block apply if condition is false [OK]
Common Mistakes:
  • Thinking Terraform ignores check blocks
  • Assuming resources apply with warnings
  • Confusing default variable values with overrides
4. This Terraform check block causes an error during plan:
check "valid_name" { condition = var.name != "" error_message = "Name cannot be empty" }

What is the likely cause if var.name is not set?
medium
A. Terraform errors because var.name is null and comparison fails
B. Terraform ignores the check block if variable is unset
C. Terraform treats unset variable as empty string and passes check
D. Terraform applies default empty string and shows warning only

Solution

  1. Step 1: Analyze variable unset behavior

    If var.name is not set and has no default, it is null, not an empty string.
  2. Step 2: Understand condition evaluation

    Comparing null to empty string with != causes an error because null is not a string.
  3. Final Answer:

    Terraform errors because var.name is null and comparison fails -> Option A
  4. Quick Check:

    Null variables cause check block errors if compared to strings [OK]
Hint: Unset variables are null, not empty strings [OK]
Common Mistakes:
  • Assuming unset variables default to empty strings
  • Expecting check blocks to ignore null values
  • Thinking Terraform only warns on check failures
5. You want to enforce that a variable region is either "us-east-1" or "us-west-2" using a check block. Which is the correct check block to enforce this?
hard
A. check "valid_region" { condition = var.region == "us-east-1" || var.region == "us-west-2" error_message = "Region must be us-east-1 or us-west-2" }
B. check "valid_region" { condition = var.region == ["us-east-1", "us-west-2"] error_message = "Region must be us-east-1 or us-west-2" }
C. check "valid_region" { condition = contains(["us-east-1", "us-west-2"], var.region) error_message = "Region must be us-east-1 or us-west-2" }
D. check "valid_region" { condition = var.region in ("us-east-1", "us-west-2") error_message = "Region must be us-east-1 or us-west-2" }

Solution

  1. Step 1: Understand how to check membership in a list

    Terraform uses the contains(list, value) function to check if a value is in a list.
  2. Step 2: Evaluate each option

    check "valid_region" { condition = var.region == "us-east-1" || var.region == "us-west-2" error_message = "Region must be us-east-1 or us-west-2" } uses logical OR correctly but is verbose; check "valid_region" { condition = var.region == ["us-east-1", "us-west-2"] error_message = "Region must be us-east-1 or us-west-2" } compares a string to a list incorrectly; check "valid_region" { condition = contains(["us-east-1", "us-west-2"], var.region) error_message = "Region must be us-east-1 or us-west-2" } uses contains properly; check "valid_region" { condition = var.region in ("us-east-1", "us-west-2") error_message = "Region must be us-east-1 or us-west-2" } uses invalid syntax in.
  3. Final Answer:

    check "valid_region" { condition = contains(["us-east-1", "us-west-2"], var.region) error_message = "Region must be us-east-1 or us-west-2" } -> Option C
  4. Quick Check:

    Use contains(list, value) to check membership [OK]
Hint: Use contains() to check if value is in list [OK]
Common Mistakes:
  • Using 'in' keyword which Terraform does not support
  • Comparing string directly to list
  • Using verbose OR instead of contains()