Managing Terraform resource dependencies
One of Terraform’s main features is to allow the parallelization of operations while considering resource dependencies.
In this recipe, we will learn how to create dependencies between resources. We will do this using both implicit and explicit dependencies.
Let’s get started!
Getting ready
To start this recipe, we will use the following Terraform configuration to provision an Azure resource group and, inside it, one Azure virtual network.
Here is the basic configuration:
resource "azurerm_resource_group" "rg" {
name = "rgdep"
location = "westeurope"
}
resource "azurerm_virtual_network" "vnet" {
name = "vnet"
location = "westeurope"
resource_group_name = "rgdep"
address_space = ["10.0.0.0/16"]
}
The problem with the above configuration is that there are no Terraform dependencies between the resource group and the virtual network. Since Terraform executes its operations according to its dependency graph calculation, during the apply
execution the virtual network can be created before the resource group. However, this isn’t acceptable because the virtual network must be created after the resource group.
The goal of this recipe is to learn how to create dependencies between the Azure virtual network and the Azure resource group.
Find out more about the concept of dependencies at https://www.terraform.io/language/resources/behavior#resource-dependencies.
The source code of this recipe is available at https://github.com/PacktPublishing/Terraform-Cookbook-Second-Edition/tree/main/CHAP02/dep.
How to do it…
Perform the following steps:
- To create implicit dependencies, update the
azurerm_virtual_network
resource with the following code:resource "azurerm_virtual_network" "vnet" { name = "vnet" location = "westeurope" resource_group_name = azurerm_resource_group.rg.name address_space = ["10.0.0.0/16"] }
- To create explicit dependencies, update the
azurerm_virtual_network
resource with the following code:resource "azurerm_virtual_network" "vnet" { name = "vnet" location = "westeurope" resource_group_name = "rgdep" address_space = ["10.0.0.0/16"] depends_on = [azurerm_resource_group.rg] }
How it works…
In the first configuration, we use an implicit dependency. By using the resource_group_name = azurerm_resource_group.rg.name
property, Terraform creates a dependency that is waiting for the creation of the resource so it can know its name and pass it to the virtual network.
In the second configuration, we use an explicit dependency by using the depends_on
meta-argument, which contains the list of resources that must be created before this current resource. Here, we explicitly set that the Azure resource group must be created before creating the Azure virtual network.
Finally, we execute the terraform apply
command. The following image shows that this execution has dependencies:
Figure 2.17: Terraform uses implicit and explicit dependency
We can see the order of resource creation: the Azure resource group [1] is created before the Azure virtual network [2].
And when we run the terraform destroy
command, the Azure virtual network is destroyed before the Azure resource group.
There’s more…
If we have the choice between using an implicit or explicit dependency, it’s recommended to use an implicit dependency as explained in this documentation: https://www.terraform.io/language/meta-arguments/depends_on#processing-and-planning-consequences
It is possible to display the Terraform resources graph’s dependencies by using the terraform graph
command, which we will learn about in detail in Chapter 6, Applying a Basic Terraform Workflow, in the Generating the graph dependencies recipe.
See also
- The Terraform tutorial on dependencies is available at https://learn.hashicorp.com/tutorials/terraform/dependencies.
- The documentation on the
depends_on
meta-argument is available at https://www.terraform.io/language/meta-arguments/depends_on.