DEV Community

1suleyman
1suleyman

Posted on

🕸️ What Is Terraform Graph? (And Why It’s Like a Subway Map for Your Cloud)

Hey everyone 👋

If you're learning Terraform or trying to wrap your head around how all your resources connect, you need to know about a hidden gem: terraform graph.

When I first saw it, I thought:

“Wait… Terraform can draw diagrams of my infrastructure?”
And yes — it totally can.

Let me show you what it does, why it’s useful, and how you can start using it like a pro 👇


🗺️ Think of It Like a Subway Map (For Your Terraform Code)

Let’s say your infrastructure is a busy city.

  • EC2 instances are buildings
  • Load balancers are bridges
  • Security groups are fences
  • Elastic IPs are signposts

Now imagine trying to navigate that city just by reading street names from a spreadsheet 😵‍💫

That’s what Terraform code feels like… until you visualize it.

Terraform Graph builds a subway map of your infrastructure — showing which pieces connect, depend on each other, and get built first.


⚙️ What Exactly Is terraform graph?

It’s a command that shows the dependency relationships between all the resources in your .tf files.

You can:

✅ See which resources depend on others
✅ Spot indirect links (like a rule pointing to a security group)
✅ Debug tangled setups
✅ Visualize your architecture with tools like Graphviz


🧱 A Real Example – The Chain Reaction

Let’s say your code includes:

resource "aws_security_group" "example" { ... }

resource "aws_eip" "ip" { ... }

resource "aws_security_group_rule" "rule" {
  security_group_id = aws_security_group.example.id
  cidr_blocks       = [aws_eip.ip.public_ip]
}
Enter fullscreen mode Exit fullscreen mode

In a regular .tf file, that’s hard to untangle.

But with terraform graph, you’ll see:

aws_eip.ip ---> aws_security_group_rule.rule ---> aws_security_group.example
Enter fullscreen mode Exit fullscreen mode

Just like a subway map:
🚇 EIP → Rule → Security Group


🧪 How To Use It (The Fun Part)

  1. Run the command in your Terraform folder:
terraform graph
Enter fullscreen mode Exit fullscreen mode
  1. You’ll get a bunch of output in DOT language (used for rendering graphs)

  2. You can:

terraform graph | dot -Tsvg > graph.svg
Enter fullscreen mode Exit fullscreen mode
  1. Open the .svg file in your browser or VS Code — and boom 💥 You’ve got a visual map of your setup.


🧠 Why Use It?

Terraform Graph might not be as flashy as terraform plan or apply, but it’s a game changer when your project grows.

Here's why:

🔍 Use Case 💡 How Graph Helps
Planning See what gets built, and in what order
Debugging Spot missing or incorrect references
Team Communication Share architecture diagrams with teammates
Documentation Include visuals in your GitHub repos or wiki

🧱 What’s That DOT Language?

DOT is a special format used by Graphviz to describe graphs. It’s like Markdown — but for diagrams.

Terraform Graph outputs DOT text. You don’t have to read it, but it helps to know what it is.

And because it’s a standard format, there are tons of tools (online + offline) that can convert it into diagrams.


🔐 Security Tip – Don’t Paste Sensitive Graphs Online

If you’re working in a company setting and your infrastructure code includes private IPs, resource names, or sensitive architecture — be careful where you paste your DOT output.

Instead of using Graphviz websites, install it locally with:

sudo apt install graphviz
Enter fullscreen mode Exit fullscreen mode

Then run:

terraform graph | dot -Tsvg > safe-diagram.svg
Enter fullscreen mode Exit fullscreen mode

This keeps everything secure and offline 🔒


💡 Pro Tips

  • Use .svg instead of .png — easier to view and scale
  • Add the SVG file to your GitHub repo for documentation
  • Run terraform init first — otherwise graph might not work
  • Try terraform graph -draw-cycles to highlight loops

🧩 Final Thoughts

terraform graph is one of those underrated features that makes a huge difference — especially as your projects grow beyond a couple resources.

If you’re managing 20+ Terraform modules, or just want to “see” what’s going on behind the scenes — give it a try.
It helped me debug dependencies way faster and made documentation smoother too.

Got a cool Terraform visualization tip? Drop it in the comments or reach out on LinkedIn — I’d love to swap cloud stories ☁️🧠

Top comments (0)