gpt4 book ai didi

azure - 使用 Terraform 重新创建之前删除的 AKS

转载 作者:行者123 更新时间:2023-12-03 06:10:02 25 4
gpt4 key购买 nike

我在 Azure 上有一个资源组,其中包含最初使用 Terraform 脚本创建的多个资源。不知何故,我删除了 Kubernetes 集群资源并重置了 TF 状态。我的目的是重新创建此 AKS 模块,但现在当我运行 terraform 脚本管道时,出现以下错误:该资源已存在:

module.keyvault.azurerm_key_vault_access_policy.service_principle_policy:正在创建... module.keyvault.azurerm_key_vault_access_policy.users_policy:正在创建... module.keyvault.azurerm_key_vault_access_policy.readers_policy:正在创建... module.rg.azurerm_resource_group.rg:正在创建... module.keyvault.azurerm_key_vault_access_policy.readers_policy:正在创建...

我的问题是,如何在保留当前资源的同时重新创建 AKS 集群?

提前致谢。

最佳答案

由于您已从状态文件中删除了资源,一种可能的方法是通过 terraform import 命令导入相同的资源,如下所示:-

terraform import module.keyvault.azurerm_key_vault_access_policy.service_principle_policy <existing_key_vault_id>/accesspolicies/<policy_id>
terraform import module.keyvault.azurerm_key_vault_access_policy.users_policy <existing_key_vault_id>/accesspolicies/<policy_id>
terraform import module.keyvault.azurerm_key_vault_access_policy.readers_policy <existing_key_vault_id>/accesspolicies/<policy_id>
terraform import module.rg.azurerm_resource_group.rg /subscriptions/<subscription_id>/resourceGroups/<resource_group_name>

另一种方法是获取现有状态文件的 terraform 配置,然后添加 terraform 配置代码块以匹配现有状态。

检查现有状态,如下所示:-

terraform state list

enter image description here

运行 terraform show 命令检查现有配置并添加与此配置状态匹配的 AKS 代码块。

terraform show

enter image description here

为缺失或已存在的资源创建配置代码后,例如我在代码中为 NetWorkWatcherRG 创建了一个配置 block 并将其导入到我的 tfstate 中:-

enter image description here

添加了配置 block :-

resource "azurerm_resource_group" "NetworkWatcherRG" {
name = "NetworkWatcherRG"
location = var.resource_group_location

}

enter image description here

引用:-

Quickstart: Create an Azure Kubernetes Service (AKS) cluster by using Terraform - Azure Kubernetes Service | Microsoft Learn

关于azure - 使用 Terraform 重新创建之前删除的 AKS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76912374/

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