In any application with code, it is very important that the code is clean and clearly readable by all contributors (current and future) who will be involved in the maintenance and evolution of this code.
In IaC and with Terraform, it is even more important to have clear code because written code serving as documentation is an advantage of IaC.
In this recipe, we will look at how to use Terraform's command line to properly format its code and we will also see some tips for automating it.
Getting ready
To get started, we will start with a main.tf file that contains the following Terraform configuration:
As we can see, this code is not very readable; it needs to be better formatted.
To execute Terraform commands with the CLI, we use a command-line terminal (CMD, PowerShell, Bash, and so on) and the execution folder will be the folder containing the Terraform configuration of the recipe. This will apply to all recipes in this chapter.