Terraform configurations can grow complex, with interdependent resources spanning multiple modules and providers. The terraform graph
command generates a visual representation of these dependencies, giving you better insight into how your infrastructure is connected.
terraform graph
. This outputs the graph in DOT format, which can be used with graph visualization tools like Graphviz.terraform graph | dot -Tpng > graph.png
.Suppose your configuration includes a VPC, subnets, and EC2 instances. To generate a graph of their dependencies, run:
terraform graph
The output will look like this (simplified):
digraph {
"module.vpc.aws_vpc.main" -> "module.vpc.aws_subnet.public"
"module.vpc.aws_subnet.public" -> "aws_instance.example"
}
To convert this into a visual graph, use Graphviz:
terraform graph | dot -Tpng > graph.png
The resulting image will display your resources as nodes and their dependencies as arrows, providing a clear picture of the relationships in your infrastructure.
Imagine you’re working on a large configuration and need to optimize resource dependencies for faster deployments. By visualizing the dependency graph, you can identify bottlenecks and refactor configurations for improved efficiency.
The terraform graph
command helps you make sense of complex configurations by providing a clear view of resource dependencies. Whether debugging issues or optimizing deployments, this command is a valuable addition to your Terraform toolkit.