Terraform Dependencies: Implicit vs. Explicit Explained

When you are creating resources through Infrastructure as Code (IaC) either you or our coding tool have to understand how each resource needs to be created.  Which order to deploy them in so they all deploy correctly. There are two ways of handling this challenge, implicit and explicit dependencies. In this blog post we’ll define what those two types of dependencies are, show you examples and share some best practices and common pitfalls. 

What are Terraform dependencies?

In Terraform, dependencies define the order in which resources should be created, updated, or destroyed. Since many infrastructure components rely on others (e.g., a virtual machine needing a network to connect to), Terraform must understand these relationships to deploy resources in the correct order.

Terraform has a built-in dependency management system that automatically determines the correct order for most resources. However, sometimes Terraform needs additional guidance, which is where explicit dependencies come into play.

There are two types of dependencies in Terraform:

  • Implicit Dependencies – Automatically detected when a resource references another resource’s attributes.
  • Explicit Dependencies – Manually defined using the depends_on meta-argument when Terraform cannot infer dependencies correctly.

Implicit dependencies in Terraform

An implicit dependency within Terraform is when Terraform automatically detects dependencies when a resource references another resource’s attributes. 

In the example below Terraform automatically understands that there is a dependency that the resource group needs to be greeted before the virtual network because we’ve referenced the resource group name in our virtual network configuration.

resource "azurerm_resource_group" "example" {
  name     = "rg-example"
  location = "UK South"
}

resource "azurerm_virtual_network" "example" {
  name                = "networking"
  resource_group_name = azurerm_resource_group.example.name
  location            = azurerm_resource_group.example.location
  address_space       = ["10.0.0.0/16"]
}

Explicit dependencies in Terraform

An explicit dependency within Terraform is when Terraform doesn’t detect dependencies correctly and you need to use the depends_on meta-argument to ensure resources are created in the correct order. 

In the example below we are making sure Terraform makes absolutely sure that things are created in the right order with our depends_on. Even though Terraform should be able to figure it out from other references we’re making it crystal clear. 

resource "azurerm_netapp_account" "example" {
  name                = "example-netapp-account"
  location            = azurerm_resource_group.example.location
  resource_group_name = azurerm_resource_group.example.name
}

resource "azurerm_netapp_pool" "example" {
  name                = "example-netapp-pool"
  location            = azurerm_resource_group.example.location
  resource_group_name = azurerm_resource_group.example.name
  account_name        = azurerm_netapp_account.example.name
  service_level       = "Premium"
  size_in_tb          = 4

  depends_on = [azurerm_netapp_account.example]
}

resource "azurerm_netapp_volume" "example" {
  name                = "example-netapp-volume"
  location            = azurerm_resource_group.example.location
  resource_group_name = azurerm_resource_group.example.name
  account_name        = azurerm_netapp_account.example.name
  pool_name           = azurerm_netapp_pool.example.name
  volume_path         = "examplevolume"
  service_level       = "Premium"
  subnet_id           = azurerm_subnet.example.id
  protocol            = "NFSv4.1"

  depends_on = [
    azurerm_netapp_account.example,
    azurerm_netapp_pool.example
  ]
}

Common pitfalls and best practices

Implicit dependencies should be your first port of call in Terraform since they are automatically detected and Terraform handles the ordering and deployment for you.  To minimise potential issues:

  • Always document why you’ve included an explicit dependency in your code so others looking at your code can understand why it was included. 
  • Try to use implicit dependencies as much as possible and avoid over use of explicit dependencies as it can add in complexity when it is not needed.
  • Think about how you write your code, sometimes organising your resources into modules can help management dependencies more efficiently. 
  • Remember to run terraform plan before applying changes as this can help detect any misconfigurations around dependencies. 

Conclusion

Terraform can automatically handle dependencies based on resource references but sometimes it needs help and you have to manually build dependencies into your code using the depends_on meta-argument. 

However, if you understand the concepts of implicit and explicit dependencies you can help to build smoother Terraform deployments and more robust infrastructure.