4. You wrote this postcondition in a Terraform resource:
postcondition {
condition = self.name != ""
error_message = "Name must not be empty"
}
But Terraform never shows the error even if name is empty. What is the likely problem?
medium
A. Postconditions run after apply, but self.name is not set yet
B. The condition should use length(self.name) > 0 instead of self.name != ""
C. The postcondition block is misspelled and ignored
D. Postconditions run before resource creation, so condition is not checked properly
Solution
Step 1: Understand postcondition timing
Postconditions run after resource creation to verify results.
Step 2: Analyze condition evaluation
If self.name is null (not set) rather than empty string after apply, self.name != "" is true because null != "", so no error is triggered.
Final Answer:
Postconditions run after apply, but self.name is not set yet -> Option A
Quick Check:
null != "" passes; check availability [OK]
Hint: Postconditions check after apply; ensure attribute is set [OK]
Common Mistakes:
Assuming postconditions run before apply
Using wrong condition syntax without checking attribute availability
Misspelling postcondition block name
5. You want to ensure a Terraform resource only applies if var.region is either "us-east-1" or "us-west-2", and after apply, the resource's status attribute must be "active". Which is the correct way to write preconditions and postconditions?
hard
A.
precondition {
condition = var.region == "us-east-1" && var.region == "us-west-2"
error_message = "Region must be us-east-1 and us-west-2"
}
postcondition {
condition = self.status != "active"
error_message = "Status must not be active"
}
B.
precondition {
condition = var.region == "us-east-1" || var.region == "us-west-2"
error_message = "Region must be us-east-1 or us-west-2"
}
postcondition {
condition = self.status != "active"
error_message = "Status must not be active"
}
C.
precondition {
condition = var.region != "us-east-1" && var.region != "us-west-2"
error_message = "Region must not be us-east-1 or us-west-2"
}
postcondition {
condition = self.status == "active"
error_message = "Status must be active"
}
D.
precondition {
condition = var.region == "us-east-1" || var.region == "us-west-2"
error_message = "Region must be us-east-1 or us-west-2"
}
postcondition {
condition = self.status == "active"
error_message = "Status must be active"
}
Solution
Step 1: Write correct precondition for region
Use logical OR (||) to allow either region, with proper error message.
Step 2: Write correct postcondition for status
Check that self.status equals "active" after apply, with matching error message.
Final Answer:
Precondition uses OR for region check; postcondition checks status equals "active" -> Option D
Quick Check:
Precondition OR and postcondition equality check [OK]
Hint: Use OR for precondition, equality for postcondition [OK]