Skip to the content.

Exercise 3: Resource Dependencies in Terraform

Objectives

In this exercise, you will:

  1. Learn about implicit and explicit dependencies in Terraform
  2. Understand how to use depends_on meta-argument
  3. Use terraform graph to visualize the dependency graph
  4. Work with dependent resources across different provider types
  5. Understand how dependencies affect resource creation and destruction order

Prerequisites

Core Concepts

Implicit Dependencies

Terraform automatically determines dependencies between resources when one resource references attributes of another. For example, when a subnet references a VPC’s ID, Terraform understands that the VPC must exist before the subnet can be created.

Explicit Dependencies

Sometimes dependencies aren’t obvious from the configuration. In these cases, you can use the depends_on meta-argument to explicitly declare a dependency.

Dependency Graph

Terraform builds a dependency graph to determine the order in which resources should be created, modified, or destroyed. You can visualize this graph using the terraform graph command.

Exercise Overview

In this exercise, you will create a Terraform configuration that demonstrates both implicit and explicit dependencies. You’ll build a multi-component system and explore how dependencies affect the order of operations.

The exercise includes implementations for both AWS and GCP, allowing you to choose the cloud provider you’re most comfortable with.

Step 1: Examine the Configuration Files

Before making any changes, review the starter files to understand the resources that will be created and their relationships.

Step 2: Identify Implicit Dependencies

In the main configuration file, identify the implicit dependencies between resources. Consider:

Step 3: Add Explicit Dependencies

Modify the configuration to add explicit dependencies using the depends_on meta-argument. Consider scenarios where:

Step 4: Visualize the Dependency Graph

Use the terraform graph command to visualize the dependency graph of your configuration. This will help you understand how Terraform plans to create, modify, or destroy resources.

terraform graph | dot -Tpng > graph.png

Note: You’ll need Graphviz installed to convert the output to an image.

Step 5: Test Resource Creation Order

Apply the configuration and observe the order in which resources are created. Does this match your expectations based on the dependencies you identified?

Step 6: Test Resource Destruction Order

Plan a destroy operation and observe the order in which resources would be destroyed. Note that destruction happens in the reverse order of creation.

terraform plan -destroy

Learning Outcomes

After completing this exercise, you will:

Additional Challenges

  1. Create a configuration with circular dependencies and observe how Terraform handles them
  2. Experiment with the terraform graph command’s different output formats
  3. Create a configuration where a resource depends on another resource’s data, not just its existence
  4. Implement a configuration that demonstrates dependencies across different provider types

Next Steps

After completing this exercise, move on to Exercise 4: Working with State, where you’ll learn about Terraform’s state management capabilities.