| --- |
| page_title: terraform fmt command reference |
| description: >- |
| The `terraform fmt` command formats Terraform configuration contents so that it matches the canonical format |
| and style. |
| --- |
| |
| # `terraform fmt` command |
| |
| The `terraform fmt` command formats Terraform configuration file contents so that it matches the canonical format and style. This command applies a subset of |
| the [Terraform language style conventions](/terraform/language/style#code-formatting), |
| along with other minor adjustments for readability. |
| |
| ## Introduction |
| Terraform commands that generate Terraform configuration produce |
| configuration files that conform to the style imposed by `terraform fmt`. Follow this style in your files to ensure consistency. |
| |
| The canonical format may change in minor ways between Terraform versions, so |
| after upgrading Terraform we recommend to proactively run `terraform fmt` |
| on your modules along with any other changes you are making to adopt the new |
| version. |
| |
| We do not consider new formatting rules in `terraform fmt` to be a breaking |
| change in new versions of Terraform, but we minimize changes for |
| configurations that already follow the style examples shown in the |
| Terraform documentation. New formatting rules programmed into the `terraform fmt` |
| command are usually expansions to include existing rules from the documentation. |
| We recommend following the documented style even for decisions that `terraform fmt` |
| does not yet apply automatically. |
| |
| Formatting decisions are always subjective and so you might disagree with the |
| decisions that `terraform fmt` makes. This command is intentionally opinionated |
| and has no customization options because its primary goal is to encourage |
| consistency of style between different Terraform codebases, even though the |
| chosen style can never be everyone's favorite. |
| |
| We recommend that you follow the style conventions applied by `terraform fmt` |
| when writing Terraform modules, but if you find the results particularly |
| objectionable then you may choose not to use this command, and possibly choose |
| to use a third-party formatting tool instead. If you choose to use a |
| third-party tool then you should also run it on files that are generated |
| automatically by Terraform, to get consistency between your hand-written files |
| and the generated files. |
| |
| ## Usage |
| |
| Usage: `terraform fmt [options] [target...]` |
| |
| By default, the `terraform fmt` command scans your current directory for configuration files. You can also provide a `target` argument to tell `terraform fmt` to scan: |
| * A directory |
| * A specific file |
| * Standard input by supplying a single dash (`-`). |
| |
| The `terraform fmt` command accepts the following arguments. |
| |
| | Flag | Description | Required | |
| | :---- | :---- | :---- | |
| | `-list=false` | Prevents the command from listing the files containing formatting inconsistencies. | Optional | |
| | `-diff` | Displays the diffs of formatting changes. | Optional | |
| | `-write=false` | Prevents the command from overwriting files. This behavior is implied by the `-check` flag or if the input is from `STDIN`. | Optional | |
| | `-check` | Checks if the input is formatted. The exit status is `0` if the command's input is properly formatted. Otherwise, the exit status is non-zero, and the command outputs a list of improperly formatted file names. | Optional | |
| | `-recursive` | Processes files in subdirectories in addition to the current directory. By default, the command only processes the specified, or current, directory. | Optional | |