Visualize Terraform Config using Graphviz

Summary

The ‘terraform graph‘ command is utilized to generate a visual representation of either a configuration or an execution plan. This command produces a DOT format output.

In this post, we will explore how to generate an image or a visually appealing graphical representation of Terraform resources and their dependencies using the DOT file we generate. To achieve this, we will use Graphviz, an open-source graph visualization software.

I won’t delve into the details of the Terraform code, assuming you already have a ‘main.tf‘ file along with other Terraform code written in configuration files. As an example, I am using the following Terraform configuration file. I will demonstrate how to convert it to a DOT file and generate a graphical representation of the code using Graphviz.

Sample-main.tf

terraform {
  required_version = ">= 0.15"
  required_providers {
    local = {
      source  = "hashicorp/local"
      version = "~> 2.0"
    }
  }
}
resource "local_file" "Myfile" {
  filename = "techpress_test.txt"
  content  = <<-EOT
    Lorem Ipsum is simply dummy text of the printing and typesetting industry.
    only five centuries, but also the leap into electronic typesetting, remaining
    essentially unchanged. It was popularised in the 1960s with the release
    iece of classical Latin literature years old. Richard McClintock a Latin professor 
    Virginia, looked up one of the more obscure Latin.
    EOT
}

Terraform Initialize

  • Initialize the Terraform Plugin first using terraform init
terraform init
Terraform Initialize
Terraform Initialize

Terraform Graph Command

Use the below command to generate a DOT file.

terraform graph > graph.dot
Terraform Graph Command
Terraform Graph Command

Now, in the ‘LocalFileExample‘ folder, you will find two files: one is your ‘main.tf‘ file, and the other is the ‘mygraph.dot‘ file generated using the ‘terraform graph’ command. Proceed to the next step.

Install GraphViz Tool on a Windows PC

The next step is to utilize the ‘mygraph.dot’ file and use the Graphviz tool to convert it into a graphical representation format. First, we need to download the Graphviz tool on a Windows PC. Although it can be installed on Linux or Mac machines as well, I have used a Windows Installer for this demo.

Download and install the latest stable version from the available options. The installation is straightforward, following a simple wizard. One thing to note is to ensure that Graphviz is added to the PATH environment variable so that it can be used directly from the command line.

Thankfully, this step is made easy by the wizard by selecting the option to add Graphviz to the PATH environment variable. You can either add the environment variable for all users or just the current user.

  • Launch Graphwiz Setup and click on Next.
Install GraphViz Tool on a Windows PC
Install GraphViz Tool on a Windows PC
  • Click on I Agree.
Install GraphViz Tool on a Windows PC
Install GraphViz Tool on a Windows PC
  • Select “Add Graphwiz to the system PATH for current user“. Click Next.
Install GraphViz Tool on a Windows PC
Install GraphViz Tool on a Windows PC
  • Click Next.
Install GraphViz Tool on a Windows PC
  • Click Next.
Install GraphViz Tool on a Windows PC
Install GraphViz Tool on a Windows PC

Once Graphviz is installed, you need to close and reopen the command prompt window so that the environment PATH variables added by the wizard can be picked up by the console.

Test Graphviz installation

To confirm if Graphviz has been installed and is working fine, open a command prompt on a Windows 10/11 machine and run the command ‘dot -v‘ to check the version. If it returns the output and shows the version along with other information, then it’s working fine.

dot -v
Test Graphviz installation

Convert DOT file to SVG Image

Graphviz supports various output formats that you can utilize for converting a DOT file. Today, I will be using the SVG format. Once the DOT file is converted to SVG format, it can be opened in any internet browser.

I am providing two commands that can be used to convert a DOT file to SVG. Either of the commands can be used. The first command is suitable when you have already run the ‘terraform graph‘ command and generated the ‘mygraph.dot‘ file.

dot -Tsvg mygraph.dot -o mygraph1.svg

The following command uses ‘terraform graph‘ and the output is piped to the ‘dot‘ command, essentially generating the DOT format and converting it to ‘mygraph.svg.’ In this case, a DOT file is not created separately.

terraform graph | dot -Tsvg > mygraph.svg
terraform graph | dot -Tsvg > mygraph.svg
Convert DOT file to SVG Image

Open the SVG File

You can open the SVG file using any internet browser, which will display all the resource nodes and dependencies between resources.

Open SVG File
Open the SVG File

Leave a Comment

Discover more from TechPress

Subscribe now to keep reading and get access to the full archive.

Continue reading