Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert list to map with index in Terraform

Tags:

terraform

hcl

I would like to convert a simple list of string in terraform to a map with the keys as indexes.

I want to go from something like this:

locals {
  keycloak_secret = [
    "account-console",
    "admin-cli",
    "broker",
    "internal",
    "realm-management",
    "security-admin-console",
  ]
}

To something like

map({0:"account-console", 1:"admin-cli"}, ...) 

My goal is to take advantage of the new functionality of terraform 0.13 to use loop over map on terraform module.

I didn't find any solution, may something help me, thank you.

like image 769
severin.julien Avatar asked Dec 30 '22 22:12

severin.julien


1 Answers

If I understand correctly, you want to convert your list into map. If so, then you can do this as follows:

locals {
  keycloak_secret_map  = {for idx, val in local.keycloak_secret: idx => val}  
}

which produces:

{
  "0" = "account-console"
  "1" = "admin-cli"
  "2" = "broker"
  "3" = "internal"
  "4" = "realm-management"
  "5" = "security-admin-console"
}
like image 148
Marcin Avatar answered Mar 20 '23 22:03

Marcin