Visualize terraform dependency graph

Modern cloud infrastructures are composed of multiple small services. That makes it easy to maintain each of them as single distinct units. The downside of this approach is that dependencies can become really complex. Terraform manages these dependencies in a dependency graph.

Let’s say we want to create an EC2 Auto Scaling group on AWS. We will have to create a Launch Template first and reference that in our Auto Scaling group resource. Terraform uses that reference to determine in what order it has to create our resources.

We can view the dependency graph with

terraform graph

The output is in dot language. To view it as an image we have to compile it with the dot command which is part of the graphviz package. Finally we pipe it into an image viewer that is capable to read from stdin.

terraform graph | dot -Tpng | feh -

A very cool feature, isn’t it? And if you are curious now about dot I can recommend to spend an hour learning it. It is worth it.