Terraform:如何迭代映射输入的键值对并使用key作为索引



以下是示例代码:

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之上的代码:如何通过json文件迭代映射输入的键值对除了索引键之外,其他都很好。

当我做地形规划时,这是样本输出:

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_securt.injections["1"]

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

如何告诉它使用keyname作为索引谢谢,

经过摸索,我终于明白了。更新了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.

相关内容

最新更新