Convert Figma logo to code with AI

terraform-linters logotflint

A Pluggable Terraform Linter

4,841
354
4,841
29

Top Related Projects

6,861

Prevent cloud misconfigurations and find vulnerabilities during build-time in infrastructure as code, container images and open source packages with Checkov by Bridgecrew.

6,657

Tfsec is now part of Trivy

1,344

a lightweight, security focused, BDD test framework against terraform.

pre-commit git hooks to take care of Terraform configurations πŸ‡ΊπŸ‡¦

Detect compliance and security violations across Infrastructure as Code to mitigate risk before provisioning cloud native infrastructure.

Quick Overview

TFLint is a pluggable linter for Terraform code. It analyzes your Terraform configurations to detect potential errors, enforce best practices, and maintain consistency across your infrastructure-as-code projects. TFLint can be integrated into your development workflow to catch issues early and improve the quality of your Terraform code.

Pros

  • Customizable and extensible through plugins
  • Supports both built-in and custom rules
  • Can be integrated into CI/CD pipelines for automated checks
  • Helps catch errors before applying Terraform configurations

Cons

  • May require additional setup and configuration for advanced use cases
  • Some rules might produce false positives in certain scenarios
  • Learning curve for creating custom rules and plugins
  • Limited support for some provider-specific resources

Getting Started

To get started with TFLint, follow these steps:

  1. Install TFLint:

    brew install tflint
    
  2. Create a configuration file .tflint.hcl in your project root:

    plugin "aws" {
      enabled = true
      version = "0.21.1"
      source  = "github.com/terraform-linters/tflint-ruleset-aws"
    }
    
  3. Run TFLint in your Terraform project directory:

    tflint
    

This will run TFLint with the default rules and any plugins specified in your configuration file.

Competitor Comparisons

6,861

Prevent cloud misconfigurations and find vulnerabilities during build-time in infrastructure as code, container images and open source packages with Checkov by Bridgecrew.

Pros of Checkov

  • Supports multiple IaC languages (Terraform, CloudFormation, Kubernetes, etc.)
  • Offers a wider range of security and compliance checks
  • Integrates with CI/CD pipelines and provides a web interface

Cons of Checkov

  • May have a steeper learning curve due to its broader scope
  • Can be slower to run compared to TFLint, especially on large codebases
  • Requires more setup and configuration for advanced features

Code Comparison

Checkov:

checkov -d . --framework terraform

TFLint:

tflint

Key Differences

  1. Scope: Checkov is a multi-cloud, multi-framework tool, while TFLint focuses specifically on Terraform.
  2. Performance: TFLint is generally faster for Terraform-specific linting.
  3. Integration: Checkov offers more extensive integration options with various platforms and tools.
  4. Customization: TFLint provides more granular control over Terraform-specific rules and checks.
  5. Community: TFLint has a more focused community for Terraform-specific issues and improvements.

Both tools have their strengths, and the choice between them depends on your specific needs. TFLint is ideal for Terraform-centric projects, while Checkov is better suited for multi-cloud, multi-framework environments with broader security and compliance requirements.

6,657

Tfsec is now part of Trivy

Pros of tfsec

  • Focuses on security-specific checks for Terraform code
  • Provides detailed explanations and remediation advice for identified issues
  • Integrates well with CI/CD pipelines and offers various output formats

Cons of tfsec

  • Limited to security-related checks, not covering general Terraform best practices
  • May produce more false positives compared to TFLint
  • Requires additional configuration for custom rules or ignoring specific checks

Code Comparison

tfsec example:

resource "aws_s3_bucket" "example" {
  bucket = "my-bucket"
  acl    = "public-read"
}

TFLint example:

resource "aws_instance" "example" {
  ami           = "ami-12345678"
  instance_type = "t2.micro"
}

In the tfsec example, it would flag the public ACL as a security risk. TFLint, on the other hand, might check for valid instance types or AMI IDs in the second example.

Both tools serve different purposes: tfsec focuses on security checks, while TFLint covers a broader range of Terraform linting and best practices. Users often employ both tools in their workflows to ensure comprehensive code quality and security.

