/commands/terraform/terraform-fmt-check
terraform fmt -check and -recursive: format HCL files
verified onenvironmentlocal · windows 10.0.26200 arm64 · GNU bash 5.3.15 (Git Bash) · Terraform v1.16.6updated 2026-10-20
- tool
- Terraform
- versions
- Terraform 1.x, OpenTofu 1.x
- os
- any
terraform fmt rewrites .tf and .tfvars files in the current directory into Terraform's canonical style: two-space indentation and aligned = signs. terraform fmt -check changes nothing; it lists the files that need formatting and exits with a non-zero status, which makes it a one-line CI check. Add -recursive to include subdirectories such as modules/.
Check, show the diff, fix
cat > main.tf <<'EOF'
resource "terraform_data" "app" {
input="x"
triggers_replace = [1]
}
EOF
printf 'variable "cidr" {\ndefault="10.0.0.0/16"\n}\n' > variables.tf
terraform fmt -check; echo "exit status: $?"
terraform fmt -diff -no-color
terraform fmt -check; echo "exit status: $?"Output
main.tf
variables.tf
exit status: 3
main.tf
--- old/main.tf
+++ new/main.tf
@@ -1,4 +1,4 @@
resource "terraform_data" "app" {
-input="x"
- triggers_replace = [1]
+ input = "x"
+ triggers_replace = [1]
}
variables.tf
--- old/variables.tf
+++ new/variables.tf
@@ -1,3 +1,3 @@
variable "cidr" {
-default="10.0.0.0/16"
+ default = "10.0.0.0/16"
}
exit status: 0Without -check, fmt prints the names of the files it rewrote.
Options
| Option | Effect |
|---|---|
| -check | do not write; exit non-zero if any file needs formatting |
| -diff | print a unified diff of the changes |
| -recursive | also process subdirectories |
| -list=false | do not print file names |
| -write=false | do not write files (implied by -check) |
| FILE or DIR | format only the given file or directory |
In CI
- name: terraform fmt
run: terraform fmt -check -recursiveSources
Related pages
cheat sheets · terraformTerraform cheat sheet: init, plan, apply, state and workspacesTerraform cheat sheet: init and upgrade, fmt and validate, plan and apply, outputs, state list, mv and rm, import, workspaces, and common language syntax.codes · terraformterraform plan -detailed-exitcode: exit codes 0, 1 and 2With -detailed-exitcode, terraform plan exits 0 when there are no changes, 1 on error and 2 when the plan has changes. Use it in CI for drift detection.errors · terraformTerraform: Error: Unsupported argument. An argument named "x" is not expected hereTerraform fails with Unsupported argument, An argument named "name" is not expected here, when a block sets an attribute its resource or module lacks.