Skip to content

tflint

tflint documentation

tflint - GitHub

Configuration in Mega-Linter

Variable Description Default value
TERRAFORM_TFLINT_ARGUMENTS User custom arguments to add in linter CLI call
Ex: -s --foo "bar"
TERRAFORM_TFLINT_FILTER_REGEX_INCLUDE Custom regex including filter
Ex: (src\|lib)
Include every file
TERRAFORM_TFLINT_FILTER_REGEX_EXCLUDE Custom regex excluding filter
Ex: (test\|examples)
Exclude no file
TERRAFORM_TFLINT_CLI_LINT_MODE Override default CLI lint mode
- file: Calls the linter for each file
- list_of_files: Call the linter with the list of files as argument
- project: Call the linter from the root of the project
{linter.cli_lint_mode}
TERRAFORM_TFLINT_FILE_EXTENSIONS Allowed file extensions. "*" matches any extension, "" matches empty extension. Empty list excludes all files
Ex: [".py", ""]
[".tf"]
TERRAFORM_TFLINT_FILE_NAMES_REGEX File name regex filters. Regular expression list for filtering files by their base names using regex full match. Empty list includes all files
Ex: ["Dockerfile(-.+)?", "Jenkinsfile"]
Include every file
TERRAFORM_TFLINT_PRE_COMMANDS List of bash commands to run before the linter [{"command": "tflint --init", "cwd": "workspace"}]
TERRAFORM_TFLINT_POST_COMMANDS List of bash commands to run after the linter None
TERRAFORM_TFLINT_CONFIG_FILE tflint configuration file name
Use LINTER_DEFAULT to let the linter find it
.tflint.hcl
TERRAFORM_TFLINT_RULES_PATH Path where to find linter configuration file Workspace folder, then Mega-Linter default rules
TERRAFORM_TFLINT_DISABLE_ERRORS Run linter but consider errors as warnings false
TERRAFORM_TFLINT_DISABLE_ERRORS_IF_LESS_THAN Maximum number of errors allowed 0

Mega-Linter Flavours

This linter is available in the following flavours

Flavor Description Embedded linters Info
all Default Mega-Linter Flavor 93 Docker Image Size (tag) Docker Pulls
terraform Optimized for TERRAFORM based projects 44 Docker Image Size (tag) Docker Pulls

Behind the scenes

How are identified applicable files

  • File extensions: .tf

How the linting is performed

  • tflint is called one time by identified file

Example calls

tflint myfile.tf
tflint -c .tflint.hcl myfile.tf

Help content

Usage:
  tflint [OPTIONS] [FILE or DIR...]

Application Options:
  -v, --version                                           Print TFLint version
      --init                                              Install plugins
      --langserver                                        Start language server
  -f, --format=[default|json|checkstyle|junit|compact]    Output format
                                                          (default: default)
  -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
      --module                                            Inspect modules
      --force                                             Return zero exit
                                                          status even if issues
                                                          found
      --no-color                                          Disable colorized
                                                          output
      --loglevel=[trace|debug|info|warn|error]            Change the loglevel

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

Installation on mega-linter Docker image

  • Dockerfile commands :
FROM ghcr.io/terraform-linters/tflint:latest as tflint
COPY --from=tflint /usr/local/bin/tflint /usr/bin/

Example success log

Results of tflint linter (version 0.21.0)
See documentation on https://nvuillam.github.io/mega-linter/descriptors/terraform_tflint/
-----------------------------------------------

[SUCCESS] .automation/test/terraform/good/terraform_good_1.tf


Example error log

Results of tflint linter (version 0.21.0)
See documentation on https://nvuillam.github.io/mega-linter/descriptors/terraform_tflint/
-----------------------------------------------

[ERROR] .automation/test/terraform/bad/terraform_bad_1.tf
    Failed to load configurations. 1 error(s) occurred:

    Error: Invalid expression

      on .automation/test/terraform/bad/terraform_bad_1.tf line 3, in resource "aws_instance" "bad":
       3:   instance_type =            # invalid type!
       4: }

    Expected the start of an expression, but found an invalid expression token.