liveBook · Manning

Terraform graph

As explained in Terraform in Depth: Infrastructure as Code with Terraform and OpenTofu

terraform graph is a Terraform subcommand that outputs a graph representation of the configuration or infrastructure dependency graph.

terraform graph outputs a GraphViz DOT representation of Terraform’s configuration or infrastructure dependency graph. Inspecting that graph reveals resource dependencies, helps explain indirect effects, and can expose circular dependencies that are difficult to find otherwise. section 5.2.2 section 5.8

Use the command to:

  • Inspect plan, plan-refresh-only, plan-destroy, or apply graphs with the -type flag. section 5.2.2
  • Pipe DOT output through GraphViz’s dot tool to create viewable SVG or PNG diagrams. section 5.2.2
  • Review dependencies when a resource change affects other resources that are not obviously connected. section 5.2.2
  • Use a saved plan for apply graphs; without one, apply output can be empty because of a bug. section 5.2.2

Render a TLS module graph

From the directory containing the Terraform configuration, run `terraform graph | dot -Tpng > graph.png`. Terraform emits the dependency graph as DOT, and dot converts it into a PNG that is usually easier to interpret than CLI text. For a reusable DOT file, run `terraform graph > graph.dot`; for SVG, run `terraform graph | dot -Tsvg > graph.svg`. section 5.2.2

How Terraform in Depth: Infrastructure as Code with Terraform and OpenTofu covers it

How deep it goes
Explains graph output, visualization modes, and conversion commands, but does not detail Terraform’s graph-construction algorithm.
Examples
Uses a TLS module graph and demonstrates DOT, SVG, and PNG output commands.
What it assumes
Assumes familiarity with Terraform configurations, plans, and resources while introducing GraphViz and DOT output usage.
What you can do after
Diagnose dependency relationships and circular dependencies with terraform graph and GraphViz-rendered diagrams.
How it connects
Connects Terraform plans, dependency graphs, directed acyclic graphs, GraphViz, and the terraform plan and terraform apply commands.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Terraform in Depth: Infrastructure as Code with Terraform and OpenTofu. The section links point to the parts of the book it draws on.