Terraform fmt

Command: fmt

The terraform fmt command is used to rewrite Terraform configuration files to a canonical format and style. This command applies a subset of the Terraform language style conventions, along with other minor adjustments for readability.

Usage

Command:

terraform fmt [options] [DIR]

By default, fmt scans the current directory for configuration files. If the dir argument is provided then it will scan that given directory instead. If dir is a single dash (-) then fmt will read from standard input (STDIN).

The command-line flags are all optional.

1/ Check if your code folder has been formatted:

/usr/local/src/terraform/terraform_0.12 fmt -check terraform/

Terraform will list all the files that fmt command make changes. Note that -check flag is optional

Note:

If your folder contain subfolder, you need to use -recursive option:

/usr/local/src/terraform/terraform_0.12 fmt -recursive terraform/

2/ Compare changes to the original file:

/usr/local/src/terraform/terraform_0.12 fmt -diff terraform/

Your changes will be listed in the console

3/ What if your folder has been formatted?

Exit status will be 0 if all input is properly formatted

In addition to diff by fmt with -diff option, you can use git diff command to see the changes compared to previous version in GitLab