terraform graph — 依存関係を可視化する

1. 概要

  • terraform graphの基本的な使い方
  • 出力をGraphvizで可視化する
  • 依存関係を読み解く方法
  • どんな場面で役立つか

terraform graphはTerraformの依存関係グラフをDOT形式で出力するコマンドです。リソース間の依存関係を視覚的に把握でき、循環依存のデバッグや大規模構成の全体像把握に役立ちます。


2. 基本的な使い方

# 依存関係グラフをDOT形式で出力
$ terraform graph

# Graphvizでpng画像に変換
$ terraform graph | dot -Tpng -o graph.png

# SVGで出力(より鮮明)
$ terraform graph | dot -Tsvg -o graph.svg

# planのグラフ(実際に変更されるリソースのみ)
$ terraform graph -type=plan

# applyのグラフ
$ terraform graph -type=apply

出力されるDOT形式は以下のようなテキストです。

digraph {
    compound = "true"
    newrank  = "true"
    subgraph "root" {
        "[root] aws_instance.web (expand)" -> "[root] aws_subnet.private (expand)"
        "[root] aws_instance.web (expand)" -> "[root] data.aws_ami.amazon_linux_2023 (expand)"
        "[root] aws_subnet.private (expand)" -> "[root] aws_vpc.main (expand)"
    }
}

3. Graphvizのインストール

# macOS
$ brew install graphviz

# Ubuntu / Debian
$ apt-get install graphviz

# インストール確認
$ dot -V
dot - graphviz version 9.0.0

4. 依存関係グラフの実例

terraform {
  required_version = ">= 1.9"
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 5.0"
    }
  }
}

provider "aws" {
  region = "ap-northeast-1"
}

variable "environment" {
  description = "環境名"
  type        = string
  default     = "dev"
}

data "aws_ami" "amazon_linux_2023" {
  most_recent = true
  owners      = ["amazon"]
  filter {
    name   = "name"
    values = ["al2023-ami-*-x86_64"]
  }
}

resource "aws_vpc" "main" {
  cidr_block = "10.0.0.0/16"

  tags = {
    Name        = "${var.environment}-vpc"
    Environment = var.environment
    ManagedBy   = "terraform"
  }
}

resource "aws_subnet" "private" {
  vpc_id     = aws_vpc.main.id  # VPCへの依存
  cidr_block = "10.0.1.0/24"

  tags = {
    Name        = "${var.environment}-private"
    Environment = var.environment
    ManagedBy   = "terraform"
  }
}

resource "aws_instance" "web" {
  ami           = data.aws_ami.amazon_linux_2023.id  # AMIへの依存
  instance_type = "t3.micro"
  subnet_id     = aws_subnet.private.id             # サブネットへの依存

  root_block_device {
    volume_size = 20
    volume_type = "gp3"
    encrypted   = true
  }

  tags = {
    Name        = "${var.environment}-web"
    Environment = var.environment
    ManagedBy   = "terraform"
  }
}

このコードにterraform graph | dot -Tpng -o graph.pngを実行すると、data.aws_amiaws_instanceaws_subnetaws_vpcという依存関係が矢印で可視化されます。


5. graphが役立つ場面

循環依存(cycle detected)のデバッグ

Terraformが循環依存エラーを報告した際、グラフを可視化することでどのリソース間でループが発生しているかを把握できます。

大規模構成の全体把握

数十以上のリソースを持つ構成では、コードを読むだけでは全体像が掴みにくいことがあります。グラフはアーキテクチャレビューや新メンバーのオンボーディングにも使えます。

並列実行の理解

Terraformは依存関係のないリソースを並列に実行します。グラフで並列実行できるリソースのグループを確認することで、applyの所要時間を見積もれます。


6. -draw-cyclesオプション

# 循環依存を強調表示する(デバッグ用)
$ terraform graph -draw-cycles | dot -Tpng -o cycles.png

7. 関連記事


8. まとめ

  • terraform graphでリソースの依存関係をDOT形式で出力できる
  • dot -TpngでPNG画像に変換して視覚化できる(Graphvizが必要)
  • -type=plan / -type=applyで対象を絞れる
  • 循環依存のデバッグ・アーキテクチャ全体把握・並列実行の確認に役立つ
  • -draw-cyclesで循環依存箇所を強調表示できる

動作確認バージョン: Terraform >= 1.9 公式ドキュメント: https://developer.hashicorp.com/terraform/cli/commands/graph