Cert EspressoLearn. Practise. Find your flow.

A LITTLE FOCUS.
A BIG NEXT STEP.

Your pace. Your space.
HashiCorp · Terraform 004

Dependencies and lifecycle rules

Terraform 004 · Lesson 9 of 18 · 1:02:58

YouTube loads only after you press play. This chapter opens at its timestamp in the full course. Watch on YouTube ↗

Learning with these videos? Subscribe for new courses and exam-style questions.

In this lesson

Prepare for the HashiCorp Terraform Associate (004) exam. In this lesson: implicit and hidden dependencies with depends_on, replacing a resource without an outage with create_before_destroy, prevent_destroy, ignore_changes, replace_triggered_by, and the dependency graph. Includes three original practice questions, with Bluefin Logistics, a fictional parcel delivery company, as the example.

Study notes

The text of this lesson, to read at your own pace. Information as of November 2026.

Order and protection

Bluefin's tracking API must stay online when its load balancer is replaced, and its database must never be deleted by mistake. Some order also matters that Terraform cannot see in the code. This lesson covers hidden dependencies, the lifecycle rules, and the dependency graph.

Hidden dependencies

Most dependencies are implicit: when one resource refers to another, Terraform knows the order. But some dependencies are hidden: an application on a server may need an access policy to exist at boot, without any argument that refers to it. For those, add the depends on meta-argument, with a list of the resources or modules that must come first. It takes addresses only, not expressions, and it always points at whole resources or modules. Keep it as a last resort, with a comment that explains why, because it makes plans more cautious, with more values known only after apply.

Replacing without an outage

When a change cannot be made in place, Terraform replaces the object, and by default it destroys the old one first, then creates the new one. The lifecycle block changes this behavior, and its create before destroy argument creates the replacement first, then destroys the old object. That avoids an outage during the replacement, which suits a load balancer or a launch template. But both objects exist for a moment, so names that must be unique, like a bucket name, can make the creation fail.

More lifecycle rules

Prevent destroy makes Terraform reject any plan that would destroy the resource, which protects a production database from a mistake. Ignore changes lists attributes that Terraform should not reset, because something else changes them, like tags set by another tool, or a capacity set by autoscaling. Replace triggered by replaces the resource when another resource, or one of its attributes, changes. And the lifecycle block also holds preconditions and postconditions, which the next lesson covers.

The dependency graph

From references and depends on, Terraform builds a dependency graph of the resources. It walks the graph in order, and runs independent operations in parallel, up to ten at a time by default, a limit that the parallelism option changes. When it destroys, it walks the graph in reverse, so a server goes before its subnet. And terraform graph prints the graph in the DOT format, which visualization tools can draw.

Recap

Let's recap. References create implicit dependencies, and depends on handles hidden ones, as a last resort. Create before destroy builds the replacement first, and prevent destroy rejects plans that would destroy the resource, as long as its block is in the configuration. Ignore changes leaves alone the attributes that something else manages, and replace triggered by ties a replacement to another change. And Terraform walks the dependency graph in order, in parallel where it can, and in reverse to destroy.

Quick checks for this lesson

The 3 questions of this lesson, as plain text. The button above runs them with shuffled choices and the reason behind every choice.

  1. A server runs a script at boot that reads a bucket through its role. The policy that grants this access is not referenced by the server. What makes sure the policy exists first?

    • Starting the script a few minutes later
    • depends_on, pointing at the policy
    • create_before_destroy on the policy
    • Declaring the policy first in the file
    Show the answer

    depends_on, pointing at the policy

    Depends on makes the hidden dependency explicit, so Terraform finishes the policy before it creates the server. “Starting the script a few minutes later” is wrong, because a delay only hopes that the policy is ready, and guarantees nothing. “create_before_destroy on the policy” is about how the policy is replaced, not when the server is created. And “Declaring the policy first in the file” is wrong, because Terraform ignores the order of blocks in files.

  2. Autoscaling changes the desired_capacity of a server group outside Terraform, and every plan wants to set it back. What should the team add?

    • prevent_destroy on the group
    • create_before_destroy on the group
    • A depends_on that points at the scaling policy
    • ignore_changes = [desired_capacity]
    Show the answer

    ignore_changes = [desired_capacity]

    Ignore changes tells Terraform not to reset the desired capacity, while it keeps managing the rest of the group. “prevent_destroy on the group” only blocks plans that would destroy the group. “create_before_destroy on the group” only changes how a replacement happens. And “A depends_on that points at the scaling policy” is about the order of operations, and does nothing about the capacity.

  3. A production database has prevent_destroy = true. An engineer deletes its whole resource block, then runs terraform apply. What happens?

    • Terraform refuses the plan, with an error
    • Terraform forgets it, and leaves it running
    • The database is destroyed
    • Terraform asks for a second approval
    Show the answer

    The database is destroyed

    Prevent destroy is part of the resource block, so when the block is removed, the protection goes with it, and the plan destroys the database after confirmation. “Terraform refuses the plan, with an error” would happen only if the block, and its setting, were still in the configuration. “Terraform forgets it, and leaves it running” is what a removed block with destroy set to false does, which lesson 15 covers. And “Terraform asks for a second approval” is wrong, because Terraform has no second approval step.