0
0
Terraformcloud~10 mins

Check blocks for assertions in Terraform - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to add a check block that asserts the instance type is t2.micro.

Terraform
resource "aws_instance" "example" {
  instance_type = "t2.micro"

  [1] {
    condition     = var.instance_type == "t2.micro"
    error_message = "Instance type must be t2.micro"
  }
}
Drag options to blanks, or click blank then click option'
Aassert
Bcheck_block
Ccheck
Dverify
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'assert' or 'verify' instead of 'check' causes syntax errors.
Forgetting to include the check block inside the resource.
2fill in blank
medium

Complete the code to assert that the AMI ID is not empty in the check block.

Terraform
resource "aws_instance" "example" {
  ami           = var.ami_id
  instance_type = "t2.micro"

  check {
    condition     = [1]
    error_message = "AMI ID must not be empty"
  }
}
Drag options to blanks, or click blank then click option'
Avar.ami_id != ""
Bvar.ami_id > ""
Cvar.ami_id = ""
Dvar.ami_id == ""
Attempts:
3 left
💡 Hint
Common Mistakes
Using assignment operator '=' instead of comparison.
Checking for equality to empty string instead of inequality.
3fill in blank
hard

Fix the error in the check block condition to correctly assert that the count is greater than zero.

Terraform
resource "aws_instance" "example" {
  count         = var.instance_count
  instance_type = "t2.micro"

  check {
    condition     = var.instance_count [1] 0
    error_message = "Instance count must be greater than zero"
  }
}
Drag options to blanks, or click blank then click option'
A>
B==
C<=
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<=' or '<' which allow zero or negative values.
Using '==' which only allows exactly zero.
4fill in blank
hard

Fill both blanks to assert that the environment variable is either 'dev' or 'prod'.

Terraform
variable "environment" {
  type = string
}

resource "aws_instance" "example" {
  instance_type = "t2.micro"

  check {
    condition     = var.environment [1] ["dev", "prod"]
    error_message = "Environment must be 'dev' or 'prod'"
  }
}
Drag options to blanks, or click blank then click option'
A!=
B==
Cnot in
Din
Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' which compares to the whole list, causing errors.
Using 'not in' which negates the condition.
5fill in blank
hard

Fill all three blanks to assert that the tag 'Name' exists and is not empty.

Terraform
resource "aws_instance" "example" {
  tags = {
    Name = var.instance_name
  }

  check {
    condition     = contains(keys(var.tags), [1]) && var.tags[[2]] [3] ""
    error_message = "Tag 'Name' must exist and not be empty"
  }
}
Drag options to blanks, or click blank then click option'
A"Name"
C!=
D==
Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' instead of '!=' to check for non-empty value.
Forgetting to quote the key name 'Name'.