Posts tagged operation

Get list of field values from Terraform map

If you have map or object variable or local in Terraform:

locals {
    users_object_ids = {
      "user1": { object_id: "d6c7ce3e-9f1d-4310-8862-8ab64e872246" },
      "user2": { object_id: "035c4713-14c0-43c3-a8b0-d4f21bc8ffca" },
    }
}

And you need to obtain list with object ids:

["d6c7ce3e-9f1d-4310-8862-8ab64e872246", "035c4713-14c0-43c3-a8b0-d4f21bc8ffca"]

You can use Terraform function values() (run terraform console to test it):

> values(local.users_object_ids)

[
  {
    "object_id" = "d6c7ce3e-9f1d-4310-8862-8ab64e872246"
  },
  {
    "object_id" = "035c4713-14c0-43c3-a8b0-d4f21bc8ffca"
  },
]

> values(local.users_object_ids)[*].object_id

[
  "d6c7ce3e-9f1d-4310-8862-8ab64e872246",
  "035c4713-14c0-43c3-a8b0-d4f21bc8ffca",
]

GitHub Gist: