Interview › Infrastructure as Code (Terraform, Ansible)
How does Terraform build its dependency graph?
Infrastructure as Code (Terraform, Ansible) · Intermediate level
Answer
Terraform builds a dependency graph from resource references, provider configuration, module calls, meta-arguments, data sources, and explicit depends_on edges. It uses the graph to order reads, creates, updates, replacements, and destroys while parallelizing independent work.
Technical explanation
Graph construction lets Terraform handle create and destroy ordering differently, especially for replacements.
Unknown values at plan time can affect graph decisions and may delay evaluation until apply.
Understanding the graph helps debug cycles and unexpected ordering.
Keep Terraform's ownership boundary clear: one state should own a resource or field, and other tools should consume published outputs instead of modifying it.
Use fmt, validate, linting, policy checks, plan review, and state locking before production applies.
Design for small blast radius by splitting state around lifecycle, permissions, and recovery boundaries.
Hands-on example
1. Practice lifecycle and dependency controls for: How does Terraform build its dependency graph?
2. Add lifecycle rules deliberately:
resource "aws_db_instance" "prod" {
identifier = "prod-db"
lifecycle {
prevent_destroy = true
ignore_changes = [allocated_storage]
}
}
3. Force a reviewed replacement with:
terraform plan -replace='aws_instance.web["blue"]' -out=replace.tfplan
terraform apply replace.tfplan
4. Use depends_on only when references do not express the ordering. Then run terraform graph or inspect the plan to explain the dependency path.
5. If you use -target for recovery, immediately follow with a full terraform plan.
Check how well your resume matches the role with our free resume checker— match score, ATS check, and the skills you're missing.
More Infrastructure as Code (Terraform, Ansible) interview questions
- What is Infrastructure as Code, and what problems does it solve over click-ops?
- What is the difference between declarative and imperative IaC, and where do Terraform and Ansible fall?
- What is the difference between configuration management and provisioning?
- What is Terraform, and what is the core plan/apply workflow?
- What does terraform init do?
- What is the Terraform state file, and why is it critical?
- Why should state be stored remotely, and what backend would you use on AWS?
- What is state locking, and why does it matter for teams?