CertLumen

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_on adds explicit ones for hidden relationships.
  • depends_on lists 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_destroy builds the replacement first; prevent_destroy makes destroying plans fail.
  • ignore_changes skips listed attributes when planning updates; replace_triggered_by forces 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?

  1. AThe plan proposes destroying the database
  2. BTerraform refuses, because prevent_destroy is stored in state
  3. CTerraform keeps the database but removes it from state
  4. 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

← Previous objective · All 37 objectives · Next objective →