How to Use depends_on in Terraform for Resource Dependencies
In Terraform, use the
depends_on argument inside a resource or module block to explicitly specify dependencies on other resources. This ensures Terraform creates or updates resources in the correct order when implicit dependencies are not enough.Syntax
The depends_on argument takes a list of resource or module references that the current resource depends on. Terraform waits for these dependencies to be created or updated before processing the current resource.
- depends_on: A list of resource or module addresses.
- Placed inside a resource or module block.
terraform
resource "aws_instance" "example" { ami = "ami-12345678" instance_type = "t2.micro" depends_on = [aws_security_group.sg] } resource "aws_security_group" "sg" { name = "example-sg" description = "Allow SSH" ingress { from_port = 22 to_port = 22 protocol = "tcp" cidr_blocks = ["0.0.0.0/0"] } }
Example
This example shows an AWS EC2 instance that depends on a security group. Terraform ensures the security group is created before the instance.
terraform
resource "aws_security_group" "web_sg" { name = "web-sg" description = "Allow HTTP" ingress { from_port = 80 to_port = 80 protocol = "tcp" cidr_blocks = ["0.0.0.0/0"] } } resource "aws_instance" "web_server" { ami = "ami-0c55b159cbfafe1f0" instance_type = "t2.micro" depends_on = [aws_security_group.web_sg] tags = { Name = "WebServer" } }
Output
Terraform will create the aws_security_group.web_sg first, then create aws_instance.web_server after the security group is ready.
Common Pitfalls
Common mistakes when using depends_on include:
- Using
depends_onunnecessarily when Terraform already detects dependencies from resource references, which can slow down your plan and apply. - Referencing resources incorrectly inside
depends_on(must use full resource address). - Trying to use
depends_oninsidevariableoroutputblocks, which is not supported.
terraform
resource "aws_instance" "example" { ami = "ami-12345678" instance_type = "t2.micro" # Wrong: depends_on with a string instead of a list depends_on = aws_security_group.sg } # Correct usage: resource "aws_instance" "example" { ami = "ami-12345678" instance_type = "t2.micro" depends_on = [aws_security_group.sg] }
Quick Reference
Use depends_on to:
- Force resource creation order when implicit dependencies are missing.
- Control module execution order.
- Prevent race conditions in complex infrastructure.
Remember:
depends_onaccepts a list of resource or module addresses.- It is optional and should be used only when needed.
- Terraform automatically detects dependencies from resource references.
Key Takeaways
Use
depends_on to explicitly declare resource dependencies in Terraform.Place
depends_on inside resource or module blocks as a list of references.Avoid unnecessary use of
depends_on to keep Terraform plans efficient.Terraform automatically detects dependencies from resource references, so use
depends_on only when implicit detection is insufficient.Do not use
depends_on in variables or outputs; it only works in resources and modules.