Terraform Tips & Tricks, Issue 1: Format, Graph and State

Illustration of a person with light-colored hair casting a magical spell over a bubbling cauldron, with sparks flying and drawings of potion ingredients in the background. Illustration of a person with light-colored hair casting a magical spell over a bubbling cauldron, with sparks flying and drawings of potion ingredients in the background.

The goal of Terraform Lightning Course is to introduce you to Terraform, so that you know how it works and how to use it. But there are many little features and nuances in day to day Terraform usage. In these bonus articles I will show some of them.

In this issue of Terraform Tips and Tricks, I will show you 3 more Terraform commands.

The first one is super easy: terraform fmt, or terraform format. If you programmed in Go, it will be feel familiar to you. Let's look at our Template here.

Screenshot of a text editor displaying Terraform configuration code for provisioning infrastructure, including a provider, SSH key resource, device resource, and output for a public IP.

It's nice, but it could be much nicer. Let me run terraform fmt and open the Template again.

A computer screen displaying a Terraform configuration file in a text editor, with code for provisioning infrastructure, including sections for provider, project, SSH key, and device.

Now it's much nicer formatted and follows all the formatting best practices, automatically. You should run terraform fmt before commiting your changes.

The second command is terraform graph. What it does is outputs you the dependency graph of the resources in your template, in a DOT format.

Screenshot of a command-line interface showing text output of a Terraform graph command, with terminal prompt at the bottom indicating the master branch of a Git repository.

You can even visualize it with GraphViz. It's not too useful, but helps to understand what we learn in the second part of the Terraform Lightning Course.

The last command for today is terraform state. terraform state has lots of subcommands to view and manipulate the Terraform state file.

Screenshot of a terminal with Terraform CLI output showing usage information and a list of subcommands related to Terraform state management.

Let's look at just one of them, terraform state list.

A computer terminal screenshot showing a command line interface with a 'terraform state list' command and its output, within a 'mkdev-terraform git:(master)' directory.

Now we can see what is already in the state file.

That's it for this article.

DevOps consulting: DevOps is a cultural and technological journey. We'll be thrilled to be your guides on any part of this journey. About consulting


Here's the same article in video form for your convenience: