TFLint Documentation

repository·master·Indexed 26 days ago

https://github.com/terraform-linters/tflint

TFLint is a pluggable linter for Terraform designed to find errors, warn about deprecated syntax, and enforce best practices across major cloud providers including AWS, Azure, and GCP. It features a plugin architecture, support for Language Server Protocol (LSP) mode, and a CLI for recursive inspection, autofixing, and custom rule management.

Tokens
12.9K
Snippets
41
Records
86
Agent score
91%

What's inside TFLint

  1. Understand the TFLint plugin architecture

    master

    TFLint is a pluggable linter that does not contain rule implementations. Instead, rules are provided as external plugins launched as subprocesses. Communication between the TFLint host and plugins occurs over gRPC.

    In this architecture, both the host and the plugin act as both gRPC server and client:

    Plugin (Client) to Host (Server) requests:

    • Retrieve Terraform configs (e.g., aws_instance.main)
    • Evaluate expressions (e.g., var.foo)
    • Save reported issues to the host server

    Host (Client) to Plugin (Server) requests:

    • Apply plugin configurations
    • Request to run inspections

    Plugin development is based on the TFLint plugin SDK.

  2. Understand the `ipaddr` package behavior and compatibility

    master

    The ipaddr package contains a fork of Go 1.16's net package IP address parsing functions. It is used to maintain compatibility with Terraform's built-in functions: cidrhost, cidrsubnet, cidrsubnets, and cidrnetmask.

    Key Behavior: Unlike Go 1.17+, this package allows IPv4 address octets with leading zeros (e.g., 192.168.0.01). Standard Go 1.17+ rejects these to prevent ambiguity between decimal and octal interpretations.

    Usage Guidance:

    • Use this package if: You are implementing new functionality that must remain consistent with the behavior of Terraform's cidr* functions.
    • Avoid this package if: You are building new features that do not require this specific legacy compatibility. In such cases, you should prefer the stricter, standard Go parsing functions to avoid potential security concerns related to non-normalized IP forms.
  3. Understand the TFLint Terraform package fork

    master
    The terraform package within this repository is a fork of Terraform's internal packages. It is used to provide static analysis functionality for the Terraform Language. Note that the implementation is not identical to the original Terraform source; it has been simplified and modified to suit the specific needs of the TFLint project due to Terraform's package internalization policies.
  4. Understand TFLint's Terraform language compatibility

    master

    TFLint uses its own parser (a fork of Terraform's native parser) to interpret Terraform code, meaning Terraform does not need to be installed at runtime.

    Key compatibility details:

    • Supports Terraform v1.x syntax and semantics.
    • Follows Terraform Compatibility Promises.
    • The latest supported version is Terraform v1.15.
    • New features require newer TFLint versions; bug and experimental feature compatibility is not guaranteed.
  5. Verify TFLint installation integrity

    master

    It is recommended to verify the authenticity of the downloaded binaries using the GitHub CLI to check Artifact Attestations.

    Note: Cosign signatures are deprecated; use the GitHub CLI method instead.

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

    master

    You can install TFLint using several methods depending on your operating system and environment.

    Linux (Manual)

    curl -sSLO https://github.com/terraform-linters/tflint/releases/latest/download/tflint_linux_amd64.zip
    curl -sSLO https://github.com/terraform-linters/tflint/releases/latest/download/checksums.txt
    gh attestation verify checksums.txt -R terraform-linters/tflint
    sha256sum --ignore-missing -c checksums.txt
    unzip tflint_linux_amd64.zip
    sudo install -c -v tflint /usr/local/bin/
  7. Suppress specific TFLint rules on specific lines

    master

    Use the # tflint-ignore: <rule_name> annotation on the line preceding the resource or attribute to suppress specific issues. Annotations only work for issues emitted from valid, parseable modules; they cannot suppress errors.

    Rules can be specified individually, as a comma-separated list, or by using the all keyword to ignore all rules on that line. You can also include a comment after the annotation to provide a reason for the suppression. While # is recommended for Terraform, // is also supported.

    resource "aws_instance" "foo" {
        # Single rule
        # tflint-ignore: aws_instance_invalid_type
        instance_type = "t1.2xlarge"
    
        # Multiple rules
        # tflint-ignore: aws_instance_invalid_type, other_rule
        instance_type = "t1.2xlarge"
    
        # All rules
        # tflint-ignore: all
        instance_type = "t1.2xlarge"
    
        # With a reason (multi-line)
        # This instance type is new and TFLint doesn't know about it yet
        # tflint-ignore: aws_instance_invalid_type
        instance_type = "t10.2xlarge"
    
        # With a reason (same-line)
        # tflint-ignore: aws_instance_invalid_type # too new for TFLint
        instance_type = "t10.2xlarge"
    }
  8. Install TFLint plugins via configuration

    master

    You can extend TFLint by declaring plugins in your configuration file. Once declared with a source and version, you can run tflint --init to automatically download and install them.

    To install a plugin, add a plugin block to your config file and then execute the initialization command.

    plugin "foo" {
      enabled = true
      version = "0.1.0"
      source  = "github.com/org/tflint-ruleset-foo"
    }
    $ tflint --init
  9. Use the --fix flag to automatically resolve TFLint issues

    master

    TFLint can automatically resolve certain reported issues. Issues that support this feature are explicitly marked with a [Fixable] label in the output. To apply these fixes, run TFLint with the --fix flag.

    Important Note: When an autofix is applied, TFLint will automatically format the entire file. This means that code ranges unrelated to the specific issue being fixed may also be modified by the formatter.

    $ tflint --fix
  10. Use TFLint annotations in Terraform JSON

    master

    In Terraform JSON configuration, you can suppress rules for an entire file using a top-level // comment property.

    Note that tflint-ignore-file is supported in JSON, but the line-specific tflint-ignore annotation is not supported in JSON. When adding comments to the annotation in JSON, the annotation must be the first thing in the comment property string.

    {
      "//": "tflint-ignore-file: aws_instance_invalid_type",
      "resource": {
        "aws_instance": {
          "foo": {
            "instance_type": "t2.micro"
          }
        }
      }
    }