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
Using Terraform check blocks for assertions
📖 Scenario: You are managing cloud infrastructure with Terraform. You want to ensure that certain conditions are met before applying changes. For example, you want to check that the instance type is allowed and the number of instances is within limits.
🎯 Goal: Create a Terraform configuration that uses check blocks to assert that the instance type is either t2.micro or t2.small, and that the number of instances is between 1 and 3.
📋 What You'll Learn
Create a variable instance_type with default t2.micro
Create a variable instance_count with default 2
Add a check block to assert instance_type is t2.micro or t2.small
Add a check block to assert instance_count is between 1 and 3
💡 Why This Matters
🌍 Real World
Using check blocks helps catch configuration errors early in Terraform plans, avoiding costly mistakes in cloud infrastructure.
💼 Career
Cloud engineers and DevOps professionals use Terraform check blocks to enforce policies and ensure infrastructure meets requirements before deployment.
Progress0 / 4 steps
1
Create variables for instance type and count
Create a variable called instance_type with default value "t2.micro" and a variable called instance_count with default value 2.
Terraform
Hint
Use variable blocks with default values.
2
Add check block for instance_type
Add a check block that asserts the variable instance_type is either "t2.micro" or "t2.small". Use the expression var.instance_type == "t2.micro" || var.instance_type == "t2.small".
Terraform
Hint
Use a check block with condition and error_message.
3
Add check block for instance_count
Add a check block that asserts the variable instance_count is between 1 and 3 inclusive. Use the expression var.instance_count >= 1 && var.instance_count <= 3.
Terraform
Hint
Use a check block with a condition that uses comparison operators.
4
Add resource block using variables
Add an aws_instance resource named example that uses var.instance_type for instance_type and var.instance_count for count.
Terraform
Hint
Use a resource block with count and instance_type set from variables.
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