【问题标题】:Terraform: how to iterate over key-value pairs of map input and use Key as indexTerraform:如何迭代地图输入的键值对并使用键作为索引
【发布时间】:2021-09-21 21:12:04
【问题描述】:

这里是示例代码:

    locals {
  helper_list = [
    {
    "keyname" = "app_primary_key"
    "name" = "AppPrimary--ReadWriteKey"
    "value" = "dont know yet"
    "env" = "user"
  },
    {
    "keyname" = "storage_account_key"
    "name" = "StorageAccount--ReadWriteConnectionString"
    "value" = "dont know yet"
    "env" = "app"
  },
  ]
}

resource "azurerm_key_vault_secret" "injections" {
    for_each = { for idx, v in local.helper_list: idx => v }
    name = each.value.name
    value = each.value.value
    key_vault_id = azurerm_key_vault.example.id
}

我按照这个链接在Terraform: how to iterate over key-value pairs of map input via json file上面生成代码,除了索引键之外一切都很好。

当我做 terraform plan 时,这是示例输出:

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["0"] will be created
  + resource "azurerm_key_vault_secret" "injections" {
      + id             = (known after apply)
      + key_vault_id   = "/subscriptions/c-abe849261f68/resourceGroups/storageRG/providers/Microsoft.KeyVault/vaults/keyvaultinjection"
      + name           = "AppPrimary--ReadWriteKey"
      + value          = (sensitive value)
      + version        = (known after apply)
      + versionless_id = (known after apply)
    }

  # azurerm_key_vault_secret.injections["1"] will be created
  + resource "azurerm_key_vault_secret" "injections" {
      + id             = (known after apply)
      + key_vault_id   = "/subscriptions/c-abe849261f68/resourceGroups/storageRG/providers/Microsoft.KeyVault/vaults/keyvaultinjection"
      + name           = "StorageAccount--ReadWriteConnectionString"
      + value          = (sensitive value)
      + version        = (known after apply)
      + versionless_id = (known after apply)
    }

Plan: 2 to add, 0 to change, 0 to destroy.

我需要将索引号替换为键名:

来自

azurerm_key_vault_secret.injections["0"] azurerm_key_vault_secret.injections["1"]

azurerm_key_vault_secret.injections["app_primary_key"] azurerm_key_vault_secret.injections["storage_account_key"]

如何告诉它使用键名作为索引?谢谢,

【问题讨论】:

    标签: azure foreach terraform


    【解决方案1】:

    在四处寻找之后,我能够弄清楚。更新了 for_each 以使用键名作为索引。

    resource "azurerm_key_vault_secret" "injections" {
        for_each = { for inst in local.helper_list: inst.keyname => inst }
        name = each.value.name
        value = each.value.value
        key_vault_id = azurerm_key_vault.example.id
    }
    

    更新后的样子

    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["app_primary_key"] will be created
      + resource "azurerm_key_vault_secret" "injections" {
          + id             = (known after apply)
          + key_vault_id   = "/subscriptions/c-abe849261f68/resourceGroups/storageRG/providers/Microsoft.KeyVault/vaults/keyvaultinjection"
          + name           = "AppPrimary--ReadWriteKey"
          + value          = (sensitive value)
          + version        = (known after apply)
          + versionless_id = (known after apply)
        }
    
      # azurerm_key_vault_secret.injections["storage_account_key"] will be created
      + resource "azurerm_key_vault_secret" "injections" {
          + id             = (known after apply)
          + key_vault_id   = "/subscriptions/c-abe849261f68/resourceGroups/storageRG/providers/Microsoft.KeyVault/vaults/keyvaultinjection"
          + name           = "StorageAccount--ReadWriteConnectionString"
          + value          = (sensitive value)
          + version        = (known after apply)
          + versionless_id = (known after apply)
        }
    
    Plan: 2 to add, 0 to change, 0 to destroy.
    

    【讨论】:

      猜你喜欢
      • 2021-01-17
      • 2015-02-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-04
      • 1970-01-01
      • 2022-11-10
      相关资源
      最近更新 更多