1,344

a lightweight, security focused, BDD test framework against terraform.

Pros of terraform-compliance

  • Focuses on behavior-driven development (BDD) and uses Gherkin syntax for writing tests
  • Allows for more human-readable and business-oriented compliance checks
  • Supports custom steps for extended functionality

Cons of terraform-compliance

  • Steeper learning curve due to BDD approach and Gherkin syntax
  • Limited built-in rules compared to TFLint
  • May require more setup and configuration for complex scenarios

Code Comparison

terraform-compliance example:

Scenario: Ensure all EC2 instances have tags
  Given I have AWS EC2 instance defined
  Then it must contain tags
  And its value must not be null

TFLint example:

rule "aws_instance_invalid_type" {
  enabled = true
}

rule "aws_instance_previous_type" {
  enabled = true
}

Both tools serve different purposes in the Terraform ecosystem. terraform-compliance focuses on compliance and security checks using a BDD approach, while TFLint is primarily a linter for detecting possible errors and enforcing best practices in Terraform code. The choice between the two depends on specific project requirements, team preferences, and existing workflows.

pre-commit git hooks to take care of Terraform configurations πŸ‡ΊπŸ‡¦

Pros of pre-commit-terraform

  • Integrates with the pre-commit framework, allowing for easy setup and integration with other tools
  • Provides a comprehensive set of hooks for Terraform linting, formatting, and validation
  • Supports multiple Terraform-related tools in one package (e.g., tflint, terraform-docs, terrascan)

Cons of pre-commit-terraform

  • Requires additional setup and configuration of the pre-commit framework
  • May have a steeper learning curve for users unfamiliar with pre-commit
  • Depends on external tools, which may need separate installation and maintenance

Code comparison

pre-commit-terraform configuration:

repos:
  - repo: https://github.com/antonbabenko/pre-commit-terraform
    rev: v1.64.0
    hooks:
      - id: terraform_fmt
      - id: terraform_validate

tflint configuration:

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

While pre-commit-terraform provides a framework for running multiple tools, including tflint, tflint itself focuses on deep, customizable linting of Terraform code. pre-commit-terraform is better suited for teams looking for an all-in-one solution, while tflint offers more granular control over linting rules and is ideal for projects requiring specific, detailed linting configurations.

Detect compliance and security violations across Infrastructure as Code to mitigate risk before provisioning cloud native infrastructure.

Pros of Terrascan

  • Supports multiple Infrastructure as Code (IaC) tools beyond Terraform, including Kubernetes, Helm, and Dockerfiles
  • Offers out-of-the-box security and compliance checks based on industry standards
  • Provides a web UI for easier visualization and management of scan results

Cons of Terrascan

  • May have a steeper learning curve due to its broader scope
  • Can be slower in execution compared to TFLint, especially for large projects
  • Less focused on Terraform-specific linting and style checks

Code Comparison

Terrascan:

resource "aws_s3_bucket" "example" {
  bucket = "my-bucket"
  acl    = "private"
}

TFLint:

resource "aws_s3_bucket" "example" {
  bucket = "my-bucket"
  acl    = "private"  # TFLint: aws_s3_bucket_acl is deprecated
}

In this example, TFLint would specifically flag the deprecated acl attribute, while Terrascan might focus more on security aspects like encryption or public access settings.

Both tools serve valuable purposes in the IaC ecosystem. TFLint excels at Terraform-specific linting and style checks, while Terrascan offers broader security and compliance scanning across multiple IaC platforms. The choice between them depends on your specific needs and the scope of your infrastructure management.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

TFLint

Build Status GitHub release Terraform Compatibility License: MPL 2.0 + BUSL 1.1 Go Report Card Homebrew

A Pluggable Terraform Linter

Features

TFLint is a framework and each feature is provided by plugins, the key features are as follows:

  • Find possible errors (like invalid instance types) for Major Cloud providers (AWS/Azure/GCP).
  • Warn about deprecated syntax, unused declarations.
  • Enforce best practices, naming conventions.

