⚡ ~/naveed Interview Prep
⚡ Portfolio Home ✍️ Engineering Blog Deep Dives 🎯 Interview Hub 998+ Scenarios ☸️ Kubernetes Mastery Hub 24 Modules 🎮 DevOps Arcade & Quizzes Subnet Blitz ⚡ 🗺️ DevOps Roadmaps PDFs & Guides 🤖 Morpheus Analysis AI Quant ↗ 🛠️ Developer Tools Utilities 🧪 Labs & Experiments 📄 Interactive CV & Certs 🔗 All Links & Socials ⚡ Join The Dispatch (Weekly SRE Newsletter) →
Senior DevOps / SRE Terraform Terraform Core Engine Core Architecture

Q: How does the Terraform dependency graph (DAG) work internally to determine resource execution order and concurrency?

Deep dive into Terraform's internal Directed Acyclic Graph (DAG) construction, topological sort evaluation, concurrency management, and dependency cycle prevention.

#Terraform #DAG #Internals #IaC #Graph Theory
🎙️ Candidate Opening & Architectural Context
"Terraform represents every resource, module, and data source as a vertex in a Directed Acyclic Graph (DAG). It uses graph theory algorithms to determine dependencies, parallelize independent actions, and walk the graph safely during plan and apply."
Advertisement

🛠️ Production Runbook & Step-by-Step Resolution

1

Graph Construction & Implicit References

Terraform analyzes references in HCL. When aws_instance.web references aws_security_group.sg.id, Terraform automatically creates a directed edge: sg -> instance. Explicit depends_on directives add manual edges when implicit data references do not exist.

2

Topological Sorting & Parallel Execution

Terraform performs a topological sort on the DAG to find independent root nodes. Resources with zero incoming dependencies are provisioned in parallel up to the concurrency limit (-parallelism=10 by default).

3

Detecting Cycles and Visualizing the Graph

If resource A references resource B, and resource B references resource A, Terraform detects a cycle during graph compilation and errors with 'Cycle detected' before touching real infrastructure.

terraform graph | dot -Tpng > graph.png
💡 The Senior SRE Gold Nugget (Key Architectural Takeaway)
"Terraform builds a DAG from implicit references and depends_on, topologially sorts it, and provisions independent branches in parallel up to -parallelism limit."
⚡ 60-Second Elevator Pitch Talking Points
  • Terraform builds a Directed Acyclic Graph where resources are nodes and references are edges.
  • Implicit dependencies (attribute references) and explicit depends_on define graph ordering.
  • Topological sorting allows independent resources to deploy concurrently (default 10 parallel threads).
  • Cycle detection prevents deadlocks before touching any cloud provider APIs.
Advertisement
Want more Terraform scenarios?
Explore our complete collection of scenario-based Terraform interview runbooks.
Browse All Terraform Questions →

📚 Related Production Scenarios in Terraform