For loop converting list to nested map

Hi,

I’m trying to change following code’s output to a nested list so I can access each key’s attributes using ...outputname.global.compute.id instead of ...outputname.global[0].compute.id

locals {
  keys = { for key in var.list : split("/", key.self_link)[3] => { "${key.name}" = key }... }
}


output "keys" {
  value = local.keys
}



variable "list" {
  type = list(any)
  default = [
    {
      "id"                            = "projects/test-project/locations/global/keyRings/keyring-example/cryptoKeys/bigquery"
      "key_ring"                      = "projects/test-project/locations/global/keyRings/keyring-example"
      "labels"                        = {}
      "name"                          = "bigquery"
      "purpose"                       = "ENCRYPT_DECRYPT"
      "rotation_period"               = "1000000s"
      "self_link"                     = "projects/test-project/locations/global/keyRings/keyring-example/cryptoKeys/bigquery"
      "skip_initial_version_creation" = false
      "version_template" = [
        {
          "algorithm"        = "GOOGLE_SYMMETRIC_ENCRYPTION"
          "protection_level" = "SOFTWARE"
        },
      ]
    },
    {
      "id"                            = "projects/test-project/locations/global/keyRings/keyring-example/cryptoKeys/compute"
      "key_ring"                      = "projects/test-project/locations/global/keyRings/keyring-example"
      "labels"                        = {}
      "name"                          = "compute"
      "purpose"                       = "ENCRYPT_DECRYPT"
      "rotation_period"               = "1000000s"
      "self_link"                     = "projects/test-project/locations/global/keyRings/keyring-example/cryptoKeys/compute"
      "skip_initial_version_creation" = false
      "version_template" = [
        {
          "algorithm"        = "GOOGLE_SYMMETRIC_ENCRYPTION"
          "protection_level" = "SOFTWARE"
        },
      ]
    },
    {
      "id"                            = "projects/test-project/locations/us-central1/keyRings/keyring-test2/cryptoKeys/bigquery"
      "key_ring"                      = "projects/test-project/locations/us-central1/keyRings/keyring-test2"
      "labels"                        = {}
      "name"                          = "bigquery"
      "purpose"                       = "ENCRYPT_DECRYPT"
      "rotation_period"               = "1000000s"
      "self_link"                     = "projects/test-project/locations/us-central1/keyRings/keyring-test2/cryptoKeys/bigquery"
      "skip_initial_version_creation" = false
      "version_template" = [
        {
          "algorithm"        = "GOOGLE_SYMMETRIC_ENCRYPTION"
          "protection_level" = "SOFTWARE"
        },
      ]
    },
  ]
}

current output:

Outputs:

keys = {
  "us-central1" = [
    {
      "bigquery" = {
        "id" = "projects/test-project/locations/us-central1/keyRings/keyring-test2/cryptoKeys/bigquery"
        "key_ring" = "projects/test-project/locations/us-central1/keyRings/keyring-test2"
        "labels" = {}
        "name" = "bigquery"
        "purpose" = "ENCRYPT_DECRYPT"
        "rotation_period" = "1000000s"
        "self_link" = "projects/test-project/locations/us-central1/keyRings/keyring-test2/cryptoKeys/bigquery"
        "skip_initial_version_creation" = false
        "version_template" = [
          {
            "algorithm" = "GOOGLE_SYMMETRIC_ENCRYPTION"
            "protection_level" = "SOFTWARE"
          },
        ]
      }
    },
  ]
  "global" = [
    {
      "bigquery" = {
        "id" = "projects/test-project/locations/global/keyRings/keyring-example/cryptoKeys/bigquery"
        "key_ring" = "projects/test-project/locations/global/keyRings/keyring-example"
        "labels" = {}
        "name" = "bigquery"
        "purpose" = "ENCRYPT_DECRYPT"
        "rotation_period" = "1000000s"
        "self_link" = "projects/test-project/locations/global/keyRings/keyring-example/cryptoKeys/bigquery"
        "skip_initial_version_creation" = false
        "version_template" = [
          {
            "algorithm" = "GOOGLE_SYMMETRIC_ENCRYPTION"
            "protection_level" = "SOFTWARE"
          },
        ]
      }
    },
    {
      "compute" = {
        "id" = "projects/test-project/locations/global/keyRings/keyring-example/cryptoKeys/compute"
        "key_ring" = "projects/test-project/locations/global/keyRings/keyring-example"
        "labels" = {}
        "name" = "compute"
        "purpose" = "ENCRYPT_DECRYPT"
        "rotation_period" = "1000000s"
        "self_link" = "projects/test-project/locations/global/keyRings/keyring-example/cryptoKeys/compute"
        "skip_initial_version_creation" = false
        "version_template" = [
          {
            "algorithm" = "GOOGLE_SYMMETRIC_ENCRYPTION"
            "protection_level" = "SOFTWARE"
          },
        ]
      }
    },
  ]
}

output I’m trying to get:

Outputs:

keys = {
  "us-central1" = {
    {
      "bigquery" = {
        "id" = "projects/test-project/locations/us-central1/keyRings/keyring-test2/cryptoKeys/bigquery"
        "key_ring" = "projects/test-project/locations/us-central1/keyRings/keyring-test2"
        "labels" = {}
        "name" = "bigquery"
        "purpose" = "ENCRYPT_DECRYPT"
        "rotation_period" = "1000000s"
        "self_link" = "projects/test-project/locations/us-central1/keyRings/keyring-test2/cryptoKeys/bigquery"
        "skip_initial_version_creation" = false
        "version_template" = [
          {
            "algorithm" = "GOOGLE_SYMMETRIC_ENCRYPTION"
            "protection_level" = "SOFTWARE"
          },
        ]
      }
    },
  }
  "global" = {
    {
      "bigquery" = {
        "id" = "projects/test-project/locations/global/keyRings/keyring-example/cryptoKeys/bigquery"
        "key_ring" = "projects/test-project/locations/global/keyRings/keyring-example"
        "labels" = {}
        "name" = "bigquery"
        "purpose" = "ENCRYPT_DECRYPT"
        "rotation_period" = "1000000s"
        "self_link" = "projects/test-project/locations/global/keyRings/keyring-example/cryptoKeys/bigquery"
        "skip_initial_version_creation" = false
        "version_template" = [
          {
            "algorithm" = "GOOGLE_SYMMETRIC_ENCRYPTION"
            "protection_level" = "SOFTWARE"
          },
        ]
      }
    },
    {
      "compute" = {
        "id" = "projects/test-project/locations/global/keyRings/keyring-example/cryptoKeys/compute"
        "key_ring" = "projects/test-project/locations/global/keyRings/keyring-example"
        "labels" = {}
        "name" = "compute"
        "purpose" = "ENCRYPT_DECRYPT"
        "rotation_period" = "1000000s"
        "self_link" = "projects/test-project/locations/global/keyRings/keyring-example/cryptoKeys/compute"
        "skip_initial_version_creation" = false
        "version_template" = [
          {
            "algorithm" = "GOOGLE_SYMMETRIC_ENCRYPTION"
            "protection_level" = "SOFTWARE"
          },
        ]
      }
    },
  }
}

Grouping operator ... creates list as expected and I couldn’t find a way to convert it to a map.

Thanks!