Installation

Bash script (Linux):

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

Homebrew (macOS):

brew install tflint

Chocolatey (Windows):

choco install tflint

NOTE: The Chocolatey package is NOT directly maintained by the TFLint maintainers. The latest version is always available by manual installation.

Verification

GitHub CLI (Recommended)

Artifact Attestations are available that can be verified using the GitHub CLI.

gh attestation verify checksums.txt -R terraform-linters/tflint
sha256sum --ignore-missing -c checksums.txt

Cosign

Cosign verify-blob command ensures that the release was built with GitHub Actions in this repository.

cosign verify-blob --certificate=checksums.txt.pem --signature=checksums.txt.keyless.sig --certificate-identity-regexp="^https://github.com/terraform-linters/tflint" --certificate-oidc-issuer=https://token.actions.githubusercontent.com checksums.txt
sha256sum --ignore-missing -c checksums.txt

Docker

Instead of installing directly, you can use the Docker image:

docker run --rm -v $(pwd):/data -t ghcr.io/terraform-linters/tflint

GitHub Actions

If you want to run on GitHub Actions, setup-tflint action is available.

Getting Started

First, enable rules for Terraform Language (e.g. warn about deprecated syntax, unused declarations). TFLint Ruleset for Terraform Language is bundled with TFLint, so you can use it without installing it separately.

The bundled plugin enables the "recommended" preset by default, but you can disable the plugin or use a different preset. Declare the plugin block in .tflint.hcl like this:

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

See the tflint-ruleset-terraform documentation for more information.

Next, If you are using an AWS/Azure/GCP provider, it is a good idea to install the plugin and try it according to each usage:

If you want to extend TFLint with other plugins, you can declare the plugins in the config file and easily install them with tflint --init.

plugin "foo" {
  enabled = true
  version = "0.1.0"
  source  = "github.com/org/tflint-ruleset-foo"

  signing_key = <<-KEY
  -----BEGIN PGP PUBLIC KEY BLOCK-----

  mQINBFzpPOMBEADOat4P4z0jvXaYdhfy+UcGivb2XYgGSPQycTgeW1YuGLYdfrwz
  9okJj9pMMWgt/HpW8WrJOLv7fGecFT3eIVGDOzyT8j2GIRJdXjv8ZbZIn1Q+1V72
  AkqlyThflWOZf8GFrOw+UAR1OASzR00EDxC9BqWtW5YZYfwFUQnmhxU+9Cd92e6i
  ...
  KEY
}

See also Configuring Plugins.

If you want to add custom rules that are not in existing plugins, you can build your own plugin or write your own policy in Rego. See Writing Plugins or OPA Ruleset.

Usage

TFLint inspects files under the current directory by default. You can change the behavior with the following options/arguments:

$ tflint --help
Usage:
  tflint --chdir=DIR/--recursive [OPTIONS]

Application Options:
  -v, --version                                                 Print TFLint version
      --init                                                    Install plugins
      --langserver                                              Start language server
  -f, --format=[default|json|checkstyle|junit|compact|sarif]    Output format
  -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 other defaults. 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|none]                       Types of module to call (default: 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 found
      --minimum-failure-severity=[error|warning|notice]         Sets minimum severity level for exiting with a non-zero error code
      --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)

Help Options:
  -h, --help                                                    Show this help message

See User Guide for details.

Debugging

If you don't get the expected behavior, you can see the detailed logs when running with TFLINT_LOG environment variable.

$ TFLINT_LOG=debug tflint

Developing

See Developer Guide.

Security

If you find a security vulnerability, please refer our security policy.

License

Please note that although much of this project is licensed under MPL 2.0, some files in the terraform package are licensed under BUSL 1.1.

For the reasons stated above, the executable forms (release binaries) is bound by both licenses.

See also https://discuss.hashicorp.com/t/hashicorp-projects-changing-license-to-business-source-license-v1-1/57106/7

Stargazers over time

Stargazers over time