title: Terraform: how to iterate over key-value pairs of map input and use Key as index
body: <p>Here is the sample code:</p>
  <pre><code>    locals {
    helper_list = [
      {
      &quot;keyname&quot; = &quot;app_primary_key&quot;
      &quot;name&quot; = &quot;AppPrimary--ReadWriteKey&quot;
      &quot;value&quot; = &quot;dont know yet&quot;
      &quot;env&quot; = &quot;user&quot;
    },
      {
      &quot;keyname&quot; = &quot;storage_account_key&quot;
      &quot;name&quot; = &quot;StorageAccount--ReadWriteConnectionString&quot;
      &quot;value&quot; = &quot;dont know yet&quot;
      &quot;env&quot; = &quot;app&quot;
    },
    ]
  }
  
  resource &quot;azurerm_key_vault_secret&quot; &quot;injections&quot; {
      for_each = { for idx, v in local.helper_list: idx =&gt; v }
      name = each.value.name
      value = each.value.value
      key_vault_id = azurerm_key_vault.example.id
  }
  </code></pre>
  <p>I followed this link to generate code above <a href="https://stackoverflow.com/questions/64164785/terraform-how-to-iterate-over-key-value-pairs-of-map-input-via-json-file">Terraform: how to iterate over key-value pairs of map input via json file</a> Everything is good except for the index key.</p>
  <p>When I do terraform plan this is the sample output:</p>
  <pre><code>An execution plan has been generated and is shown below.
  Resource actions are indicated with the following symbols:
    + create
  
  Terraform will perform the following actions:
  
    # azurerm_key_vault_secret.injections[&quot;0&quot;] will be created
    + resource &quot;azurerm_key_vault_secret&quot; &quot;injections&quot; {
        + id             = (known after apply)
        + key_vault_id   = &quot;/subscriptions/c-abe849261f68/resourceGroups/storageRG/providers/Microsoft.KeyVault/vaults/keyvaultinjection&quot;
        + name           = &quot;AppPrimary--ReadWriteKey&quot;
        + value          = (sensitive value)
        + version        = (known after apply)
        + versionless_id = (known after apply)
      }
  
    # azurerm_key_vault_secret.injections[&quot;1&quot;] will be created
    + resource &quot;azurerm_key_vault_secret&quot; &quot;injections&quot; {
        + id             = (known after apply)
        + key_vault_id   = &quot;/subscriptions/c-abe849261f68/resourceGroups/storageRG/providers/Microsoft.KeyVault/vaults/keyvaultinjection&quot;
        + name           = &quot;StorageAccount--ReadWriteConnectionString&quot;
        + value          = (sensitive value)
        + version        = (known after apply)
        + versionless_id = (known after apply)
      }
  
  Plan: 2 to add, 0 to change, 0 to destroy.
  </code></pre>
  <p>What I need is to replace the index number to a keyname:</p>
  <p>from</p>
  <blockquote>
  <blockquote>
  <p><strong>azurerm_key_vault_secret.injections[&quot;0&quot;]
  azurerm_key_vault_secret.injections[&quot;1&quot;]</strong></p>
  </blockquote>
  </blockquote>
  <p>to</p>
  <blockquote>
  <blockquote>
  <p><strong>azurerm_key_vault_secret.injections[&quot;app_primary_key&quot;]
  azurerm_key_vault_secret.injections[&quot;storage_account_key&quot;]</strong></p>
  </blockquote>
  </blockquote>
  <p><strong>How can I tell it to use the keyname as index??</strong> Thanks,</p>