TFLint

TFLint is a pluggable linter for Terraform code. It can detect possible errors, enforce best practices, and provide style checking.

Installation

Using Homebrew

brew install tflint

Using Curl

curl -s https://raw.githubusercontent.com/terraform-linters/tflint/master/install_linux.sh | bash

Docker

docker pull ghcr.io/terraform-linters/tflint

Configuration

Create .tflint.hcl in your project root:

plugin "aws" {
  enabled = true
  version = "0.23.1"
  source  = "github.com/terraform-linters/tflint-ruleset-aws"
}

rule "aws_instance_invalid_type" {
  enabled = true
}

rule "terraform_deprecated_index" {
  enabled = true
}

# Disallow // comments in favor of #
rule "terraform_comment_syntax" {
  enabled = true
}

# Enforce consistent naming
rule "terraform_naming_convention" {
  enabled = true
  format  = "snake_case"
}

Available Rules

Built-in Rules

Provider-specific Rules

Integration with CI/CD

GitHub Actions

Azure DevOps Pipeline

Pre-commit Hook Integration

Add to .pre-commit-config.yaml:

Best Practices

1. Rule Configuration

2. Plugin Management

3. Ignore Rules When Needed

Common Issues and Solutions

  1. Version Mismatches

    • Keep TFLint and plugins updated

    • Pin versions in CI/CD

    • Use version constraints

  2. Performance

    • Use .tflint.hcl to enable only needed rules

    • Implement caching in CI/CD

    • Use parallel execution for large codebases

  3. False Positives

    • Use ignore comments judiciously

    • Configure rules appropriately

    • Report issues to maintainers

Checklist

Last updated