如何查找 Terraform 代码中未使用的所有 tf​​vars 变量的列表?

Bij*_*iju 4 amazon-web-services terraform

s3.tfvars

bucket = "first-bucket"
acl="private"
env = "dev"
owner = "abc@def.com"
var1 = "unused variable"
Run Code Online (Sandbox Code Playgroud)

s3.tf

resource "aws_s3_bucket" "abucket" {
  bucket = var.bucket 
  acl = "private"

  tags = {
    Environment = var.env
    Owner = var.owner
  }
}
Run Code Online (Sandbox Code Playgroud)

var1 没有在我的代码中使用,即使它是在 tfvars 中声明的,如上所示。

当我运行以下命令时

terraform plan -var-file=s3.tfvars
Run Code Online (Sandbox Code Playgroud)

...,我收到以下警告消息:

Warning: Value for undeclared variable

The root module does not declare a variable named "var1" but a value was
found in file "s3.tfvars". To use this value, add a "variable" block to the
configuration.
Run Code Online (Sandbox Code Playgroud)

有没有办法将此警告捕获为错误?或者还有其他方法可以找出所有未使用变量的列表吗?

Som*_*ter 9

您想使用tflint

mkdir test/
cd test/
echo 'variable "what" {}' > main.tf
Run Code Online (Sandbox Code Playgroud)

添加.tflint.hcl

rule "terraform_unused_declarations" {
  enabled = true
}
Run Code Online (Sandbox Code Playgroud)
mkdir test/
cd test/
echo 'variable "what" {}' > main.tf
Run Code Online (Sandbox Code Playgroud)