gpt4 book ai didi

terraform - 在 Terraform 中引用 "for_each"创建的资源实例

转载 作者:行者123 更新时间:2023-12-05 05:09:40 27 4
gpt4 key购买 nike

我正在测试 Terraform 12.6 中现在可用的“for_each”资源属性,但无法在其他资源中引用创建的实例。

天蓝色的.tf


variable "customers" {
type = map(object({name=string}))
}

resource "azurerm_storage_account" "provisioning-datalake" {

for_each = var.customers

name = "mydatalake${each.key}"
resource_group_name = "${azurerm_resource_group.provisioning-group.name}"
location = "${azurerm_databricks_workspace.databricks.location}"
account_kind = "StorageV2"
account_tier = "Standard"
account_replication_type = "GRS"
is_hns_enabled = true
enable_advanced_threat_protection = true

tags = {
environment = var.environment
customer = each.value.name
}
}

resource "azurerm_key_vault_secret" "key-vault-datalake-secret" {

for_each = var.customers

name = "mydatalake-shared-key-${each.key}"
value = azurerm_storage_account.provisioning-datalake[each.key].primary_access_key
key_vault_id = azurerm_key_vault.key-vault.id

tags = {
environment = var.environment
customer = each.value.name
}
}

构建.tfvars

environment = "Build"
customers = {
dev = {
name = "Development"
},
int = {
name = "Integration"
},
stg = {
name = "Staging"
}
}

我希望使用生成的“provisioning-datalake”资源的匹配 key 创建“keyvault-datalake-secret”条目。

但是当我运行 terraform plan --var-file=build.tfvars 时,出现以下错误:

Error: Invalid index

on azure.tf line 45, in resource "azurerm_key_vault_secret" "key-vault-datalake-secret":
45: value = azurerm_storage_account.provisioning-datalake[each.key].primary_access_key
|----------------
| azurerm_storage_account.provisioning-datalake is object with 52 attributes
| each.key is "stg"

The given key does not identify an element in this collection value.


Error: Invalid index

on azure.tf line 45, in resource "azurerm_key_vault_secret" "key-vault-datalake-secret":
45: value = azurerm_storage_account.provisioning-datalake[each.key].primary_access_key
|----------------
| azurerm_storage_account.provisioning-datalake is object with 52 attributes
| each.key is "int"

The given key does not identify an element in this collection value.


Error: Invalid index

on azure.tf line 45, in resource "azurerm_key_vault_secret" "key-vault-datalake-secret":
45: value = azurerm_storage_account.provisioning-datalake[each.key].primary_access_key
|----------------
| azurerm_storage_account.provisioning-datalake is object with 52 attributes
| each.key is "dev"

The given key does not identify an element in this collection value.

最佳答案

错误已在 Terraform 0.12.7 中更正

关于terraform - 在 Terraform 中引用 "for_each"创建的资源实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57412821/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com