Skip to main content
depends_on is a Terraform meta-argument that creates explicit ordering between resources. Terraform usually determines ordering automatically via implicit dependencies when one resource references another’s attributes. For example, a subnet that references a VPC ID creates an implicit dependency and Terraform ensures the VPC is created first. But when Terraform cannot infer the relationship from resource attributes — for instance, when one resource must be fully ready before another starts — you should use depends_on to enforce ordering and prevent race conditions. Key benefits of depends_on:
  • Forces a specific creation order in Terraform plans and applies.
  • Prevents race conditions when resources must be fully provisioned before others start.
  • Ensures downstream resources that perform startup actions (migrations, initializations) have their upstream dependencies available.
  • Use sparingly — prefer implicit dependencies where possible to retain parallelism and faster provisioning.
A purple presentation slide titled "depends-on Meta-Argument" about ensuring resources are created in the right order with Terraform. It lists tips like explicitly defining dependencies, ensuring correct provisioning order, and preventing race conditions.
Example: RDS database and EC2 web server
  • Without depends_on, Terraform may create both resources in parallel. If the web server expects the database to be available and initialized at startup (beyond just knowing its ID), you should enforce order with depends_on.
This depends_on prevents Terraform from creating the EC2 instance until the database resource has been created. Use this pattern when the application will try to connect or run migrations at startup and requires the database to be fully provisioned.
Prefer implicit dependencies (resource attribute references) when possible. Overusing depends_on serializes execution and reduces Terraform’s ability to parallelize resource creation.
Implicit vs explicit dependencies — clarification and examples
  • Implicit dependency: Terraform infers ordering from attribute references between resources.
  • Explicit dependency: You use depends_on when no attribute reference exists that would create the dependency automatically, but you still require a specific order.
Implicit dependency example (security group referenced by EC2 instance)
In this example, Terraform automatically orders creation so the security group exists before the instance. Explicit dependency example (Lambda function depending on a policy attachment that isn’t referenced)
Here, because the Lambda function does not reference attributes from the policy attachment resource, Terraform would not infer the dependency. depends_on explicitly enforces the order.
Only use depends_on when necessary. If an attribute reference already expresses the dependency, prefer that implicit dependency. Misusing depends_on can hide configuration issues and slow down apply operations.
When to use depends_on — quick reference table Related links and references Summary
  • depends_on gives you explicit control over resource ordering when Terraform cannot infer dependencies from configuration.
  • Use it to avoid race conditions and to ensure resources that rely on completed provisioning are created in the correct order.
  • Favor implicit dependencies where possible; reserve depends_on for cases where explicit ordering is truly required.

Watch Video

Practice Lab