HashiCorp Terraform Associate (004) · Section 4 · 24% of the exam
Define resource dependencies in configuration
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 resource is replaced by default by destroying the old object and then creating the new one. The engineer needs the new object to exist before the old one is removed. What should be set?
- A
lifecycle { prevent_destroy = true }on the resource being replaced - B
lifecycle { create_before_destroy = true } - C
depends_on = [self] - D
lifecycle { ignore_changes = all }
Show the answer
B. With create_before_destroy, Terraform builds the replacement first. The settings must allow both objects to exist at once, for example by using unique names.
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
- Validate configuration using custom conditions
- Understand best practices for managing sensitive data, including secrets management with Vault