06 Oct 2019

Terraform Resource Creation Order

Terraform Resource Creation Order

Creating resources in Terraform may present a problem for users familiar with sequential tools like Chef.

Terraform generates a graph of the dependency tree prior to planning the order of changes. This means your Terraform file is a desired state, not an official order.

For example, creating a subnet within a VNET using the name value as shown below will cause issues because Terraform doesn’t understand that it needs to create the VNET before the subnet, even though we provide the eventual name of the VNET (VNET-NAME).

resource "azurerm_virtual_network" "vnet" {
    name = "VNET-NAME"
}
resource "azurerm_subnet" "vnet_subnet" {
    name = "MY-SUBNET"
    virtual_network_name = "VNET-NAME"
}

To tell Terraform to create the VNET first, use an output variable from the VNET (azurerm_virtual_network.vnet.name).


resource "azurerm_virtual_network" "vnet" {
    name = "VNET-NAME"
}
resource "azurerm_subnet" "vnet_subnet" {
    name = "MY-SUBNET"
    virtual_network_name = azurerm_virtual_network.vnet.name
}

This works with any output variable from a resource. Lets say both resources use the same resource group. You can force the order by using the output resource group name from the first Terraform resource and pipe it into the second Terraform resource. The resource group names have no significance in relation to dependency, but Terraform will assume there is a dependency through the usage of the output variables.

resource "azurerm_virtual_network" "vnet" {
    name = "VNET-NAME"
    resource_group_name = "RESOURCE-GROUP"
}

resource "azurerm_subnet" "vnet_subnet" {
    name = "MY-SUBNET"
    virtual_network_name = "VNET-NAME"
    resource_group_name = azurerm_virtual_network.vnet.resource_group_name
}

Defining Dependencies

Terraform also allows you to explicitly define dependencies for each resource, using depends_on, which accepts an array of resources.

This can be helpful in situations where it may be hard to pass outputs from one resource to another. The example below tells Terraform that the resource vnet_subnet depends on azurerm_virtual_network.vnet.


resource "azurerm_virtual_network" "vnet" {
    name = "VNET-NAME"
}
resource "azurerm_subnet" "vnet_subnet" {
    name = "MY-SUBNET"
    virtual_network_name = azurerm_virtual_network.vnet.name
    depends_on = [azurerm_virtual_network.vnet]
}

Learn More

For a more in-depth explanation, see Terraform Resource Dependencies.

Edited by Melissa Kendall

Latest Posts

Terraform Resource Creation Order

06 Oct 2019

Terraform Resource Creation Order

Creating resources in Terraform may present a problem for users familiar with sequential tools like Chef.

Terraform generates a graph of the dependency tree prior to planning the order of changes. This means your Terraform file is a desired state, not an official order.

...
Making The Case For Simpler Processes

24 Sep 2019

Making The Case For Simpler Processes

Code is only valuable when it’s in production. Until then, it’s a liability. It is essential that we focus both on quickly delivering high quality software to our users and improving our processes to make that delivery easier.

High performing IT organizations recognize that software development is a process that begins with an idea, and finishes with code in the hands of their users, and tries to optimize the complete process, not just parts.

...
Building Scalable Build and Deployment Pipelines

24 Sep 2019

Building Scalable Build and Deployment Pipelines

Build and Deployment Pipelines are not a sexy subject within our industry. It’s often an afterthought relegated to the last minute and cobbled together. It’s also extremely hard to find resources from people who’ve been in the trenches and seen what has worked and what hasn’t. This article is an attempt at sharing the knowledge I have from building Build and Deployment Pipelines that have deployed applications that served billions of requests daily.

You may not agree with everything that is documented here and that’s ok! If you are going to deviate, deviate with purpose!

...