tflint how to disable rules?

447 Views Asked by At

I'm facing a strange problem. I have countless examples in my repository which do not contain any provider file or config. To avoid the errors regarding this, I would love to disable the related tflint rules terraform_required_version and terraform_required_providers. I tried the config below running tflint --config=.tflint.hcl --recursive, but tflint still reports these types of errors:

32 issue(s) found:

Warning: terraform "required_version" attribute is required (terraform_required_version)

  on  line 0:
   (source code not available)

Reference: https://github.com/terraform-linters/tflint-ruleset-terraform/blob/v0.5.0/docs/rules/terraform_required_version.md


....


Warning: Missing version constraint for provider "aws" in `required_providers` (terraform_required_providers)

  on modules/rds/password_secrets_manager/main.tf line 26:
  26: resource "aws_secretsmanager_secret_version" "secret_version" {

Reference: https://github.com/terraform-linters/tflint-ruleset-terraform/blob/v0.5.0/docs/rules/terraform_required_providers.md

What do I do wrong?

I have the following .tflint.hcl config:

config {
  force               = true
  module              = true
  disabled_by_default = false
}

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

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

rule "terraform_required_version" {
  enabled = false
}

rule "terraform_required_providers" {
  enabled = false
}
2

There are 2 best solutions below

0
On

I had the same issue and found out that if you use the --recursive flag, it expects the .tflint.hcl file to be present in every directory in the file tree. I will write a Python script to enter every folder, copy the file in the directory, use linting, and delete it again. If there is a better I am all ears.

Something like

import os
import subprocess
import shutil

def lint(path: str):
    STATIC_TFLINT_CONFIG_PATH = "path to your .tflint.hcl"
    
    # Navigate to the directory
    os.chdir(path)

    # get lint config file
    shutil.copyfile(STATIC_TFLINT_CONFIG_PATH, path)

    subprocess.run("tflint", "--version", check=True)
    subprocess.run("tflint", "--init", check=True)
    subprocess.run("tflint", "--chdir=.", check=True)

path = "your path"
os.chdir(path)
for dir in os.listdir(path):
  target_path = os.path.join(path, dir)
    if os.path.isdir(target_path):
      lint(target_path)
0
On

Had the same issue and discovered an easier way than the python script listed in the other answer. You need to supply an absolute path to .tflint.hcl for the --config argument. This solved my issue: tflint --minimum-failure-severity=notice --recursive --config $(pwd)/.tflint.hcl.

This helped me identify the problem: https://github.com/terraform-linters/tflint/discussions/1872#discussion-5695423