如何抑制/忽略 tflint 警告

发布于 2025-01-10 09:04:24 字数 1400 浏览 0 评论 0原文

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

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

输入图像描述这里

I am using tflint for the first time to scan my terraform code. For that I have created shell script to execute tflint command however, I am getting some [WARN] messages when tflint job is executed. I am not sure how they are generated. Is there a way to suppress it?

tflint command is getting executed successfully and also showing possible issues/notice in my terraform code.

I am using below Github workflow action;

      - 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" file ->

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 workflow output showing [WARN] messages-->

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

请帮我爱他 2025-01-17 09:04:24

从 tflint v0.39.3 参考
您可以使用以下注释来内联忽略规则。

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

从 tflint v0.40.0 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
}

As of tflint v0.39.3 Ref
You can use an the following annotation to inline-ignore rules.

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

As of tflint v0.40.0 Ref adds two more annotation styles.

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

# ingore all using keyword
# tflint-ignore: all

Different rules can apply to resource blocks or to the elements within.
Take terraform_naming_convention in the example below. This rule describes the terraform naming convention violation for the resource. To ignore this statement the directive is located above the block.

# 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
}
情未る 2025-01-17 09:04:24

您需要在需要忽略的块之外添加注释。例如:

# tflint-ignore: terraform_unused_declarations
variable "branch" {
  type        = string
  description = "Git branch"
}

您还可以放置多个忽略:

# tflint-ignore: terraform_unused_declarations, other_rule
variable "branch" {
  type        = string
  description = "Git branch"
}

最后您可以从 lint 中排除该块:

# tflint-ignore: all
variable "branch" {
  type        = string
  description = "Git branch"
}

注意:取决于被忽略的块,您也可以尝试将 tflint-ignore 放在块内 if该资源尚未创建。

参考:

You need to add an annotation outside the block that need to be ignored. For example :

# tflint-ignore: terraform_unused_declarations
variable "branch" {
  type        = string
  description = "Git branch"
}

you can also put multiple ignores:

# tflint-ignore: terraform_unused_declarations, other_rule
variable "branch" {
  type        = string
  description = "Git branch"
}

and finally you can exclude the block from the lint:

# tflint-ignore: all
variable "branch" {
  type        = string
  description = "Git branch"
}

Note: Depends from the ignored block, you may also try to put the tflint-ignore inside the block if the ressource is not created already.

reference:

枕梦 2025-01-17 09:04:24

我用一个创建 dynamodb 表的 terraform 文件遇到了这个问题,上面的建议都不起作用。我已经在资源块内部和外部尝试过这些方法,但它们不起作用:

# tflint-ignore: server_side_encryption
# tflint-disable: server_side_encryption
# tflint-ignore: all

tflint 项目当前版本为 v0.47,此时可能不应该使用它。

I've run into this with a terraform file that creates a dynamodb table, and none of the suggestions above work. I've tried these both inside and outside of the resource block and they don't work:

# tflint-ignore: server_side_encryption
# tflint-disable: server_side_encryption
# tflint-ignore: all

The tflint project is currently at v0.47 and it probably shouldn't be used at this point.

你如我软肋 2025-01-17 09:04:24

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

最终代码:

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

By the way, I have managed to suppress the warning messages by making use of null device /dev/null and redirected STDERR logs generated by script to 2> /dev/null.

Final Code:

- name: Lint Terraform Code
  run: scripts/tflint.sh 2> /dev/null
  shell: bash
  continue-on-error: false
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文