Interview Prep/Terraform/Resources Variables and Expressions

When should you add `depends_on`, and what can go wrong with it?

AdvancedScenarioResources Variables and Expressions
Answer

Situation read → Terraform infers most dependencies from references; explicit ones are for hidden dependencies.

What I'd do →

  1. Use it only when the dependency isn't visible in an attribute reference, such as an IAM policy that must exist before an instance starts.
  2. Avoid depends_on on modules or data sources unless essential.
resource "aws_instance" "app" {
  # ...
  depends_on = [aws_iam_role_policy.app]
}

Why → depends_on on a data source defers its read to apply time, so values become unknown at plan time and downstream diffs get noisy. On modules it makes everything in the module wait.

Interview tip → Prefer expressing the dependency through a real reference.

All Terraform questions Roadmap: Terraform

More on Resources Variables and Expressions