引用Terraform中“ for_each”创建的资源实例

时间:2019-08-08 12:39:43

标签: terraform

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

azure.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
  }
}

build.tfvars

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

我希望使用生成的“ provisioning-datalake”资源的匹配键来创建“ 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.

1 个答案:

答案 0 :(得分:0)

在Terraform 0.12.7中更正了错误