Terraform depends_on: What it is, When to use it, and Best Practices
When working with Terraform, managing resource dependencies effectively is key to avoiding deployment issues. Terraform is great at automatically determining the order of resource creation, but sometimes it needs a little help, this is where depends_on comes in.
In this guide, we’ll explain Terraform depends_on, how to use it, when to use it, and best practices for writing clean and efficient Terraform code.
What is Terraform depends_on?
Terraform’s depends_on is a meta-argument that allows you to specify an explicit relationship between resources. When you use it you can enforce an order in which resources should be created or updated.
When you configure Terraform, it analyses its dependency graph to understand the relationships between different resources. This graph helps Terraform determine the order in which resources should be created, updated, or destroyed. But, this might not always be enough when you are dealing with complex or non-obvious relationships between resources.
This is where the Terraform depends_on meta-argument can be used to add explicit dependencies.
How to use depends_on in Terraform
Using the depends_on meta-argument in Terraform is straightforward. Here is an example of syntax:
resource "azure_resource_type" "resource_name" {
#resource configuration
depends_on = [azure_resource_type.dependent_resource_name]
}
A bit more of a working example would be:
resource "azurerm_resource_group" "example" {
name = "example-rg"
location = "East US"
}
resource "azurerm_storage_account" "example" {
name = "examplestoracct"
resource_group_name = azurerm_resource_group.example.name
location = azurerm_resource_group.example.location
account_tier = "Standard"
account_replication_type = "LRS"
}
resource "azurerm_virtual_machine" "example" {
name = "example-vm"
resource_group_name = azurerm_resource_group.example.name
location = azurerm_resource_group.example.location
network_interface_ids = []
vm_size = "Standard_B1s"
depends_on = [azurerm_storage_account.example]
}
Now in the example above you don't necessarily need to ensure the storage account to be created before the virtual machine, but you may have a specific use case for doing this.
Remember that the depends_on meta-argument will accept a list of resources, enabling you to tell the Terraform code that multiple dependencies exist.
When to use depends_on in Terraform
You should only really use the depends_on within your Terraform code when the implicit dependencies are not enough.
Here are some situations where the depends_on will make sense:
- Sometimes resources take a long time to fully provision, you can use the depends_on to help you manage this timing issue.
- When working with Terraform provisioners (like local-exec and remote-exec) run scripts or commands during deployment. Terraform doesn’t track script execution state so depends_on may help ensure things are deployed in the correct order.
- Terraform modules may not always recognise dependencies between separate module calls, you can use depend_on to force execution order.
Best practices for Terraform depends_on
The depends_on meta-argument is a powerful tool, however, overuse or incorrect use of it can lead to complications in your Terraform configurations. Here are some best practices to consider when working with it:
- Rely on implicit dependencies whenever possible, only resort to depends_on when there is no other viable option.
- When you do use the depends_on option, make sure you document why so other team members, or you at a later date understand why it’s there and can avoid adding in unintended complexity.
- Be sure to check during version upgrades or providers or modules if any changes affect the implicit or explicit dependencies, as sometimes updates can lead to unexpected behaviour in your code.
- Best sure to test any dependencies you implement into your code and ensure it causes no issues.
Conclusion
Terraform’s depends_on meta-argument is a useful tool when implicit dependencies aren’t enough, ensuring that resources are created in the right order.
By following best practices, such as relying on implicit dependencies where possible, documenting explicit dependencies, and testing your configurations, you can keep your Terraform code clean, efficient, and maintainable.
Use depends_on wisely, and your Terraform deployments will be more reliable and predictable—helping you avoid unnecessary complexity and troubleshooting headaches.