0
0
Terraformcloud~5 mins

Depends_on for explicit dependencies in Terraform - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes Terraform needs to know the order to create resources. Depends_on lets you tell Terraform to wait for one resource before starting another. This avoids errors when one resource needs another to exist first.
When you have two resources that must be created in a specific order but Terraform does not detect the dependency automatically.
When a resource uses data or output from another resource that is not directly referenced in its configuration.
When you want to ensure a resource is fully created before starting another that depends on it.
When you face errors due to race conditions during resource creation.
When you want to control the deployment order explicitly for clarity or troubleshooting.
Config File - main.tf
main.tf
resource "aws_s3_bucket" "my_bucket" {
  bucket = "example-bucket-terraform-12345"
  acl    = "private"
}

resource "aws_s3_bucket_object" "my_object" {
  bucket = aws_s3_bucket.my_bucket.bucket
  key    = "example.txt"
  content = "Hello, Terraform!"

  depends_on = [aws_s3_bucket.my_bucket]
}

This file creates an S3 bucket first.

The second resource uploads an object to that bucket.

The depends_on ensures the bucket is created before the object upload starts.

Commands
This command initializes Terraform in the current directory. It downloads necessary provider plugins and prepares the environment.
Terminal
terraform init
Expected OutputExpected
Initializing the backend... Initializing provider plugins... - Finding latest version of hashicorp/aws... - Installing hashicorp/aws v4.0.0... - Installed hashicorp/aws v4.0.0 (signed by HashiCorp) Terraform has been successfully initialized! You may now begin working with Terraform. Try running "terraform plan" to see any changes that are required for your infrastructure.
This command applies the configuration to create the resources. The -auto-approve flag skips manual approval to speed up the process.
Terminal
terraform apply -auto-approve
Expected OutputExpected
aws_s3_bucket.my_bucket: Creating... aws_s3_bucket.my_bucket: Creation complete after 2s [id=example-bucket-terraform-12345] aws_s3_bucket_object.my_object: Creating... aws_s3_bucket_object.my_object: Creation complete after 1s [id=example.txt] Apply complete! Resources: 2 added, 0 changed, 0 destroyed.
-auto-approve - Automatically approve the apply without prompting
This command lists all resources Terraform is managing in the current state. It helps verify that both resources were created.
Terminal
terraform state list
Expected OutputExpected
aws_s3_bucket.my_bucket aws_s3_bucket_object.my_object
Key Concept

If you remember nothing else from this pattern, remember: depends_on tells Terraform the exact order to create resources when it cannot figure it out by itself.

Common Mistakes
Not using depends_on when a resource depends on another but the dependency is not explicit in the config.
Terraform may try to create resources in parallel, causing errors if one resource needs the other to exist first.
Add depends_on with the resource that must be created first to ensure proper order.
Using depends_on unnecessarily when Terraform already detects the dependency.
This can make the configuration more complex and slower by forcing unnecessary waits.
Only use depends_on when Terraform does not detect the dependency automatically.
Summary
Use terraform init to prepare your working directory.
Use depends_on in resource blocks to explicitly set creation order.
Use terraform apply to create resources and terraform state list to verify them.