How to Format Terraform Code
Tags: terraform, devops, infrastructureascode
Published: 2023-09-12 14:00:00

Does poorly formatted code upset you? If so, it’s essential you avoid creating such code yourself. Most programming languages adhere to specific style guidelines, and [HashiCorp Terraform](https://www.techielass.com/introduction-to-terraform/) language is certainly no exception.

A single misaligned bracket or excessive indentation can render your Terraform configuration files hard to read and maintain. However, by employing the _terraform fmt_ command you can avoid these coding inconsistencies. 

In this blog post, I want to explore the _fmt_ command and help you understand when and where to use it. 

## What is Terraform fmt?

The _terraform fmt_ command is used to format your Terraform configuration files into a canonical format and style.  When the command is run it applies a set of [Terraform language style conventions](https://www.terraform.io/language/syntax/style), along with some other minor adjustments to improve readability of the files. 

## What does the Terraform fmt check do?

When you run the _terraform fmt_ command by default it will look in the current directory and apply formatting to all the .tf files there.  

There is an option within the _terraform fmt_ command which allows you to process all files in subdirectories as well, that would be _terraform fmt -recursive_

It will look to correct any style conventions that you have not followed, an example would be fixing spacing indents. 

Sometimes the canonical formatting may change between Terraform versions so it’s good practice after a Terraform upgrade to run the _terraform fmt_ against your code to adopt any new changes. 

## Using the Terraform fmt Command 

Let’s see the _terraform fmt_ command being used to format a Terraform file. I have this main.tf file that looks like it was hastily written and has some bad formatting. 

![Terraform file with the wrong format](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5x4dffvtpo4j5p0wmv4v.png)

After I run the _terraform fmt_ command, you can see the formatting of the file is much better!

![Terraform file with the correct format](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t6aiyfljy7wv08xv9865.png)

Those extra spaces have been removed that were making the file hard to write.  However you will notice the extra line breaks at line 30 and 31 haven’t been removed, those would need to be manually removed if required. 


## Conclusion

In conclusion, the importance of well-formatted code cannot be overstated, and this principle holds true for HashiCorp Terraform configuration files as well. The slightest code irregularities, such as misaligned brackets or excessive indentation, can lead to frustration and hinder the maintainability of your Terraform projects.

Fortunately, the terraform fmt command comes to the rescue. Remember that the benefits of utilizing terraform fmt extend beyond mere aesthetics. It promotes code consistency, eases collaboration among team members, and ensures your configurations remain compatible with evolving Terraform versions.

So, the next time you find yourself grappling with unruly Terraform code, reach for_ terraform fmt_ to bring order and clarity to your projects. Your future self and collaborators will thank you for it. 

Happy coding!