0
0
Terraformcloud~30 mins

Check blocks for assertions in Terraform - Mini Project: Build & Apply

Choose your learning style9 modes available
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
Need a 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
Need a 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
Need a 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
Need a hint?

Use a resource block with count and instance_type set from variables.