我正在通过 DevOps 中的 CI 管道使用 Terraform 将简单的基础架构部署到 Azure。
terraform init
、terraform plan
和 terraform 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" {
最佳答案
如果没有看到创建资源的第一次运行的日志,就很难说。
需要注意的一件事是,在幕后,一些复杂的资源需要多个 API 调用才能创建。如果这些 API 调用之一失败,资源仍会在云中创建,但它们可能不会进入状态文件。当出现权限错误(不允许其中一个 API 调用)或某种超时时,通常会发生这种情况。
发生这种情况时,您可以手动删除冲突的资源,或者可以将该资源导入到状态中并执行另一个 terraform apply 以完成配置。
不过,这应该是相当罕见的情况,并且初始运行的日志应该可以帮助您确定根本原因。
关于azure - 什么可能导致 Terraform "forget"它已经在管理它创建的资源?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71159918/