Home DevOps Terraform Module Dependency

Terraform Module Dependency

by admin
terraform module dependency screenshot

As of Terraform 0.13 you can create dependencies between your Terraform modules easily. Version 0.13 of Terraform has been available since August 2020, and introduced some enhancements to Terrafrom modules capability including countdepends_on and for_each functions. In this short article we will look at an example of how to use depends_on with Terraform modules.

Terraform typically does a great job of understanding dependencies in your Terraform plans, however sometimes it is useful to manually configure a Terraform module dependency to ensure resources deploy in the expected order.

Explicitly specifying a dependency is only necessary when a resource or module relies on some other resource’s behavior but doesn’t access any of that resource’s data in its arguments.

This is done using depends_on, and has been available for Terraform resources for quite a while. With Terraform 0.13 and later it’s possible to make a whole module dependent on another Terraform module. Next we will have a look at a quick example of how to create a Terraform module dependency.

Terraform Module depends_on Example

In the example below we are calling a module called mssql, and we have made it dependent on a module called network.

# Creates a Azure MS SQL Server
module "mssql" {
    source = "./modules/sql-server"
    depends_on = [module.network]
    resource_group_name = "azure-sql"
    location = "eastus" 
}

It’s as straight forward as that! The mssql module will only be deployed once the resources in the network module have been. Often using depends_on is generally best avoided, as Terraform does a pretty good job of working out dependencies on it’s own. However, it can be useful to know how to define module dependencies in Terraform if you have a reason to force the modules to deploy in a particular order.

You can check out examples of how to use depend_on between Terraform resources within a plan or module in this article covering how to deploy WVD using Terraform.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More