Terraform

How to Generate Images with Terraform Graph Command

How to Generate Images with Terraform Graph Command

In this article, we will delve into the Terraform graph command, explaining what Terraform graphs are and how they are generated, how to use the command itself with examples, and a rundown of other visualization tools you should consider.

We will cover:

  1. What are Terraform graphs?
  2. How to use the Terraform graph command?
  3. Terraform graph examples
  4. Other Terraform visualization tools

What are Terraform graphs?

The Terraform graph command generates a visual representation of the dependency relationships between resources in your Terraform configuration or execution plan, helping you to understand the structure and dependencies within your infrastructure. These graphs can also be useful for identifying potential issues, planning changes, and debugging. Each resource is represented as a node, and the arrows between nodes represent the dependencies between resources.

Graphs are generated in the DOT output format, a text-based graph description language, which can be processed by GraphViz software to create visual diagrams (e.g., PNG, SVG).

The graph command does not create or apply any infrastructure changes; it’s purely for visualization. For large or complex graphs, breaking them down into smaller sections or using interactive visualizations (see later section on other available tools) can improve readability.

How to use the Terraform graph command?

To generate a Terraform graph type:

terraform graph [options]

The options available with this command:

  • -type= — Specify the type of graph to generate, which can be set to one of the following:
  • plan — Graph based on the current configuration.
  • plan-refresh-only — Graph-based on a refresh plan only.
  • plan-destroy — Graph-based on a plan for destroying resources.
  • apply — Graph-based on a saved execution plan.
  • -draw-cycles — Include cycles in the graph with colored edges (useful for identifying potential issues).
  • -plan=tfplan — Render a graph based on specified plan file instead of configuration files in the current working directory.

Terraform graph examples

My Terraform configuration file main.tf contains the following to create an nginx docker image:

terraform {
  required_providers {
    docker = {
      source  = "kreuzwerker/docker"
      version = "2.23.1"
    }
  }
}

provider "docker" {
  host = "tcp://localhost:2375"
  #host = "unix:///var/run/docker.sock" # Docker on ubuntu connection
}

# Creating a Docker Image ubuntu with the latest as the Tag.
resource "docker_image" "ubuntu" {
  name = "ubuntu:latest"
}

# Creating a Docker Container using the latest ubuntu image.
resource "docker_container" "webserver" {
  image             = docker_image.ubuntu.latest
  name              = "terraform-docker-test"
  must_run          = true
  publish_all_ports = true
  command = [
    "tail",
    "-f",
    "/dev/null"
  ]
}

resource "docker_image" "nginx" {
  name         = "nginx:latest"
  keep_locally = false
}

resource "docker_container" "nginx" {
  image = docker_image.nginx.latest
  name  = "nginx-test"
  ports {
    internal = 80
    external = 8000
  }
}

resource "docker_network" "private_network" {
  name = "my_network"
}

# node should be a swarm manager. Use "docker swarm init" or "docker swarm join" to connect
#resource "docker_secret" "foo" {
#  name = "foo"
#  data = base64encode("{\"foo\": \"s3cr3t\"}")
#}

resource "docker_volume" "shared_volume" {
  name = "shared_volume"
}

#The source image must exist on the machine running the docker daemon.
#resource "docker_tag" "tag" {
#  source_image = "xxxx"
#  target_image = "xxxx"
#}

To visualize the dependencies, I simply run:

terraform graph

This provides me with the code in DOT output format:

terraform graph output

I paste this code into the online graph viewer Webgraphviz, which generates my visual representation!

terraform dependency graph

If you have installed Graphviz, you can also generate the dot output file directly on the command line and then use the dot command to generate the PNG file:

terraform graph > graph.dot
dot -Tpng graph.dot -o graph.png
terraform resource graph

Other Terraform visualization tools

Visualizing complex graphs can be challenging, for more interactive or alternative visualizations, you can consider tools like Blast Radius, Inframap, Rover, or Terraform Visual. These tools offer different features.

1. Terraform Visual

Terraform Visual is a free and open-source tool specifically designed to visualize your Terraform plan interactively. To try it out, simply generate your plan file using the commands below and upload it here.

$ terraform plan -out=plan.out
$ terraform show -json plan.out > plan.json
terraform graph visualizer

Source

2. Blast radius

Blast radius is another great free option for Terraform visualization.

terraform graph example

Source

3. Inframap

Inframap reads your tfstate or HCL to generate a graph specific for each provider, showing only the resources that are most important/relevant.

terraform graph dot

Source

4. WebGraphviz and GraphvizOnline

Lastly, if you cannot make changes to your local system, online services like WebGraphviz and GraphvizOnline can render DOT files without installing software.

Key points

The terraform graph command in Terraform is used to generate a visual representation of the dependency graph of your infrastructure. There are many useful alternatives, so have fun experimenting with these and finding the right option for you.

We encourage you also to explore how Spacelift makes it easy to work with Terraform. If you need any help managing your Terraform infrastructure, building more complex workflows based on Terraform, and managing AWS credentials per run, instead of using a static pair on your local machine, Spacelift is a fantastic tool for this. It supports Git workflows, policy as code, programmatic configuration, context sharing, drift detection, and many more great features right out of the box. You can check it for free, by creating a trial account.

Note: New versions of Terraform are placed under the BUSL license, but everything created before version 1.5.x stays open-source. OpenTofu is an open-source version of Terraform that expands on Terraform’s existing concepts and offerings. It is a viable alternative to HashiCorp’s Terraform, being forked from Terraform version 1.5.6.

Manage Terraform Better with Spacelift

Build more complex workflows based on Terraform using policy as code, programmatic configuration, context sharing, drift detection, resource visualization and many more.

Start free trial

The Practitioner’s Guide to Scaling Infrastructure as Code

Transform your IaC management to scale

securely, efficiently, and productively

into the future.

ebook global banner
Share your data and download the guide