Skip to content

fmt

Usage

The terraform fmt command is a command-line tool that is used to automatically format Terraform configuration files. Here are some key points about the terraform fmt command:

  • The terraform fmt command is used to rewrite Terraform configuration files in a standard format.
  • It can be used to format all configuration files in a directory, or specific files specified on the command line.
  • The command updates the configuration files in place, but can also be run in “check” mode to determine if any changes would be made.
  • The terraform fmt command is useful for ensuring that your Terraform configurations are consistently formatted and easy to read.
  • It can be run manually, or integrated into your development workflow as part of a build or continuous integration process.

Options

-check

The -check option in terraform fmt checks if configuration files are formatted according to Terraform’s style conventions without rewriting the files. If any files are not correctly formatted, it returns a non-zero exit code and prints which files need to be reformatted. This option can be useful in automation scripts or as part of a continuous integration process.

Example usage:

  • terraform fmt -check

-list=false

The -list=false option in terraform fmt suppresses the output of the names of the files that were changed. Use it when you want to format configuration files without seeing the list of changed files.

Example usage:

  • terraform fmt -list=false

By default, terraform fmt prints the names of the files that were changed. Use the -check option to check if configuration files are formatted according to Terraform’s style conventions without rewriting the files. This option can be useful in automation scripts or as part of a continuous integration process.

-write=false

The -write=false option in the terraform fmt command displays the formatted configuration without saving changes to the file. It can be used with a specific file, like main.tf, to preview changes. By default, terraform fmt rewrites configuration files in place, updating them to match the standard format. Use -write=false to preview changes without updating files.

Example usage:

  • terraform fmt -write=false main.tf

-diff

The terraform fmt command is used to format Terraform configuration files. It rewrites files in a standard format and can be used to format all files in a directory or specific files specified on the command line. Use -check to determine if any changes would be made, -diff to display a diff of the changes, and -write=false to preview changes without updating files. This command is useful for ensuring consistently formatted and readable configurations. Example usage:

  • terraform fmt -diff main.tf

-recursive

The recursive option in terraform fmt is used to recursively process subdirectories. This means that it will process all .tf and .tfvars files in the current directory and its subdirectories. It's useful for large Terraform projects with multiple modules organized into subdirectories.

By default, terraform fmt only processes files in the current directory, but using recursive extends it to subdirectories.

Example usage:

  • terraform fmt -recursive.