如何抑制/忽略 tflint 警告

Sag*_*hav 6 lint terraform github-actions

我第一次使用 tflint 扫描我的 terraform 代码。为此,我创建了 shell 脚本来执行 tflint 命令,但是,在执行 tflint 作业时,我收到一些 [WARN] 消息。我不确定它们是如何生成的。有办法抑制吗?

tflint 命令已成功执行,并且还在我的 terraform 代码中显示可能的问题/通知。

我正在使用下面的 Github 工作流程操作;

      - name: Setup TFLint
        uses: terraform-linters/setup-tflint@v1
        with:
          tflint_version: v0.26.0

      - name: Lint Terraform Code
        run: scripts/tflint.sh
        shell: bash
        continue-on-error: false
Run Code Online (Sandbox Code Playgroud)

“.tflint.hcl”文件 ->

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

rule "terraform_naming_convention" {
  enabled = true
}

rule "terraform_unused_declarations" {
  enabled = true
}

rule "terraform_deprecated_index" {
  enabled = true
}

rule "terraform_documented_outputs" {
  enabled = true
}

rule "terraform_documented_variables" {
  enabled = true
}

rule "terraform_typed_variables" {
  enabled = true
}
Run Code Online (Sandbox Code Playgroud)

tflint.sh->

#!/usr/bin/env bash
echo "Scanning all files(*.tf) with tflint"
find * -name '*.tf' | grep -E -v ".terraform|.terragrunt-cache" | while read -r line; do
    tflint "$line" -f compact
done
Run Code Online (Sandbox Code Playgroud)

显示 [WARN] 消息的 Github 工作流程输出-->

在此输入图像描述

eep*_*lip 11

从 tflint v0.39.3 Ref开始 ,您可以使用以下注释来内联忽略规则。

resource "aws_instance" "foo" {
    # tflint-ignore: aws_instance_invalid_type
    instance_type = "t1.2xlarge"
}
Run Code Online (Sandbox Code Playgroud)

从 tflint v0.40.0 Ref开始,又添加了两种注释样式。

# comma-sperated
# tflint-ignore: aws_instance_invalid_type, other_rule

# ingore all using keyword
# tflint-ignore: all
Run Code Online (Sandbox Code Playgroud)

不同的规则可以应用于资源块或其中的元素。看看terraform_naming_convention下面的例子。此规则描述了资源的 terraform 命名约定违规。要忽略此语句,该指令位于块上方。

# tflint-ignore: terraform_naming_convention
resource "random_id" "bad-example" {
  # tflint-ignore: terraform_deprecated_interpolation
  prefix = "${local.prefix}"
  keepers = {
    id = "dev-test"
  }
  byte_length = 2
}
Run Code Online (Sandbox Code Playgroud)


Sag*_*hav -4

顺便说一句,我已经成功地通过使用空设备来抑制警告消息/dev/null,并将脚本生成的 STDERR 日志重定向到2> /dev/null.

最终代码:

- name: Lint Terraform Code
  run: scripts/tflint.sh 2> /dev/null
  shell: bash
  continue-on-error: false
Run Code Online (Sandbox Code Playgroud)