Search code examples
syntaxterraformterraform-provider-aws

Ignore specific key from terraform outputs or locals


I have the following code:

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]]),
  }
}

Now, what I need to do is that - in the ssm_only - there is this one key called "EXAMPLE_KEY" which I do not want it to include in the outputs - so meaning ignore it.

I really do not know how to do this... should I modify the local.ssm or the ssm_only output, and how?


Solution

  • Given each of the keys in local.ssm is s, one way is to apply a conditional in your for-loop to filter elements for the ssm_only output:

    if s != "EXAMPLE_KEY"

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

    If you don't want it on the local level, you can apply a similar logic to the 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"]
      # ...
    }
    

    Please note I didn't test this code, but hopefully this helps!

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