Challenge - 5 Problems
Preconditions and Postconditions Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Configuration
intermediate2:00remaining
Terraform precondition blocks usage
Given the following Terraform resource configuration, what will happen if the variable
instance_count is set to 0?resource "aws_instance" "example" {
count = var.instance_count
ami = "ami-123456"
instance_type = "t2.micro"
lifecycle {
precondition {
condition = var.instance_count > 0
error_message = "instance_count must be greater than zero"
}
}
}Attempts:
2 left
💡 Hint
Preconditions in lifecycle blocks run during plan phase to validate conditions.
✗ Incorrect
The precondition checks if
instance_count is greater than zero. If it is zero, the plan fails with the specified error message.❓ service_behavior
intermediate2:00remaining
Terraform postcondition effect on resource creation
Consider this Terraform resource with a postcondition:
What happens if after bucket creation, no ID is assigned?
resource "aws_s3_bucket" "bucket" {
bucket = var.bucket_name
lifecycle {
postcondition {
condition = length(aws_s3_bucket.bucket.id) > 0
error_message = "Bucket ID must be set after creation"
}
}
}What happens if after bucket creation, no ID is assigned?
Attempts:
2 left
💡 Hint
Postconditions run after resource creation to validate state.
✗ Incorrect
If the postcondition fails after resource creation, Terraform apply fails with the error message to prevent inconsistent state.
❓ Architecture
advanced3:00remaining
Designing Terraform modules with preconditions and postconditions
You are designing a Terraform module that creates an AWS RDS instance. You want to ensure:
1. The allocated storage is at least 20 GB before creation.
2. After creation, the instance endpoint is not empty.
Which lifecycle blocks correctly enforce these preconditions and postconditions?
1. The allocated storage is at least 20 GB before creation.
2. After creation, the instance endpoint is not empty.
Which lifecycle blocks correctly enforce these preconditions and postconditions?
Attempts:
2 left
💡 Hint
Preconditions check inputs before creation; postconditions check resource attributes after creation.
✗ Incorrect
Option B correctly places the storage size check as a precondition and the endpoint existence as a postcondition. Option B reverses them incorrectly. Option B uses a weaker precondition and a null check that may not work as expected. Option B misses the postcondition.
❓ security
advanced3:00remaining
Using preconditions to enforce security policies in Terraform
You want to enforce that all AWS EC2 instances created by Terraform use an approved security group ID stored in
var.approved_sg_ids. Which precondition block correctly enforces this?Attempts:
2 left
💡 Hint
Use the contains function to check membership in a list.
✗ Incorrect
Option D correctly checks if the instance's first security group ID is in the approved list. Option D only compares to the first approved ID, which is too restrictive. Options C and D only check presence, not approval.
✅ Best Practice
expert3:00remaining
Ensuring consistent infrastructure state with postconditions in Terraform
You have a Terraform resource that creates an Azure Storage Account. You want to ensure that after creation, the storage account's primary endpoint URL starts with
https://. Which postcondition block correctly enforces this?Attempts:
2 left
💡 Hint
Use the startswith function for prefix checks.
✗ Incorrect
Option C uses the startswith function correctly to check the URL prefix. Option C uses slicing which is error-prone and less readable. Option C only checks if 'https://' appears anywhere, which is insufficient. Option C only checks presence, not the prefix.