【问题标题】:How to suppress/ignore tflint warnings如何抑制/忽略 tflint 警告
【发布时间】:2022-10-18 04:03:39
【问题描述】:

我第一次使用 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

“.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
}

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

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

【问题讨论】:

  • v0.26.0 有点旧(2021 年 4 月 4 日)。您能否先尝试升级到最新的 v0.34.1 以排除开发人员已经修复的任何旧问题?
  • 是的,我确实尝试过使用版本0.34.1,但没有运气。实际上,我觉得这是由于我的脚本造成的吗?
  • 你也可以添加你的 Terraform 代码吗?你使用自定义的 GitHub 运行器吗?我在具有相同 tflint 版本、相同脚本和相同 .tflint.hcl 文件的私人仓库上对其进行了测试,没有任何问题。
  • 那很奇怪。好吧,不确定是否与进一步引用此Github issue 的 terraform 代码有关。
  • 你使用天蓝色插入?如果是,它的哪个版本?由于它不是您在此处发布的 .tflint.hcl 的一部分,因此我最初没有对其进行测试,但是使用此插件时,我会收到相同的错误消息。

标签: terraform github-actions lint


【解决方案1】:

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

最终代码:

- name: Lint Terraform Code
  run: scripts/tflint.sh 2> /dev/null
  shell: bash
  continue-on-error: false

【讨论】:

    【解决方案2】:

    截至 tflint v0.39.3 Ref 您可以使用以下注释来内联忽略规则。

    resource "aws_instance" "foo" {
        # tflint-ignore: aws_instance_invalid_type
        instance_type = "t1.2xlarge"
    }
    

    截至 tflint unreleased PR1492 Ref 增加了两种注释样式。

    # comma-sperated
    # tflint-ignore: aws_instance_invalid_type, other_rule
    
    # ingore all using keyword
    # tflint-ignore: all
    

    不同的规则可以应用于资源块或其中的元素。 在下面的示例中以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
    }
    

    【讨论】:

    • 奇怪的是,我发现这些注释仅在放置在资源块之外时才有效,而不是在其中。
    猜你喜欢
    • 1970-01-01
    • 2013-12-15
    • 1970-01-01
    • 1970-01-01
    • 2017-05-01
    • 2018-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多