如何使用 Terraform 在现有的 keyvault 中创建秘密?

How to create secret in existing keyvault with Terraform?

我有一个简单的 Terraform 配置来在 Azure keyvault 中创建秘密。

provider "azurerm" {
  features {}
}

data "azurerm_key_vault" "SomeApp-DEV" {
  name = "SomeApp-DEV"
  resource_group_name = "SomeApp"
}

resource "azurerm_key_vault_secret" "test-secret" {
  name = "some-key"
  value = "test value"
  key_vault_id = data.azurerm_key_vault.SomeApp-DEV
}

terraform plan 之后出现以下错误:

Error: Incorrect attribute value type

  on secret.tf line 13, in resource "azurerm_key_vault_secret" "test-secret":
  13:   key_vault_id = data.azurerm_key_vault.SomeApp-DEV
    ├────────────────
    │ data.azurerm_key_vault.SomeApp-DEV is object with 17 attributes

Inappropriate value for attribute "key_vault_id": string required.

如何让它发挥作用?我不知道这条 object with 17 attributes 消息是什么意思?

当您使用命名空间 data.<type>.<name> 访问导出属性时,您将访问该数据中导出属性的整个映射(资源的导出属性也是如此)。在这种情况下,您只需要 id 的字符串,其值分配给导出属性映射中的键 id

resource "azurerm_key_vault_secret" "test-secret" {
  name = "some-key"
  value = "test value"
  key_vault_id = data.azurerm_key_vault.SomeApp-DEV.id
}

这将解决您的问题。