Table of Contents
- Summary
- Terraform Initialize
- Terraform Graph Command
- Install GraphViz Tool on Windows PC
- Test Graphviz installation
- Convert DOT file to Image
- Open SVG File

Summary
Terraform graph
command is used to generate visual representation of either a configuration or execution plan. The command generates a DOT format Output. In the post we will see how to generate an image / nice graphical representation of Terraform resources and their dependencies using the DOT file we will generate. We will use Graphviz which is an open-source graph visualization software.
I will not go in detail of the Terraform code and assume that you already have main.tf
file along with other terraform code written in configuration files. As an example, i am using below terraform configuration file which i will use it to convert to a DOT file and generate a graphical representation of the code using graphviz.
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
and then use terraform graph
command to generate a dot file.
terraform init

Terraform Graph Command
terraform graph > graph.dot

Now you will have two files in LocalFileExample Folder. One is your main.tf
file and other one is mygraph.dot
file generated using terraform graph
command.
Install GraphViz Tool on Windows PC
Next step is utilize mygraph.dot file and use the Graphviz tool to convert it into a graphical representation format. First we need to download Graphviz tool on a Windows PC. 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. Installation is straightforward by following a simple wizard. Only thing to note is to ensure that Graphviz is added to the PATH enviornment variable so that it can be used directly from command line. Thankfully this step is made easy by the wizard by selecting the option to add Graphviz to the PATH Enviornment variable. You can either Add the Enviornment variable for all users or just current user only.





Once Graphviz is installed, you need to close the reopen the command prompt window so that the Enviornment PATH variables which was added by the wizard can be picked up by the console.
Test Graphviz installation
To confirm if Graph has been installed and working fine. Open a command prompt on windows 10 machine and run a command dot -v
to check the version. If it returns the output and shows the version along with other information then its working fine.

Convert DOT file to Image
Graphviz supports a lot of output formats which you can utilize for converting a DOT File. Today, I will be using SVG Format. Once the DOT file is converted to SVG Format, this can be opened in any internet browser.
I am showing two commands which can be used to convert dot file to svg. Either of the command can be used. First command can be used when you already have run terraform graph command and generated mygraph.dot file.
dot -Tsvg mygraph.dot -o mygraph1.svg
Below Command is using Terraform graph and output of the command is piped to the dot command. Basically generating dot format and converting it to mygraph.svg. In this case dot file is not getting created.
terraform graph | dot -Tsvg > mygraph.svg

Open SVG File
You can open svg file using any internet browser which shows all the resource nodes and dependencies between resources.
