Terraform Best Practices

Chapter 1 · Practice 2 of 4

Run fmt, validate and a linter before every commit

From Structure and style

Why it matters

Formatting arguments in a review is a waste of everyone's time. The style guide says to run terraform fmt and terraform validate before each commit, ideally from a Git pre-commit hook, and to use TFLint or a similar linter for the rules Terraform does not check itself.

CommandCatches
terraform fmt -recursiveIndentation, alignment, spacing
terraform validateSyntax errors, wrong argument names, bad references
tflintProvider-specific mistakes (invalid instance types) and your own conventions

Run the same commands in CI with terraform fmt -check -recursive, so a missed hook cannot slip unformatted code into main.

To check, introduce a typo in an argument name on a branch and confirm the pipeline fails before any plan runs.

Example

Shell
terraform fmt -check -recursive
terraform init -backend=false
terraform validate
tflint --recursive