Linting with tflint
validate checks that the code is valid HCL with valid references. It doesn't know that t3.mega is not an EC2 instance type, that a variable is declared but never used, or that a module forgot required_version. tflint is the linter that fills that gap: a pluggable rule engine with a bundled terraform ruleset (language best practices) and provider rulesets (aws, azurerm, google) that know the provider's valid values. The sandbox has tflint 0.65.0 with the AWS ruleset 0.50.0 pre-installed.
Configuration
.tflint.hcl in the folder you lint:
config {
call_module_type = "local" # also inspect local modules ("all" includes remote ones, "none" none)
}
plugin "terraform" {
enabled = true
preset = "recommended" # the bundled ruleset; "all" enables every rule
}
plugin "aws" {
enabled = true
version = "0.50.0"
source = "github.com/terraform-linters/tflint-ruleset-aws"
}
rule "terraform_naming_convention" {
enabled = true # snake_case names everywhere
}Normally tflint --init downloads the plugins listed there (cached in ~/.tflint.d); in the sandbox the AWS plugin is already in place.
A first run
mkdir app && cd app
cat > .tflint.hcl <<'HCL'
plugin "aws" {
enabled = true
version = "0.50.0"
source = "github.com/terraform-linters/tflint-ruleset-aws"
}
HCL
cat > main.tf <<'HCL'
terraform {
required_providers {
aws = { source = "hashicorp/aws", version = "~> 6.0" }
}
}
variable "unused" {}
resource "aws_instance" "web" {
ami = "ami-0123456789abcdef0"
instance_type = "t3.mega"
}
resource "aws_db_instance" "db" {
identifier = "linkly"
instance_class = "db.t3.mirco"
engine = "postgres"
allocated_storage = 20
username = "linkly_admin"
manage_master_user_password = true
}
HCL
tflint --version
lint() { tflint --format compact "$@" > out.txt; local rc=$?; grep -v "^main" out.txt; grep "^main" out.txt | sort -t: -k2,2n -k3,3n; return $rc; }
lint; echo "exit=$?"TFLint version 0.65.0
+ ruleset.aws (0.50.0)
+ ruleset.terraform (0.15.0-bundled)
5 issue(s) found:
main.tf:1:1: Warning - terraform "required_version" attribute is required (terraform_required_version)
main.tf:7:1: Warning - `unused` variable has no type (terraform_typed_variables)
main.tf:7:1: Warning - variable "unused" is declared but not used (terraform_unused_declarations)
main.tf:11:19: Error - "t3.mega" is an invalid value as instance_type (aws_instance_invalid_type)
main.tf:16:33: Error - "db.t3.mirco" is invalid instance type. (aws_db_instance_invalid_type)
exit=2
Two typos that validate and even plan accept (t3.mega, db.t3.mirco are just strings to them) would only fail at apply, half-way through creating resources. tflint catches them in a second, offline. The terraform ruleset adds hygiene: a variable without a type, an unused declaration, a missing required_version. Exit codes: 0 clean, 2 issues found, 1 tflint itself failed.
Fixing, ignoring and failing on severity
The helper lint in these sessions sorts the findings by line, because tflint doesn't guarantee their order.
mkdir app && cd app
lint() { tflint --format compact "$@" > out.txt; local rc=$?; grep -v "^main" out.txt; grep "^main" out.txt | sort -t: -k2,2n -k3,3n; return $rc; }
cat > .tflint.hcl <<'HCL'
plugin "aws" {
enabled = true
version = "0.50.0"
source = "github.com/terraform-linters/tflint-ruleset-aws"
}
rule "terraform_naming_convention" {
enabled = true
}
HCL
cat > main.tf <<'HCL'
terraform {
required_version = ">= 1.6"
required_providers {
aws = { source = "hashicorp/aws", version = "~> 6.0" }
}
}
variable "region" {
type = string
default = "eu-west-1"
}
resource "aws_instance" "legacyWeb" {
ami = "ami-0123456789abcdef0"
instance_type = "m3.medium"
}
HCL
echo "--- all issues"
lint; echo "exit=$?"
echo "--- accept the old instance type, with a reason"
sed -i 's|^ instance_type = "m3.medium"| # tflint-ignore: aws_instance_previous_type # migrating in Q3, see OPS-142\n&|' main.tf
lint; echo "exit=$?"
echo "--- only errors fail the build"
lint --minimum-failure-severity=error; echo "exit=$?"
echo "--- --fix removes the unused variable"
cp main.tf before.tf.bak
tflint --fix >/dev/null
diff before.tf.bak main.tf--- all issues
3 issue(s) found:
main.tf:8:1: Warning - variable "region" is declared but not used (terraform_unused_declarations)
main.tf:13:1: Notice - resource name `legacyWeb` must match the following format: snake_case (terraform_naming_convention)
main.tf:15:19: Warning - "m3.medium" is previous generation instance type. (aws_instance_previous_type)
exit=2
--- accept the old instance type, with a reason
2 issue(s) found:
main.tf:8:1: Warning - variable "region" is declared but not used (terraform_unused_declarations)
main.tf:13:1: Notice - resource name `legacyWeb` must match the following format: snake_case (terraform_naming_convention)
exit=2
--- only errors fail the build
2 issue(s) found:
main.tf:8:1: Warning - variable "region" is declared but not used (terraform_unused_declarations)
main.tf:13:1: Notice - resource name `legacyWeb` must match the following format: snake_case (terraform_naming_convention)
exit=0
--- --fix removes the unused variable
8,12d7
< variable "region" {
< type = string
< default = "eu-west-1"
< }
<
14c9
< ami = "ami-0123456789abcdef0"
---
> ami = "ami-0123456789abcdef0"
# tflint-ignore: rule_nameon the line above silences one finding, with the reason next to it. Use it rarely; it is greppable debt.--minimum-failure-severity=errorlets warnings be printed without failing CI, useful while adopting tflint on an old codebase.--fixapplies the fixes for rules marked[Fixable](unused declarations, deprecated syntax) and rewrites the touched file in canonical format, which is why theamiline lost its alignment (the comment line now separates it frominstance_type). Review the diff, and runfmtafter it.--recursivelints every module folder in a repository;--formatalso offersjson,checkstyle,junitandsarif(GitHub code scanning reads SARIF).
In the pipeline
- uses: terraform-linters/setup-tflint@v6
with:
tflint_version: v0.65.0
- run: tflint --init
env:
GITHUB_TOKEN: ${{ github.token }} # avoids GitHub API rate limits when downloading plugins
- run: tflint --recursive --format compactOrder the cheap checks first: fmt -check (milliseconds), validate, tflint, test, then the plan that needs credentials (Plan on Pull Request). tflint finds mistakes; security misconfigurations (an unencrypted bucket, an open security group) are the domain of policy scanners such as Trivy (which absorbed tfsec) and Checkov, covered in the Toolkit.
Test yourself
instance_type = "t3.mega" passes tofu validate. Why does tflint flag it?
- It checks the HCL syntax more strictly
- The AWS ruleset knows the list of valid instance types
- It calls the AWS API
- It runs a plan
How do you silence one specific finding with a documented reason?
- Delete .tflint.hcl
# tflint-ignore: rule_nameabove the line, with a commenttflint --quiet- Rename the resource
What exit code does tflint return when it finds issues?
- 0
- 1
- 2
- 3