Skip to content

/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

bash
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: 0

Without -check, fmt prints the names of the files it rewrote.

Options

OptionEffect
-checkdo not write; exit non-zero if any file needs formatting
-diffprint a unified diff of the changes
-recursivealso process subdirectories
-list=falsedo not print file names
-write=falsedo not write files (implied by -check)
FILE or DIRformat only the given file or directory

In CI

.github/workflows/terraform.yml (step)
- name: terraform fmt
  run: terraform fmt -check -recursive

Sources