Search code examples
terraformterraform-provider-aws

Convert Terraform map keys into a list


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"]


Solution

  • 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"]
    }