Convert Terraform map keys into a list

Viewed 94

I have a Terraform map of accounts like below.

 account_map  = {
      111111111111 = "DEV"
      222222222222 = "DEV"
      333333333333 = "STG"
      333333333333 = "PROD"
 }

How can I create a list of Dev accounts IDs as below from the above map?. I tired to use keys function but not sure how to search the DEV value inside the keys function.

dev_accounts = ["111111111111", "222222222222"]

1 Answers

For situations like this without an intrinsic function, and no possibility of a custom function, then you basically must use a for expression:

local {
  dev_accounts = [ for number, account in var.account_map : number if account == "DEV" ] # ["111111111111", "222222222222"]
}
Related