HashiCorp Terraform Associate (004) · Section 4 · 24% of the exam
Validate configuration using custom conditions
One of the objectives of the HashiCorp Certified: Terraform Associate (004) exam, in the section “Terraform configuration”. Here is what it covers, the points to remember and a question to try.
What it covers
Terraform orders work from references (implicit dependencies) and from depends_on (explicit ones). The lifecycle block changes how replacement and updates happen, and custom conditions (variable validation, preconditions, postconditions and check blocks) turn assumptions into enforced rules.
Points to remember
- References create implicit dependencies;
depends_onadds explicit ones for hidden relationships. depends_onlists resources or modules (no attributes) and is allowed on resources, data sources, modules and outputs.- Terraform walks the graph in parallel, and destroys in reverse dependency order.
create_before_destroybuilds the replacement first;prevent_destroymakes destroying plans fail.ignore_changesskips listed attributes when planning updates;replace_triggered_byforces replacement.
Try a question
A database resource has lifecycle { prevent_destroy = true }. An engineer deletes the whole resource block from the configuration and runs terraform apply. What happens?
- AThe plan proposes destroying the database
- BTerraform refuses, because prevent_destroy is stored in state
- CTerraform keeps the database but removes it from state
- DTerraform asks the provider for permission to destroy
Show the answer
A. prevent_destroy only guards against plans that destroy the object while the block remains. Deleting the block removes the guard, so the object becomes an orphan to destroy.
Written for practice in the style of the exam: never a question from the real exam.
In the course
The lesson Dependencies, lifecycle and custom conditions teaches this objective (about 45 minutes), and 17 practice questions test it. The course has lessons, exam-style questions, timed practice exams and an AI tutor for every objective, and a pass prediction that tells you when you're ready to book.
More in this section
- Use and differentiate
resourceanddatablocks - Refer to resource attributes and create cross-resource references
- Use variables and outputs
- Understand and use complex types
- Write dynamic configuration using expressions and functions
- Define resource dependencies in configuration
- Understand best practices for managing sensitive data, including secrets management with Vault