忽略来自 terraform 输出或本地的特定键

Ignore specific key from terraform outputs or locals

我有以下代码:

locals {
  env-list    = [for k, v in local.all-env-vars : { "${k}" : v }]
  test-list = [for k, v in local.test-env-vars : { "${k}" : v }]

  ssm    = setsubtract(local.env-list, local.test-list)
  test   = setsubtract(local.test-list, local.env-list)
}

output "diffs" {
  value = {
    ssm_only : nonsensitive([for s in local.ssm : keys(s)[0]]),
    test_only : nonsensitive([for s in local.test : keys(s)[0]]),
  }
}

现在,我需要做的是 - 在 ssm_only 中 - 有一个名为“EXAMPLE_KEY”的键,我不希望它包含在输出中 - 所以意思忽略它。

我真的不知道该怎么做...我应该修改 local.ssm 还是 ssm_only 输出,如何修改?

鉴于 local.ssm 中的每个键都是 s,一种方法是在 for-loop 中应用条件来过滤 ssm_only 输出的元素:

if s != "EXAMPLE_KEY"

# ...
output "diffs" {
  value = {
    ssm_only : nonsensitive([for s in local.ssm : keys(s)[0] if s != "EXAMPLE_KEY"]),
  }
}

如果您不想在本地使用它,您可以对 locals 应用类似的逻辑:

locals {
  env-list  = [for k, v in local.all-env-vars : { "${k}" : v } if k != "EXAMPLE_KEY"]
  test-list = [for k, v in local.test-env-vars : { "${k}" : v } if k != "EXAMPLE_KEY"]
  # ...
}

请注意我没有测试这段代码,但希望这对您有所帮助!

文档:https://www.terraform.io/language/expressions/for#filtering-elements