Skip to main content
RunBook Academy

← All labs in Terraform

Lab · advanced · ~20 min

Lab: depends_on and Implicit Dependencies

C · Simulation

Objectives

  • Demonstrate the lab scenario
  • Verify the expected outcome

Prerequisites

Objective

By the end of this lab, you will have:

  • Created a configuration with implicit dependencies through references.
  • Created a configuration with explicit dependencies through depends_on.
  • Visualized the dependency graph with terraform graph.
  • Recognised when depends_on is appropriate.

Requirements

  • A Linux or macOS workstation with shell access.
  • The Terraform CLI 1.9.x or later installed.
  • Graphviz (optional, for visualising the graph).

Scenario

You have a configuration with two resources where one depends on the other. The dependency can be implicit (through a reference) or explicit (through depends_on). The choice has implications for clarity and correctness.

Tasks

Task 1: Create the working directory

mkdir -p ~/rb-deps-lab
cd ~/rb-deps-lab

Task 2: Create the implicit-dependency configuration

Create main.tf:

terraform {
  required_version = ">= 1.9.0"
  required_providers {
    local = {
      source  = "hashicorp/local"
      version = "~> 2.5"
    }
  }
}

resource "local_file" "readme" {
  filename = "${path.module}/README.md"
  content  = "Initial content.\n"
}

resource "local_file" "followup" {
  filename = "${path.module}/followup.md"
  content  = "local_file.readme.content = ${local_file.readme.content}"
}

The local_file.followup references local_file.readme.content. The dependency is implicit.

Task 3: Apply the configuration

terraform init
terraform apply

Verify the files:

cat README.md
cat followup.md

The followup.md file contains the content of README.md.

Task 4: Visualize the dependency graph

terraform graph > graph.dot

(If Graphviz is installed:)

dot -Tpng graph.dot > graph.png

The graph shows local_file.readme -> local_file.followup.

Task 5: Create the explicit-dependency configuration

Edit main.tf to use depends_on:

resource "local_file" "followup" {
  filename = "${path.module}/followup.md"
  content  = "Followup content.\n"

  depends_on = [local_file.readme]
}

The content does not reference local_file.readme.content. The dependency is explicit.

Task 6: Apply the configuration

terraform apply

The plan shows both resources being updated. The followup resource is updated after the readme resource.

Task 7: Visualize the dependency graph

terraform graph > graph.dot

The graph shows the same dependency: local_file.readme -> local_file.followup.

Task 8: Recognise when depends_on is appropriate

The depends_on is appropriate when:

  • The dependency is on a side effect (e.g. an IAM role).
  • The reference is to a known-after-apply attribute.
  • The implicit dependency is not strong enough.

The depends_on is not appropriate when:

  • The dependency is already implicit (a reference to the resource).
  • The dependency is to an unrelated resource.

Validation

The lab is successful if:

  • The implicit-dependency configuration created both files.
  • The explicit-dependency configuration created both files.
  • The dependency graph shows the right relationship.

Expected Outcome

At the end of the lab:

+---------------------------------+
| ~/rb-deps-lab/                     |
|   .terraform/                    |
|   .terraform.lock.hcl            |
|   README.md                       |
|   followup.md                     |
|   main.tf                        |
+---------------------------------+

The followup.md file contains the content of README.md.

Cleanup

cd ~/rb-deps-lab
terraform destroy
rm -rf .terraform .terraform.lock.hcl terraform.tfstate*

What You Learned

You learned the dependency mechanism:

  • Implicit dependencies are created by references. Terraform infers the dependency from the expression.
  • Explicit dependencies are created by depends_on. The operator declares the dependency.
  • Most dependencies should be implicit. The reference is the documentation.
  • depends_on is for side effects. When the dependency is on a side effect, the reference is not enough.

Deliverables

  • · A configuration with implicit dependencies
  • · A configuration with explicit dependencies
  • · A demonstration of the dependency graph

Verification status

Last reviewed
2026-08-12
Executed end to end
not yet run on hardware

The commands and configuration here have been reviewed against the verified software versions, but nobody has run this lab start to finish on a system meeting its prerequisites. Treat the Expected Outcome as the intended result rather than an observed one, and keep the Cleanup section to hand.