DevTools Logo

Terraform DAG Diagrammer

Terraform DAG Diagrammer

Turn Terraform HCL into a Mermaid dependency graph in your browser

Runs in your browser
Try an example

Terraform HCL

35 lines · 587 chars

Mermaid dependency graph

8 blocks · 9 edges
Arrows point from a block to what it depends on; dashed arrows come only from depends_on. Text analysis only: terraform is never run.
flowchart TD
  tf_aws_instance_web[aws_instance.web]
  tf_aws_internet_gateway_gw[aws_internet_gateway.gw]
  tf_aws_subnet_app[aws_subnet.app]
  tf_aws_vpc_main[aws_vpc.main]
  tf_data_aws_ami_ubuntu[aws_ami.ubuntu]
  tf_local_name[local.name]
  tf_output_web_id[output.web_id]
  tf_var_env[var.env]
  tf_aws_instance_web -.-> tf_aws_internet_gateway_gw
  tf_aws_instance_web --> tf_aws_subnet_app
  tf_aws_instance_web --> tf_aws_vpc_main
  tf_aws_instance_web --> tf_data_aws_ami_ubuntu
  tf_aws_internet_gateway_gw --> tf_aws_vpc_main
  tf_aws_subnet_app --> tf_aws_vpc_main
  tf_aws_vpc_main --> tf_local_name
  tf_local_name --> tf_var_env
  tf_output_web_id --> tf_aws_instance_web

Examples

Reference linking with string masking

Input
buildTerraformDag('resource "aws_vpc" "main" { cidr_block = var.cidr }\nresource "aws_subnet" "app" { vpc_id = aws_vpc.main.id note = "aws_vpc.fake.id" }')
Output
edges: [aws_subnet.app → aws_vpc.main], references of main include var.cidr

Real HCL references link blocks while the quoted fake address stays masked, and var.cidr is recorded without becoming a node.

Two-resource cycle

Input
buildTerraformDag('resource "b" "two" { depends_on = [a.one] }\nresource "a" "one" { depends_on = [b.two] }')
Output
cycles: [["a.one", "b.two", "a.one"]], mermaid nodes tf_a_one, tf_b_two

Mutual references produce one cycle path, both addresses in cyclicResources, and deterministic Mermaid node IDs.

About this tool

Terraform DAG Diagrammer parses resource and data blocks with buildTerraformDag(source) (also exported as parseTerraformHcl and analyzeTerraformDag): headers match resource/data "type" "name" {, brace depth is tracked on comment- and string-masked HCL, and blocks sort deterministically by address with data blocks prefixed data..

References resolve only against known block addresses: bare type.name.attr links become edges, while var., local., and module. hits are kept in each block's references list but never become nodes or edges. Comments and quoted strings are masked before scanning, so a resource-looking header inside a string literal is ignored.

Cycles surface explicitly via depth-first search: cycles holds each loop path (e.g. a.one → b.two → a.one) and cyclicResources lists the sorted participants, and the Mermaid output always starts with flowchart TD followed by sorted node lines then sorted edges.

How to use this tool

  1. Paste Terraform HCL

    Drop resource and data blocks into the input — variables, locals, and outputs without blocks are skipped automatically.

  2. Read nodes and edges

    Check the sorted block addresses and from → to edges to confirm each dependency points at a defined block.

  3. Inspect cycles and Mermaid

    Review cycles and cyclicResources for loops, then copy the flowchart TD diagram into Mermaid for visualization.

Use cases

Visualizing module dependencies

Turn a pile of resource blocks into a sorted edge list and Mermaid flowchart for review.

Hunting dependency cycles

Surface mutual depends_on or interpolation loops via cycles before running terraform plan.

Auditing orphan references

Compare each block's references against edges to spot var/local inputs versus true block links.

Common mistakes

Mistake:Expecting var. and local. entries to appear as graph nodes.

Fix:They are kept in references only — edges and Mermaid nodes are built solely from known resource/data addresses.

Mistake:Assuming a quoted resource name creates a block.

Fix:Strings and comments are masked before header scanning, so headers inside quotes are never parsed as blocks.

Mistake:Reading source order as graph order.

Fix:Blocks, references, edges, and Mermaid lines all sort with localeCompare — input order never affects the output.

Frequently asked questions

What does this tool do?

It parses resource and data blocks with buildTerraformDag (aliases parseTerraformHcl, analyzeTerraformDag), links type.name references into sorted edges, detects cycles, and emits a flowchart TD Mermaid diagram with deterministic node IDs.

Do var, local, and module references become nodes?

No. They are recorded in each block's references list but never become nodes or edges — only references matching a known resource or data address produce edges.

Is my Terraform code uploaded or stored?

No. Masking, block parsing, cycle detection, and Mermaid rendering are 100% client-side — your HCL never leaves your browser.

Why does a resource name inside a string not create a block or edge?

Comments and quoted strings are masked before scanning, so headers or addresses inside string literals are ignored and only real HCL references link blocks.

References & standards