Bird
Raised Fist0
Terraformcloud~5 mins

Check blocks for assertions in Terraform - Cheat Sheet & Quick Revision

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
Recall & Review
beginner
What is the purpose of a check block in Terraform?
A check block is used to verify conditions during Terraform plan or apply. It ensures that certain rules or assertions are met before continuing, helping catch configuration errors early.
Click to reveal answer
beginner
How do you write a simple assertion inside a check block?
Inside a check block, use the assert keyword followed by a condition. For example:
check {
  assert var.instance_count > 0
  error_message = "Instance count must be greater than zero."
}
Click to reveal answer
beginner
What happens if an assertion in a check block fails during Terraform execution?
Terraform stops the plan or apply process and shows the error message defined in the check block. This prevents invalid infrastructure changes.
Click to reveal answer
intermediate
Can check blocks access variables and resource attributes?
Yes, check blocks can use variables, locals, and resource attributes to make assertions based on the current configuration values.
Click to reveal answer
intermediate
Why are check blocks considered a best practice in Terraform configurations?
They help catch mistakes early by validating assumptions and constraints. This reduces errors in infrastructure deployment and improves reliability.
Click to reveal answer
What keyword is used inside a Terraform check block to verify a condition?
Acheck
Bvalidate
Ctest
Dassert
What happens if an assertion in a check block fails?
ATerraform stops and shows an error
BTerraform retries the assertion
CTerraform ignores the check
DTerraform continues with a warning
Which of these can a check block use to make assertions?
AOnly hardcoded values
BVariables, locals, and resource attributes
CExternal scripts only
DTerraform providers
Where is a check block typically placed in Terraform code?
AAt the root module level
BInside resource blocks
CInside provider blocks
DIn output blocks
Why use check blocks in Terraform?
ATo automate resource creation
BTo speed up Terraform apply
CTo validate configuration before deployment
DTo replace variables
Explain how a check block with assertions improves Terraform configuration reliability.
Think about catching mistakes early.
You got /4 concepts.
    Describe the syntax and components of a basic check block with an assertion in Terraform.
    Focus on how to write the condition and message.
    You got /4 concepts.

      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()