Formatting, validation and TFLint

Beginner · 8 min

The three cheap checks to run on every commit: terraform fmt, terraform validate and TFLint, what each knows and what each misses, in the order to run them.

Before thisInit, validate, plan, apply and destroy in practiceVersion constraints and the lock file

networknutsdotnet · 8 min

Formatting, validation and TFLint

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by networknutsdotnetReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • terraform fmt -check -recursive -diff fails a build on unformatted files; without -recursive subdirectories are skipped.
  • terraform validate checks syntax, types and references without touching remote services; init -backend=false lets it run with no credentials.
  • TFLint adds rules about good practice, such as required_version, typed variables, deprecated interpolation, unused declarations and pinned modules, plus provider plugins like tflint-ruleset-aws.
  • Run fmt, init -backend=false, validate and tflint before any plan; none needs cloud credentials.

Three checks that cost nothing

A pull request arrives. The reviewer spends ten minutes on whitespace, a variable with no type, and a provider with no version constraint. None of that needs a human. Terraform and one extra tool can catch it in seconds, before the plan runs and before anyone has to read the code.

Three checks form the cheap first layer, in order of how much they know:

Check Knows about Needs
terraform fmt Layout of the file Nothing
terraform validate Syntax, types, references init first
tflint Rules about good practice and provider-specific mistakes A config file and plugins

None of them talks to your cloud account, which is why they are safe to run on every commit.

terraform fmt

fmt rewrites .tf files into the canonical layout: indentation, aligned = signs in a block, spacing. The CLI docs call it a subset of the language style conventions plus minor readability adjustments. In CI you do not want it to rewrite files, you want it to fail:

terraform fmt -check -recursive -diff

-check exits 0 if everything is formatted and non-zero otherwise, and lists the files that are not. -recursive descends into subdirectories, which it does not do by default, so forgetting it means envs/prod/app is never checked. -diff prints what would change, so the CI log tells the author how to fix it. Locally, terraform fmt -recursive just fixes everything.

OpenTofu has the same command, tofu fmt.

terraform validate

validate checks that the configuration is syntactically valid and internally consistent: attribute names exist on each resource, values have the right types, references resolve. It does this without variable values or state. The docs are explicit about what it does not do: it “does not validate remote services, such as remote state or provider APIs.” A valid configuration can still fail at apply because a name is taken or you lack permission.

It needs an initialised directory, because it has to read provider schemas. In CI you can skip the backend:

terraform init -backend=false
terraform validate -json

-backend=false installs providers and modules without touching remote state, so a lint job needs no credentials. -json gives valid, error_count, warning_count and a list of diagnostics, which is what editor integrations read.

The docs point to terraform plan for validation against specific variable values or a workspace, since plan includes an implied validation check.

TFLint

Neither of the above knows what a good Terraform repo looks like. TFLint, an open-source linter, does. It is a separate binary. Its rules are in plugins, and one plugin, the Terraform language ruleset, is bundled. A minimal .tflint.hcl:

plugin "terraform" {
  enabled = true
  preset  = "recommended"
}

Run tflint --init to install declared plugins, then tflint --recursive. --format can emit JSON, SARIF or JUnit for CI tools.

I looked at the ruleset’s docs on GitHub on 28 November 2026. It has 20 rules. With preset = "recommended" only those marked recommended are on; others, such as terraform_documented_variables, terraform_naming_convention and terraform_standard_module_structure, you opt into. The recommended ones include:

  • terraform_required_version and terraform_required_providers: the terraform block pins a version and every provider has a constraint, which is the pinning discipline in a rule.
  • terraform_typed_variables: a variable with no type is flagged.
  • terraform_deprecated_interpolation: "${var.name}" where var.name would do.
  • terraform_unused_declarations: variables, locals and data sources nobody reads.
  • terraform_module_pinned_source and terraform_module_version: modules must be pinned.

The AWS provider has its own plugin, tflint-ruleset-aws, v0.50.0 as of 23 October 2026, with rules that flag values the provider would reject at apply time, such as an invalid database instance class.

Seeing what it catches

TFLint is a Go binary and I could not install it here, so I did not run it. To show the idea I wrote a stand-in in Python that implements five of the recommended rules above by hand, with a brace-counting parser and some regexes, and ran it on a deliberately sloppy file. It is not TFLint and does not match its output format.

$ python3 -I minilint.py lint
Warning: terraform block has no required_version (terraform_required_version)
Warning: provider "aws" has no version constraint in required_providers (terraform_required_providers)
Warning: variable "name" has no type (terraform_typed_variables)
Warning: interpolation-only expression "${var.name}", write var.name (terraform_deprecated_interpolation)
Warning: variable "size" is declared but not used (terraform_unused_declarations)
Warning: local.unused is declared but not used (terraform_unused_declarations)
6 issue(s)

Every one of those is a thing terraform validate accepts. That is the gap a linter fills. My first version of the typed-variables check was wrong. A regex matched variable "name" {} against the next variable’s body and missed the untyped one. I only caught it by reading the output carefully, which is a decent argument for using a maintained tool.

Running them together

The usual order in CI or a pre-commit hook is fmt -check, init -backend=false, validate, tflint. None needs cloud credentials, and all run before the expensive plan. I have not timed them on a real repo. The pre-commit-terraform project collects these hooks. The next lessons add the slower layers: native tests, policy scanning and cost checks.

About the video

The embed is networknutsdotnet’s “Terraform Linter - TFLINT - Tools must to use with Terraform”, 8 minutes 16 seconds, February 2022. Its description says TFLint is a linter that finds possible errors such as wrong instance types, warns about deprecated syntax and enforces best practices and naming conventions. It is a small channel and an older video, so the plugin set up it shows may differ from current releases, and it does not cover fmt or validate, which this lesson takes from the CLI docs. English was judged from the title, description, channel and its English auto-caption track. Anton Babenko’s long live stream about pre-commit-terraform, in deeper, is 1 hour 8 minutes and I did not clip it.

Where this fits

Linting finds mistakes a reader could find. To prove behaviour, you need tests that run the configuration: terraform test is next.

Check yourself

  1. 01Why does terraform fmt -check -recursive need -recursive in a monorepo?

    By default fmt processes only the given directory, so nested environment directories would never be checked.

  2. 02What can terraform validate not catch?

    Anything that needs a remote service: a taken name, missing permissions, an invalid value the provider API rejects at apply.

  3. 03What are two things TFLint flags that validate accepts?

    A variable with no type, a provider with no version constraint, an unused local or an old-style "${var.x}" interpolation.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark