How to Use TFLint for Terraform Linting
Use
tflint to analyze your Terraform files for errors and style issues by running tflint in your Terraform project directory. It scans your code and reports problems to help you write better, error-free Terraform configurations.Syntax
The basic command to run TFLint is tflint. You run it inside your Terraform project folder where your .tf files are located. You can also use options like --init to set up plugins or --config to specify a custom config file.
tflint: Runs linting on Terraform files in the current directory.tflint --init: Downloads and installs necessary plugins.tflint --config path/to/.tflint.hcl: Uses a custom configuration file.
bash
tflint [options] # Common options: # --init Initialize plugins # --config Specify config file # --help Show help information
Example
This example shows how to run tflint on a simple Terraform project. It checks your Terraform files and prints any issues found.
bash
# Navigate to your Terraform project folder cd my-terraform-project # Initialize TFLint plugins (only needed once) tflint --init # Run TFLint to check your Terraform files tflint
Output
Warning: aws_instance.example: attribute deprecated
Error: aws_s3_bucket.example: missing required argument 'acl'
Common Pitfalls
Common mistakes when using TFLint include:
- Running
tflintoutside the Terraform project folder, so no files are found. - Not running
tflint --initfirst, causing plugin errors. - Ignoring warnings and errors reported by TFLint, which can lead to broken Terraform deployments.
- Using outdated TFLint versions missing latest rules.
Always run tflint --init before linting and fix reported issues promptly.
bash
## Wrong: Running tflint outside project folder cd ~ tflint ## Right: Run inside project and initialize plugins cd my-terraform-project tflint --init tflint
Quick Reference
| Command | Description |
|---|---|
| tflint | Run linting on Terraform files in current directory |
| tflint --init | Download and install required plugins |
| tflint --config | Use a custom configuration file |
| tflint --help | Show help and usage information |
Key Takeaways
Run tflint inside your Terraform project folder to check your code.
Use tflint --init once to install necessary plugins before linting.
Fix all warnings and errors reported by tflint to improve code quality.
Use a custom config file with --config to customize linting rules.
Keep tflint updated to get the latest linting checks and features.