terraform-fmt
Format Terraform configuration files
TLDR
Format the configuration in the current directory
Format the configuration in the current directory and subdirectories
Display diffs of formatting changes
Do not list files that were formatted to stdout
SYNOPSIS
terraform fmt [options] [path...]
PARAMETERS
-check
Checks if the input files are already formatted. The command exits with a non-zero status code (3) if any files are not correctly formatted, making it suitable for CI/CD pipelines.
-diff
Displays the differences (diff) between the current file content and what terraform fmt would produce, without applying any changes to the files.
-list=false
By default (-list=true), terraform fmt lists the names of files whose formatting would change. Setting this to false suppresses that output.
-write=false
By default (-write=true), terraform fmt writes the formatted content back to the original files. Setting this to false prevents in-place modification, useful when combined with -diff or -check.
-recursive
Recursively formats files in subdirectories. This is the default behavior when no specific paths are provided or when a directory path is given. This flag can be set to -recursive=false to prevent recursion.
-compact
When used with -check or -diff, output only filenames to stdout, one per line. This is particularly useful for scripting.
-no-color
Disable color output in the console.
-json
Output machine-readable JSON for -check or -diff results. This is useful for programmatic integration.
DESCRIPTION
The terraform fmt command is a crucial utility for maintaining consistent code style and readability in Terraform configurations. It automatically rewrites Terraform configuration files (.tf, .tfvars, and .tf.json) to a canonical format, ensuring adherence to standard indentation, spacing, and line breaking rules. This helps improve readability, reduces merge conflicts in version control systems, and enforces a common style across teams.
By default, terraform fmt processes all relevant files in the current directory and its subdirectories recursively, applying changes in-place. It's an idempotent operation, meaning running it multiple times on the same files will produce the same result once formatted. While it ensures stylistic consistency, it does not validate the syntax or configuration logic beyond what's necessary for parsing the HCL.
CAVEATS
terraform fmt modifies files in place by default. It's highly recommended to use version control (e.g., Git) to manage your Terraform configurations and commit changes before running fmt, or to use the -diff or -check flags first to preview changes. The command aims to enforce a single, opinionated style and does not offer options for custom formatting rules.
DEFAULT BEHAVIOR
If no specific paths are provided, terraform fmt defaults to recursively finding and formatting all .tf, .tfvars, and .tf.json files within the current working directory and its subdirectories. It writes changes directly to these files.
EXIT CODES
The command can return different exit codes:
0: Success; files were formatted or were already correctly formatted.
1: An error occurred during execution (e.g., syntax error in a file, I/O error).
3: Used specifically with the -check flag; indicates that one or more files were not correctly formatted according to terraform fmt's rules.
HISTORY
The terraform fmt command has been an integral part of the Terraform CLI since its early versions, evolving alongside the HashiCorp Configuration Language (HCL). Its introduction simplified codebase maintenance for individual users and, more importantly, enabled large teams to enforce a consistent coding style, which became critical as Terraform adoption grew for managing complex infrastructure across various cloud providers.