0
0
Terraformcloud~10 mins

Preconditions and postconditions 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 precondition that checks if the variable 'instance_count' is greater than zero.

Terraform
variable "instance_count" {
  type = number
  [1] = [
    {
      condition     = var.instance_count > 0
      error_message = "Instance count must be greater than zero."
    }
  ]
}
Drag options to blanks, or click blank then click option'
Aprecondition
Bpostcondition
Cvalidation
Dconstraint
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'precondition' or 'postcondition' inside variable blocks instead of 'validation'.
Placing the condition outside the variable block.
2fill in blank
medium

Complete the resource block to add a precondition that ensures the 'ami' attribute is not empty.

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

  [1] {
    condition     = length(self.ami) > 0
    error_message = "AMI must not be empty."
  }
}
Drag options to blanks, or click blank then click option'
Apostcondition
Bvalidation
Cconstraint
Dprecondition
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'validation' inside resource blocks (only valid in variables).
Using 'postcondition' instead of 'precondition' for input checks.
3fill in blank
hard

Fix the error in the postcondition block that checks if the instance state is 'running'.

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

  lifecycle {
    [1] {
      condition     = self.instance_state.name == "running"
      error_message = "Instance must be running after creation."
    }
  }
}
Drag options to blanks, or click blank then click option'
Aprecondition
Bpostcondition
Cvalidation
Dconstraint
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'precondition' inside lifecycle blocks.
Using 'validation' inside lifecycle blocks.
4fill in blank
hard

Fill both blanks to add a precondition and a postcondition to a resource that checks 'instance_type' before creation and 'state' after creation.

Terraform
resource "aws_instance" "example" {
  ami           = var.ami
  instance_type = var.instance_type

  [1] {
    condition     = self.instance_type == "t2.micro"
    error_message = "Instance type must be t2.micro."
  }

  lifecycle {
    [2] {
      condition     = self.instance_state.name == "running"
      error_message = "Instance must be running after creation."
    }
  }
}
Drag options to blanks, or click blank then click option'
Aprecondition
Bvalidation
Cpostcondition
Dconstraint
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping precondition and postcondition blocks.
Using 'validation' inside resource blocks.
5fill in blank
hard

Fill all three blanks to add a variable with validation, a resource with precondition, and a lifecycle postcondition.

Terraform
variable "region" {
  type = string
  [1] = [
    {
      condition     = var.region != ""
      error_message = "Region cannot be empty."
    }
  ]
}

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

  [2] {
    condition     = self.instance_type == "t2.micro"
    error_message = "Instance type must be t2.micro."
  }

  lifecycle {
    [3] {
      condition     = self.instance_state.name == "running"
      error_message = "Instance must be running after creation."
    }
  }
}
Drag options to blanks, or click blank then click option'
Avalidation
Bprecondition
Cpostcondition
Dconstraint
Attempts:
3 left
💡 Hint
Common Mistakes
Using precondition inside variable blocks.
Using validation inside resource blocks.
Placing postcondition outside lifecycle.