How to use Terraform Graph to visualize your execution plan

terraform
Reading Time: 3 minutes

With terraform, we can generate a visual representation of either our configuration or execution plan. We can use terraform graph command to generate a visual representation.

The output of the command is in DOT format, we can use tools like Graphviz to generate charts.

Let’s Understand in detail.

Explore the directory first!

Let’s see what terraform configuration files are present in our working directory.

By looking at the above snapshot you must have got an understanding of what we are trying to achieve through this configuration.

Let’s understand each file in our directory one by one:

  • version.tf: In this file we have defined the required terraform versions that we can use with these configurations.
  • variables.tf: In variables.tf we have defined all the variables that we are using in our terraform configurations.
  • ec2-instance.sh: Here, we have defined the configuration for creating the ec2-instance.
  • ami-datasource.tf: This file is to fetch the AMI for ec2 instance using datasource.
  • apache-install.sh: This file contains the script to install apache webserver on ec2 instance.
  • security-groups.tf: Security groups acts as virtual firewall for ec2 instances to control incoming and outgoing traffic.Therefore, In this file we are creating security groups for vpc in which we are creating the ec2 instances. 
  • outputs.tf: This file, we have configured to output the public ip and dns url of ec2 instance.

Let’s visualize the configurations

First, make sure to initialize your working directory with “terraform init” to download the required providers.

With terraform, we can run the “terraform graph” command inside our directory. This will generate a DOT format code.

We can easily copy this code and make use of tools like GraphViz to analyze and visualize our terraform configurations.

Similarly, we can also use the online Graphviz website to get a visual representation of the terraform configurations.

Let’s see this in action!

So, as soon as we copy our Dot code generated by “terraform graph” on the left section of the Graphviz website, it generates a beautiful graph for us on the right section, from this graph we can visualize our infrastructure.

Let’s see the generated graph more clearly to see what it looks like:

So, we can visualize every component of our Terraform configuration files and can also verify that the ec2 instance is attached with correct security groups, data sources, etc.

Since with terraform graph we get to know each and every detail of every component/resource that will be created, we can make use of these visual representations if we are deploying a very complex infrastructure and we need to double-check how our infrastructure will look after applying the configurations.

In addition, with terraform graph command we can use the “-draw-cycles” option to highlight any cycles in the graph with colored edges, this helps in diagnosing cycle errors.

Conclusion

In Conclusion, we learned how we can create graphs with the help of Terraform and can make use of the visual representation while deploying complex infrastructure to make sure everything is in place.

Happy Learning!

Written by 

DevOps Engineer at Knoldus. Loves to solve new problems. Always eager to learn new technologies.

Leave a Reply