Afraz
Afraz

Reputation: 847

Extract Maps from List in Terraform

I have a list of complex maps, like so (I've simplified the maps, but really they're made up more maps):

[
  {
     "hooha" = {
         "foo" = { "something" = "this" }
      }
  },
  {
     "woot" = {
         "bar" = { "other_things" = "that" }
      }
  },
]

I need to feed this into jsonencode(), but first have it look like this:

{
   "hooha" = {
       "foo" = { "something" = "this" }
    },
   "woot" = {
       "bar" = { "other_things" = "that" }
    }
}

So from a list of maps, to a map of the maps.

This seemed trivial, but for the life of me, I can't figure it out!

Upvotes: 0

Views: 60

Answers (1)

Ervin Szilagyi
Ervin Szilagyi

Reputation: 16775

You can use the function merge with a combination of with ... operator.

locals {
  my_list = [
    {
      "hooha" = {
        "foo" = { "something" = "this" }
      },
      "www" = {
        "aa" = {"x" = "y"}
      }
    },
    {
      "woot" = {
        "bar" = { "other_things" = "that" }
      }
    },
  ]
}

output "prepare" {
  value = merge(local.my_list...)
}

The output will be something like this:

prepare = {
  "hooha" = {
    "foo" = {
      "something" = "this"
    }
  }
  "woot" = {
    "bar" = {
      "other_things" = "that"
    }
  }
  "www" = {
    "aa" = {
      "x" = "y"
    }
  }
}

Upvotes: 2

Related Questions