【问题标题】:What could cause Terraform to "forget" that it's already managing a resource it created?什么可能导致 Terraform “忘记”它已经在管理它创建的资源?
【发布时间】:2023-03-20 14:11:01
【问题描述】:

我正在使用 Terraform 通过 DevOps 中的 CI 管道将一个简单的基础架构部署到 Azure。

terraform initterraform planterraform apply 运行,第一次运行,一切正常。

当我添加后续资源时,apply 失败并出现类似的错误:

│ Error: A resource with the ID "/subscriptions/578e0f86-0491-4137-9a4e-3a3c0ff28e91/resourceGroups/myResourceGroup/providers/Microsoft.ContainerService/managedClusters/stihldevlift-cluster" already exists - to be managed via Terraform this resource needs to be imported into the State. Please see the resource documentation for "azurerm_kubernetes_cluster" for more information.

Terraform 刚刚在运行之前创建了这个资源。是什么导致它忘记了这一点并将资源视为已经存在并需要导入?

注意:我在 Azure 上,根据安全策略,我们必须在 skip_provider_registration = true. 上拥有,我不知道这是否会导致问题。

在类似的问题中,“修复”是简单地摧毁一切并重新开始。我没有那种奢侈。我需要了解它发生的原因以及如何将其修复到位。这些是生产资源。

有趣的是,我可以在一个单独的开发空间中旋转所有这些,并且可以重现错误。当我删除有问题的资源时,terraform apply 会识别它们并相应地删除它们。那么这是否意味着这是一个误报。即使是这样,错误也会被 CI/CD 进程停止。

这是管道错误的完整转储。


 Error: A resource with the ID "/subscriptions/0-0491-4137-9a4e-3a3c0ff28e91/resourceGroups/DEV-Lift_test-Dev_CentralUS/providers/Microsoft.OperationsManagement/solutions/ContainerInsights(testdevliftLogAnalyticsWorkspace-12879201083717606753)" already exists - to be managed via Terraform this resource needs to be imported into the State. Please see the resource documentation for "azurerm_log_analytics_solution" for more information.
 
   with azurerm_log_analytics_solution.container_insights,
   on 02-aks-container-insights.tf line 19, in resource "azurerm_log_analytics_solution" "container_insights":
   19: resource "azurerm_log_analytics_solution" "container_insights" {
 

##[error]Terraform command 'apply' failed with exit code '1'.
##[error]╷
 Error: A resource with the ID "/subscriptions/0-0491-4137-9a4e-3a3c0ff28e91/resourceGroups/DEV-Lift_test-Dev_CentralUS/providers/Microsoft.ContainerService/managedClusters/testdevlift-cluster" already exists - to be managed via Terraform this resource needs to be imported into the State. Please see the resource documentation for "azurerm_kubernetes_cluster" for more information.
 
   with azurerm_kubernetes_cluster.k8s,
   on 02-aks-cluster-definition.tf line 4, in resource "azurerm_kubernetes_cluster" "k8s":
    4: resource "azurerm_kubernetes_cluster" "k8s" {
 


╷
 Error: A resource with the ID "/subscriptions/0-0491-4137-9a4e-3a3c0ff28e91/resourceGroups/DEV-Lift_test-Dev_CentralUS/providers/Microsoft.OperationsManagement/solutions/ContainerInsights(testdevliftLogAnalyticsWorkspace-12879201083717606753)" already exists - to be managed via Terraform this resource needs to be imported into the State. Please see the resource documentation for "azurerm_log_analytics_solution" for more information.
 
   with azurerm_log_analytics_solution.container_insights,
   on 02-aks-container-insights.tf line 19, in resource "azurerm_log_analytics_solution" "container_insights":
   19: resource "azurerm_log_analytics_solution" "container_insights" {
 

【问题讨论】:

  • 在您声明您正在“添加”一个资源的问题中,而且您已经在管理相同的资源。资源是添加到配置中还是已经存在于配置中?此外,您是否看到所有资源或资源子集的此错误?
  • 您可以尝试的另一件事是将环境变量TF_LOG 设置为适当的级别,例如调试,这可能有助于深入了解具体发生的情况。虽然您可能不想删除这组资源,但也许您可以创建另一组并运行此日志级别,这可以提供更多洞察力。 terraform.io/internals/debugging
  • @MattSchuchard 很抱歉造成混乱。我已经发布了清单,因为这里要添加的内容有点多。另外,我的意思是重新添加。我必须进行初始申请才能启动并运行核心基础架构。根据 terraform,kubernetes_manifests 需要在后续应用中应用。当我这样做时,我得到了这些资源存在并需要导入的错误。大多数与文件02有关,但在这里它们都仅供参考。 github.com/blue928/demoterraform

标签: azure terraform terraform-provider-azure


【解决方案1】:

如果没有看到创建资源的第一次运行的日志,很难说。

需要注意的一点是,在幕后创建一些复杂的资源需要多次 API 调用。如果其中一个 API 调用失败,资源仍会在云中创建,但它们可能不会进入状态文件。这通常发生在出现权限错误(不允许调用其中一个 API)或某种超时时。

发生这种情况时,您可以手动删除冲突的资源,或者您可以将该资源导入状态并执行另一个 terraform 应用以完成配置。

不过,这应该是相当罕见的情况,最初运行的日志应该可以帮助您确定根本原因是什么。

【讨论】:

    猜你喜欢
    • 2018-10-23
    • 1970-01-01
    • 2021-01-03
    • 2020-11-10
    • 1970-01-01
    • 2020-04-16
    • 2020-01-05
    • 2012-05-06
    相关资源
    最近更新 更多