user3079474
user3079474

Reputation: 1793

I want to create a Terraform list of maps with some items in the list being conditional

How can I achieve the following:

I want to add map objects to a list based on whether they are passed as empty or not in the root module. So if this is passed in the variables

A = { foo = bar }
B = {}
C = { foo = bar }

then I would expect the final_list variable to be the one below

# main.tf
final_list = [{A}, {C}]

If there are alternate ways of achieving this (ie by using boolean flags), I'm ok with using those as well.

Upvotes: 0

Views: 859

Answers (1)

Maciej Rostański
Maciej Rostański

Reputation: 405

Probably you want filter usage like this:

locals {
  a = {
    foo = "bar"
  }
  b = {}
  c = {
    foo = "bar"
  }

  merge = [
    for map in [local.a,local.b,local.c] :
      map if map != {}
  ]
}

output "merge" {
  value = local.merge
}

Upvotes: 2

Related Questions