Add an explicit dependency
Learn
Explicit dependencies (depends_on) — the escape hatch
Sometimes resource B must come after resource A but doesn't use any of A's values — so there's nothing to reference. A classic case: an app server that must boot after a database is up, even though it discovers the database some other way. For that, you state the dependency explicitly:
resource "demo_server" "app" {
depends_on = [demo_server.web] # ← explicit ordering, no value passed
}
depends_on takes a list of resources. Use it only when a reference won't do —
if you find yourself adding depends_on for something you also reference, the
reference already created the edge and the depends_on is redundant.
Only app is new here, so the apply creates just that one (1 added): the network and
web are already in state from the last step.
The idiom: prefer references. Reach for
depends_ononly for the dependencies Terraform genuinely can't see.
Your task
Now add a second server app that must come up after web (say it registers with web on boot) — but it doesn't use any of web's attributes, so there's nothing to reference. That's exactly when you reach for the explicit depends_on meta-argument:
resource "demo_server" "app" {
name = "app"
network_id = demo_network.main.id
depends_on = [demo_server.web]
}
Run apply, then answer the questions below.
Your answers are checked when you run this step.
Interview prep
Interviewers love the dependency graph because it reveals whether you understand how Terraform actually executes.
What's the difference between an implicit and an explicit dependency?
An implicit dependency is created when one resource references another's attribute (e.g. network_id = demo_network.main.id) — Terraform infers the order from the reference. An explicit dependency is declared with the depends_on meta-argument when one resource must come after another but doesn't use any of its values. Both add an edge to the graph; references are preferred because they're self-documenting.
When should you use depends_on, and when should you avoid it?
Use it only for a hidden dependency — an ordering requirement Terraform can't infer because no value flows between the resources (e.g. an app that must start after a database it discovers at runtime). Avoid it when you already reference the other resource: the reference creates the edge, so the depends_on is redundant and can even mask the real relationship.
How does Terraform decide what order to create resources in?
It builds a directed dependency graph from references and depends_on, then creates resources so that every dependency comes first; independent resources are created in parallel. There's no implicit top-to-bottom file order — only the graph matters.