azure Terraform keyvault定义有问题

ttp71kqs  于 2023-06-24  发布在  其他
关注(0)|答案(1)|浏览(150)

当我为Azure keyvault定义运行terraform计划时,我收到以下错误。

│ Error: Key Vault (Subscription: "<removed>"
│ Resource Group Name: "aazurerm_kubernetes_cluster.demo.k8s-resources"
│ Key Vault Name: "tfkv") was not found
│ 
│   with data.azurerm_key_vault.demo,
│   on data.tf line 10, in data "azurerm_key_vault" "demo":
│   10: data "azurerm_key_vault" "demo" {

代码非常简单。

# Set up the key vault
resource "azurerm_key_vault" "demo" {
  name                        = "tfkv"
  location                    = "${var.location}"
  resource_group_name         = "${var.resGroup}"
  tenant_id                   = data.azurerm_client_config.current.tenant_id
  sku_name = "standard"
}

这段代码本身运行正常,但当我试图引用资源时,错误就发生了。

data "azurerm_key_vault" "demo" {
  name                = "tfkv"
  resource_group_name = "${var.resGroup}"
}
ds97pgxw

ds97pgxw1#

我尝试执行terraform步骤以实现Azure Key Vault定义,并成功执行了terraform命令。
我引用了official Terraform Resource azurerm_key_vault documentofficial Terraform Data Source: azurerm_key_vault document(https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/data-sources/key_vault)的代码,并相应地修改了它。
问题中提到的模块。

data "azurerm_key_vault" "demo" {
  name                = "tfkv"
  resource_group_name = "${var.resGroup}"
}

它对我来说也不起作用,所以我最终根据需求修改了模块,并能够产生您正在寻找的输出。
这里的要求是访问现有的azurerm_key_vault,我在data "azurerm_key_vault"中包含了输出模块,以便我们能够在输出端查看目标密钥库。
这是导致在尝试访问密钥库时出现上述错误的主要原因,这可以通过下面提到的代码来克服。

我的main.tf代码:-

provider  "azurerm" {

features {

key_vault {

purge_soft_delete_on_destroy  =  true

recover_soft_deleted_key_vaults  =  true

}

}

}

  

data  "azurerm_client_config"  "current" {}

  

resource  "azurerm_resource_group"  "example" {

name  =  "demorgvk"

location  =  "East US"

}

  

resource  "azurerm_key_vault"  "example" {

name  =  "vkbollikeyvault"

location  =  azurerm_resource_group.example.location

resource_group_name  =  azurerm_resource_group.example.name

enabled_for_disk_encryption  =  true

tenant_id  =  data.azurerm_client_config.current.tenant_id

soft_delete_retention_days  =  7

purge_protection_enabled  =  false

  

sku_name  =  "standard"

  

access_policy {

tenant_id  =  data.azurerm_client_config.current.tenant_id

object_id  =  data.azurerm_client_config.current.object_id

  

key_permissions  =  [

"Get",

]

  

secret_permissions  =  [

"Get",

]

  

storage_permissions  =  [

"Get",

]

}

}

  

data  "azurerm_key_vault"  "example" {

name  =  "vkbollikeyvault"

resource_group_name  =  "demorgvk"

}

  

output  "vault_uri" {

value  =  data.azurerm_key_vault.example.vault_uri

}

输出:

执行terrafrom_plan x1c 0d1x时
执行terraform_apply

通过使用脚本,我成功地执行了所有terraform步骤和所需的输出。

相关问题