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
✗ Incorrect
The assert keyword is used inside a check block to verify conditions.
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
✗ Incorrect
Terraform stops the plan or apply and shows the error message if an assertion fails.
Which of these can a check block use to make assertions?
AOnly hardcoded values
BVariables, locals, and resource attributes
CExternal scripts only
DTerraform providers
✗ Incorrect
check blocks can use variables, locals, and resource attributes.
Where is a check block typically placed in Terraform code?
AAt the root module level
BInside resource blocks
CInside provider blocks
DIn output blocks
✗ Incorrect
check blocks are usually placed at the root module level to validate configuration.
Why use check blocks in Terraform?
ATo automate resource creation
BTo speed up Terraform apply
CTo validate configuration before deployment
DTo replace variables
✗ Incorrect
check blocks validate configuration and catch errors early.
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
Step 1: Understand the role of check blocks
Check blocks are used to verify conditions before Terraform creates resources to avoid invalid configurations.
Step 2: Differentiate from other blocks
Variables define inputs, outputs show results, and loops create multiple resources; none verify conditions before creation.
Final Answer:
To verify conditions before resource creation and prevent errors -> Option B
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
Step 1: Identify correct attribute names
The correct syntax uses condition for the boolean check and error_message for the error text.
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.
Final Answer:
check "valid_region" { condition = var.region == "us-east-1" error_message = "Region must be us-east-1" } -> Option D
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
Step 1: Understand the check block condition
The check block requires var.count > 0. Setting count = 0 violates this condition.
Step 2: Predict Terraform behavior on violation
Terraform stops and shows the error message from the check block instead of applying resources.
Final Answer:
Terraform fails with error: Count must be positive -> Option A
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:
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
Step 1: Analyze variable unset behavior
If var.name is not set and has no default, it is null, not an empty string.
Step 2: Understand condition evaluation
Comparing null to empty string with != causes an error because null is not a string.
Final Answer:
Terraform errors because var.name is null and comparison fails -> Option A
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
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.
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.
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
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