Comprehensive Guide to TFLint
Introduction to TFLint
TFLint is an open-source Terraform linter (static analysis tool) that checks Terraform configurations for errors, potential bugs, and stylistic or best-practice violations. It helps developers identify issues that Terraform’s built-in validation (terraform validate) might not catch, especially provider-specific problems like invalid AWS instance types or missing required Terraform versions.
Key Benefits of TFLint:
- Early Error Detection – Catches issues before deployment, preventing costly failures.
- Best Practice Enforcement – Ensures configurations align with Terraform and provider recommendations.
- Security & Compliance Checks – Detects security risks like open security groups.
- CI/CD Integration – Works in development workflows to maintain high-quality infrastructure as code (IaC).
Installation
TFLint is available for macOS, Linux, and Windows. You can install it using package managers or download the binary.
macOS (Homebrew):
brew install tflint
Linux:
curl -s https://raw.githubusercontent.com/terraform-linters/tflint/master/install_linux.sh | bash
Code language: JavaScript (javascript)
Alternatively, install it via Snap:
sudo snap install tflint
Windows (Chocolatey):
choco install tflint
Docker (Alternative):
docker run --rm -v $(pwd):/data -t ghcr.io/terraform-linters/tflint
Code language: JavaScript (javascript)
Verify Installation:
tflint --version
Basic Usage
TFLint is straightforward to use. Navigate to your Terraform project directory and run:
tflint
If using provider-specific rules, initialize plugins first:
tflint --init
Example Output:
1 issue(s) found:
Warning: terraform "required_version" attribute is required.
Code language: JavaScript (javascript)
Here is the normal table representation of the tflint --help options:
| Option | Description |
|---|---|
-v, --version | Print TFLint version |
--init | Install plugins |
--langserver | Start language server |
| `-f, –format=[default | json |
-c, --config=FILE | Config file name (default: .tflint.hcl) |
--ignore-module=SOURCE | Ignore module sources |
--enable-rule=RULE_NAME | Enable rules from the command line |
--disable-rule=RULE_NAME | Disable rules from the command line |
--only=RULE_NAME | Enable only this rule, disabling all others (can be specified multiple times) |
--enable-plugin=PLUGIN_NAME | Enable plugins from the command line |
--var-file=FILE | Terraform variable file name |
--var='foo=bar' | Set a Terraform variable |
| `–call-module-type=[all | local |
--chdir=DIR | Switch to a different working directory before executing the command |
--recursive | Run command in each directory recursively |
--filter=FILE | Filter issues by file names or globs |
--force | Return zero exit status even if issues are found |
| `–minimum-failure-severity=[error | warning |
--color | Enable colorized output |
--no-color | Disable colorized output |
--fix | Fix issues automatically |
--no-parallel-runners | Disable per-runner parallelism |
--max-workers=N | Set maximum number of workers in recursive inspection (default: number of CPUs) |
-h, --help | Show this help message |
This table provides a clean and structured reference for TFLint command-line options. 🚀 Let me know if you need any modifications!
Configuration
TFLint can be customized using a .tflint.hcl configuration file.
Example .tflint.hcl Configuration:
tflint {
required_version = ">= 0.50"
}
config {
format = "compact"
plugin_dir = "~/.tflint.d/plugins"
call_module_type = "local"
}
plugin "aws" {
enabled = true
version = "0.4.0"
source = "github.com/terraform-linters/tflint-ruleset-aws"
}
rule "terraform_required_version" {
enabled = false
}
Code language: JavaScript (javascript)
Rules and Plugins
TFLint has built-in Terraform language rules and provider-specific plugins.
Built-in Rules:
- Required Terraform Version – Ensures
required_versionis defined. - Type Constraints – Checks explicit type constraints on variables.
- Deprecated Syntax – Flags usage of removed Terraform features.
- Unused Declarations – Identifies unused variables and outputs.
Provider Plugins:
- AWS Plugin – Checks instance types, security group configurations, and more.
- Azure Plugin – Validates Azure-specific configurations.
- GCP Plugin – Ensures Google Cloud best practices.
Enabling a Plugin:
plugin "aws" {
enabled = true
version = "0.24.1"
source = "github.com/terraform-linters/tflint-ruleset-aws"
}
Code language: JavaScript (javascript)
Run:
tflint --init
CI/CD Integration
TFLint should be incorporated into CI/CD pipelines to enforce Terraform quality standards.
GitHub Actions Example:
jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: tflint --init
- run: tflint
GitLab CI Example:
lint:terraform:
image: terraform-linters/tflint:latest
script:
- tflint --init
- tflint
Best Practices
- Run TFLint Early – Use it during development, not just in CI/CD.
- Customize Rules – Enable or disable rules in
.tflint.hclbased on project needs. - Use Provider Plugins – AWS, Azure, and GCP plugins catch cloud-specific issues.
- Inline Rule Ignoring – Use
# tflint-ignore: rule_namefor exceptions. - Enforce in CI/CD – Make TFLint a required step before merging code.
- Keep TFLint Updated – Regularly update TFLint and its plugins.
- Combine with Other Tools – Use it alongside
terraform fmt,terraform validate, and security scanners liketfsec.
Conclusion
TFLint is an essential tool for ensuring Terraform code quality, catching errors early, enforcing best practices, and integrating seamlessly into CI/CD workflows. Implement it in your Terraform projects to maintain consistent and error-free infrastructure as code.
Here’s a comprehensive tutorial for using TFLint, a powerful linter for Terraform code:
Installation
- Install TFLint using one of the following methods:
- macOS:
brew install tflint - Windows:
choco install tflint - Linux:
curl -s https://raw.githubusercontent.com/terraform-linters/tflint/master/install_linux.sh | bash13
- Verify the installation by running: text
tflint --version
Setup and Configuration
- Navigate to your Terraform project directory.
- Create a
.tflint.hclconfiguration file in your project root: textplugin "terraform" { enabled = true preset = "recommended" } - Initialize TFLint in your project: text
tflint --initThis command will download and install necessary plugins based on your configuration1.
Using TFLint
- Run TFLint in your project directory: text
tflintThis will scan your Terraform files and report any issues it finds1. - For a more detailed report, use the JSON format: text
tflint --format=json
Adding Cloud Provider Plugins
To lint configurations for specific cloud providers, add the relevant plugins to your .tflint.hcl file:
textplugin "aws" {
enabled = true
version = "0.24.0"
source = "github.com/terraform-linters/tflint-ruleset-aws"
}
plugin "azurerm" {
enabled = true
version = "0.24.0"
source = "github.com/terraform-linters/tflint-ruleset-azurerm"
}
After adding plugins, run tflint --init again to download and install them2.
Advanced Usage
- To run TFLint recursively in subdirectories: text
tflint --recursive - To enable specific rules from the command line: text
tflint --enable-rule=aws_resource_missing_tags - To disable specific rules: text
tflint --disable-rule=terraform_deprecated_syntax - To use a custom configuration file: text
tflint --config=custom_tflint.hcl
Integrating with CI/CD
For GitHub Actions, you can use the setup-tflint action:
text- uses: terraform-linters/setup-tflint@v3
name: Setup TFLint
- run: tflint
Docker Usage
If you prefer using Docker, you can run TFLint without installation:
textdocker run --rm -v $(pwd):/data -t ghcr.io/terraform-linters/tflint
To download plugins and run TFLint in a single command:
textdocker run --rm -v $(pwd):/data -t --entrypoint /bin/sh ghcr.io/terraform-linters/tflint -c "tflint --init && tflint"
By following this tutorial, you’ll be able to effectively use TFLint to improve the quality and reliability of your Terraform code.
I’m a DevOps/SRE/DevSecOps/Cloud Expert passionate about sharing knowledge and experiences. I have worked at Cotocus. I share tech blog at DevOps School, travel stories at Holiday Landmark, stock market tips at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at TrueReviewNow , and SEO strategies at Wizbrand.
Do you want to learn Quantum Computing?
Please find my social handles as below;
Rajesh Kumar Personal Website
Rajesh Kumar at YOUTUBE
Rajesh Kumar at INSTAGRAM
Rajesh Kumar at X
Rajesh Kumar at FACEBOOK
Rajesh Kumar at LINKEDIN
Rajesh Kumar at WIZBRAND