How to format Terraform code

Learn essential tips and best practices for formatting Terraform code efficiently in our comprehensive guide. Streamline your infrastructure today!

How to format Terraform code
How to format Terraform code

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 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, along with some other minor adjustments to improve readability of the files.

đź’ˇ
You might be interested in my book "Terraform for Ops", a guide to Terraform for SysAdmins. You can find out more information here https://www.terraformforops.com

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 bad formatting
Terraform file with bad formatting

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

Terraform file with correct formatting
Terraform file with correct formatting

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!