Short for directed graph.
Takes a DOT-formatted digraph
where dependencies are expressed as dependent -> dependency
, determines the order in which they need to occur, and sorts the dependencies by earlier-to-later resolution.
With the -node
parameter, you can specify one of the nodes in the graph, and this will display the list of dependencies beginning with the node you specified. The intended use-case is to assume that node's dependencies are met, so just perform the work for that node and everything which depends on it.
Assuming you have the Go toolchain set up, run:
go get github.com/skyzyx/dgr
terragrunt graph-dependencies | dgr
My specific use-case is using Terragrunt to break apart monolithic Terraform into smaller, discrete units. (There's no need for an update to a Lambda function to execute the same monolithic Terraform as what powers my database.) These units can depend on each other, and Terragrunt knows how to track that.
By running terragrunt graph-dependencies
, you can get Graphviz (DOT) data which looks something like this:
digraph {
"ecs-cluster" ;
"resource-tags" ;
"public-dns" ;
"certificate" ;
"database" ;
"ecs-cluster" -> "resource-tags";
"ecs-cluster" -> "database";
"database" -> "resource-tags";
"public-dns" -> "ecs-cluster";
"certificate" -> "public-dns";
}
By piping that data into dgr
, it will read the directed acyclic graph (DAG) and sort the nodes in dependency order from lots of things depend on me → no things depend on me.
If you pass the name of one of the nodes to the -node
flag, it will give you a shorter version of the list starting with that node.
What this means is that if there is an update to one of my discrete units of Terraform (managed by Terragrunt), and I have other units which depend on that unit downstream, this will give me a list of the units which need to be re-applied in order for the changes to apply cleanly all the way through the stack.
I've tried to follow the Unix philosophy of doing one thing well, and supporting the ability to pipe things from one process to the next. I haven't broadly tested this (nor will I), but it should be able to sort nodes by their edges for any Graphviz-formatted digraph
.
Note that I have little idea how this graph stuff works. I've picked up a little here and there, but I don't understand it well enough to know how to implement it myself (sorry technical recruiters). I have an interest in learning, but I'd need someone to walk me though the algorithm(s) first.