How does Terraform handle dependencies between resources?
Learn how Terraform orders resources with implicit and explicit dependencies, the depends_on argument, the dependency graph, examples and interview Q&A.
Expected Interview Answer
Terraform builds a directed dependency graph of all resources and applies them in the correct order — mostly by inferring implicit dependencies whenever one resource references another's attribute, and letting you declare explicit ones with depends_on when no reference exists.
During planning, Terraform parses every expression: if resource B uses an attribute of resource A (like an ID that only exists after creation), it records that B depends on A and must be created after it. Resources with no relationship are created in parallel. When a dependency is real but not expressed through an attribute reference, you add a depends_on argument to force ordering. Terraform then walks this graph to create, update, or destroy resources in a safe sequence, reversing the order on destroy.
- Correct creation and destruction ordering without manual scripting
- Automatic parallelism for unrelated resources
- Implicit dependencies from ordinary attribute references
- Explicit control via depends_on for hidden relationships
- A visualizable graph for debugging ordering issues
AI Mentor Explanation
You cannot set a field placement until you know who is bowling, and you cannot bowl until the batter is at the crease — each action waits on the one it references. Terraform reads these references like a captain sequencing the over: unrelated tasks such as adjusting two boundary fielders happen at once, but anything that needs a prior result waits its turn automatically.
Step-by-Step Explanation
Step 1
Parse references
Terraform scans every expression to see which resources read attributes of others.
Step 2
Build the graph
It constructs a directed acyclic graph where an edge means 'must come after'.
Step 3
Infer implicit dependencies
Any attribute reference (like vpc_id = aws_vpc.main.id) automatically creates an ordering edge.
Step 4
Apply explicit depends_on
For relationships with no reference, depends_on adds an edge manually.
Step 5
Walk the graph
Terraform creates independent resources in parallel and dependent ones in order, reversing for destroy.
What Interviewer Expects
- Distinguishing implicit from explicit dependencies
- Knowing attribute references create implicit ordering
- When and why to use depends_on
- Understanding the dependency graph is a DAG
- That unrelated resources are created in parallel
Common Mistakes
- Overusing depends_on when an attribute reference already exists
- Assuming resources apply in top-to-bottom file order
- Creating cycles that Terraform rejects
- Forgetting destroy order is the reverse of create order
- Not knowing terraform graph exists for debugging
Best Answer (HR Friendly)
“Terraform figures out the right order to build things by noticing when one resource uses another's details, so it creates them in sequence automatically. When there is a hidden connection it cannot see, you tell it explicitly with a depends_on setting.”
Code Example
resource "aws_vpc" "main" {
cidr_block = "10.0.0.0/16"
}
resource "aws_subnet" "web" {
# referencing aws_vpc.main.id makes this depend on the VPC
vpc_id = aws_vpc.main.id
cidr_block = "10.0.1.0/24"
}resource "aws_iam_role_policy" "perms" {
role = aws_iam_role.app.id
policy = data.aws_iam_policy_document.app.json
}
resource "aws_instance" "app" {
ami = "ami-123456"
instance_type = "t3.micro"
# policy must exist before the instance that assumes the role
depends_on = [aws_iam_role_policy.perms]
}Follow-up Questions
- What is the difference between implicit and explicit dependencies?
- When should you use depends_on instead of a reference?
- What happens if two resources form a dependency cycle?
- How can you visualize the dependency graph?
- In what order are resources destroyed?
MCQ Practice
1. How does Terraform most commonly detect a dependency between two resources?
When one resource references another's attribute, Terraform infers an implicit dependency and orders them accordingly.
2. When is depends_on the right tool?
depends_on is for genuine dependencies that are not expressed through an attribute reference.
3. How are two completely unrelated resources handled during apply?
Resources with no dependency edge between them are created in parallel for speed.
Flash Cards
What is an implicit dependency? — An ordering edge Terraform infers automatically when one resource references another's attribute.
What is depends_on for? — Declaring an explicit dependency when a real relationship has no attribute reference.
What structure orders Terraform resources? — A directed acyclic graph (DAG) built from resource references.
What is the destroy order? — The reverse of the create order, so dependents are removed before their dependencies.