Format Terraform configuration files
Formats Terraform configuration files to canonical style with options to check, preview changes, or process recursively.
/plugin marketplace add JosiahSiegel/claude-plugin-marketplace/plugin install terraform-master@claude-plugin-marketplace[-check] [-recursive] [-diff]Format Terraform configuration files to canonical style.
/tf-fmt # Format current directory
/tf-fmt -check # Check without changing
/tf-fmt -recursive # Format all subdirectories
/tf-fmt -diff # Show what would change
# Check without changes
terraform fmt -check
# Check with diff output
terraform fmt -check -diff
# Format current directory
terraform fmt
# Format recursively
terraform fmt -recursive
# Format specific file
terraform fmt main.tf
| Option | Description |
|---|---|
-check | Check only, don't modify |
-diff | Show formatting changes |
-recursive | Process subdirectories |
-write=false | Don't write changes |
-list=false | Don't list formatted files |
- name: Terraform Format Check
id: fmt
run: terraform fmt -check -recursive
continue-on-error: true
- name: Format Status
if: steps.fmt.outcome == 'failure'
run: |
echo "Terraform files are not formatted!"
terraform fmt -recursive -diff
exit 1
# .pre-commit-config.yaml
repos:
- repo: https://github.com/antonbabenko/pre-commit-terraform
rev: v1.83.5
hooks:
- id: terraform_fmt
#!/bin/sh
# .git/hooks/pre-commit
# Check Terraform formatting
if ! terraform fmt -check -recursive; then
echo "Terraform files are not formatted. Run 'terraform fmt -recursive'"
exit 1
fi
| Code | Meaning |
|---|---|
| 0 | All files formatted (or no changes needed) |
| 1 | Formatting errors found (with -check) |
| 2 | Command error |
terraform fmt -check -recursive
terraform fmt -diff -recursive
terraform fmt -recursive
# Output: main.tf
# modules/vpc/main.tf
terraform fmt -recursive -list=false
= signs in blocksresource "aws_instance" "web" {
ami = "ami-12345678"
instance_type="t3.micro"
tags={Name="web"}
}
resource "aws_instance" "web" {
ami = "ami-12345678"
instance_type = "t3.micro"
tags = {
Name = "web"
}
}