Featured image of post Understanding Terraform Dependencies on Azure - Terraform on Azure Part 5

Understanding Terraform Dependencies on Azure - Terraform on Azure Part 5

Dive into the intricate world of Terraform dependencies with this insightful guide. Learn how Terraform's dependency graph orchestrates and streamlines cloud infrastructure provisioning on Azure, complete with practical examples.

Introduction ๐ŸŒŸ

๐Ÿ‘‹ Welcome back, Everyone! In our previous adventure, we unraveled the mysteries of Terraform’s state file. Today, we’ll embark on a new quest to understand the heart of Terraform’s magic: the dependency graph. Mastering this concept is crucial for efficiently managing and provisioning cloud infrastructure with Terraform.

๐Ÿงฉ Terraform’s Dependency Graph: The Recipe for Infrastructure ๐Ÿณ

Terraform’s dependency graph is like a recipe for your infrastructure. Just as a recipe specifies the order in which ingredients should be added, the dependency graph determines the order in which your resources should be created.

In technical terms, the dependency graph is an internal map in Terraform that shows how various resources, like virtual networks or resource groups, depend on each other. This means that certain resources must be created or configured before others can be.

Terra the robot analyzing terraform dependency graph in cloud computing

๐Ÿ” How Terraform Detects Dependencies

Let’s consider an example where we’re setting up an Azure Virtual Network. We need to define a Resource Group and a Virtual Network, and they need to work together:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
# Resource Group
resource "azurerm_resource_group" "iaMachsRG" {
  name     = "iaMachsResourceGroup"
  location = "East US"
}

# Virtual Network
resource "azurerm_virtual_network" "example" {
  name                = "iaMachsRG-network"
  address_space       = ["10.0.0.0/16"]
  location            = azurerm_resource_group.iaMachsRG.location
  resource_group_name = azurerm_resource_group.iaMachsRG.name
}

Here’s the magical part: Terraform automatically detects that our Virtual Network depends on the Resource Group. Why? Because the VNet is using or referencing attributes of the Resource Group, particularly the location and the name attributes.

Now, Terraform doesn’t just stop at detecting dependencies. It goes a step further:

  • Order of Creation: It decides which resources should be created first. In our example, the Resource Group is created before the Virtual Network.
  • Change Propagation: Changes in the Resource Group, like location updates, are automatically reflected in the dependent Virtual Network.
  • Destruction Order: When deleting resources, Terraform ensures dependent ones, like the Virtual Network, are removed before the Resource Group.

๐Ÿ”จ Example: Azure Virtual Network Setup

Let’s solidify our understanding of how Terraform manages dependencies and provisions resources by creating an Azure Virtual Network.

  1. Initial Setup: Configure the Azure Terraform provider.

    1
    2
    3
    
    provider "azurerm" {
      features {}
    }
    
  2. Resource Group: Define the azurerm_resource_group first.

    1
    2
    3
    4
    
    resource "azurerm_resource_group" "iaMachsRG" {
      name     = "iaMachsResourceGroup"
      location = "East US"
    }
    
  3. Virtual Network: Define the azurerm_virtual_network, referencing the Resource Group.

    1
    2
    3
    4
    5
    6
    
    resource "azurerm_virtual_network" "example" {
      name                = "iaMachsRG-network"
      address_space       = ["10.0.0.0/16"]
      location            = azurerm_resource_group.iaMachsRG.location
      resource_group_name = azurerm_resource_group.iaMachsRG.name
    }
    
  4. Run Terraform Commands: Use terraform init, terraform plan, and terraform apply to create the resources.

๐Ÿš€ Try it out yourself! Experiment with different configurations and observe how the dependency graph changes.

๐Ÿ“„ Understanding the Output

Executing terraform plan reveals Terraform’s action sequence. It’ll show you the creation order for the resource group and virtual network, respecting their dependencies:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following      
symbols:
  + create

Terraform will perform the following actions:

  # azurerm_resource_group.iaMachsRG will be created
  + resource "azurerm_resource_group" "iaMachsRG" {
      + id       = (known after apply)
      + location = "eastus"
      + name     = "iaMachsResourceGroup"
    }

  # azurerm_virtual_network.example will be created
  + resource "azurerm_virtual_network" "example" {
      + address_space       = [
          + "10.0.0.0/16",
        ]
      + dns_servers         = (known after apply)
      + guid                = (known after apply)
      + id                  = (known after apply)
      + location            = "eastus"
      + name                = "iaMachsRG-network"
      + resource_group_name = "iaMachsResourceGroup"
      + subnet              = (known after apply)
    }

Plan: 2 to add, 0 to change, 0 to destroy.

In this plan, Terraform indicates that it will first create the azurerm_resource_group.iaMachsRG (+ means create), followed by the azurerm_virtual_network.example. The order reflects the dependency of the virtual network on the resource group.

After running terraform apply, verify in the Azure portal that the resources have been created as expected.

Azure Virtual Network configuration iaMachsRG-example

๐Ÿงน Clean Up: Avoid Unwanted Costs!

๐Ÿš€ Experimented with Terraform? Remember, leaving resources active might lead to extra costs. It’s good practice to clean up.

Use terraform destroy to remove the infrastructure. This command will analyze the dependency graph and delete resources in the reverse order of their creation. Confirm with yes and watch Terraform do the cleanup. After the cleanup, your state file will be empty, reflecting that no resources are managed by Terraform.

Wrapping Up ๐ŸŽ

In this adventure, we’ve seen how Terraform’s dependency graph acts as a recipe for your infrastructure. By understanding and leveraging dependencies, you can create complex infrastructures with ease.

๐ŸŒŸ Next, we’ll venture into creating an Azure virtual machine using Terraform. Get ready for more Terraform thrills!

Stay tuned and Happy Terraforming! ๐Ÿš€