Depends On
The depends_on meta-argument forces Terraform to create one resource or module only after another, even when no argument references it. Terraform normally infers order from references, so depends_on is for hidden dependencies.
What Is depends_on?
Terraform builds its dependency graph from references like aws_vpc.main.id. Use depends_on only when the dependency is real but invisible, such as an IAM policy that must exist before a Lambda runs.
resource "aws_lambda_function" "processor" { function_name = "order-processor" role = aws_iam_role.lambda.arn handler = "app.handler" runtime = "python3.12" filename = "build/processor.zip" depends_on = [aws_iam_role_policy.lambda_s3_access] # no argument references this}TipPrefer a real attribute reference over
depends_on. It gives the right value and order, whiledepends_ononly orders and reduces parallelism.
Remember
depends_oncontrols order, not timing. It adds no delay for IAM or DNS propagation.
Frequently Asked Questions
When does Terraform need an explicit depends_on?
Only when a dependency is not expressed through any argument reference, like a policy attachment a service relies on at runtime.
Why can overusing depends_on hurt?
It removes parallelism and can defer data source reads to apply time, which makes plans slower and harder to read.