我有一个Azure功能和一个Azure服务计划,它们都是使用以下Terraform代码创建的:
resource "azurerm_app_service_plan" "asp" {
name = "asp-${var.environment}"
resource_group_name = var.rg_name
location = var.location
kind = "FunctionApp"
reserved = true
sku {
tier = "ElasticPremium"
size = "EP1"
}
}
resource "azurerm_function_app" "function" {
name = "function-${var.environment}"
resource_group_name= var.rg_name
location= var.location
app_service_plan_id= azurerm_app_service_plan.asp.id
storage_connection_string=azurerm_storage_account.storage.primary_connection_string
os_type = "linux"
site_config {
linux_fx_version = "DOCKER|${data.azurerm_container_registry.acr.login_server}/${var.image_name}:latest"
}
identity {
type = "SystemAssigned"
}
app_settings = {
#Lots of variables, but irrelevant for this issue I assume?
}
depends_on = [azurerm_app_service_plan.asp]
version = "~2"
}
resource "azurerm_storage_account" "storage" {
name = "storage${var.environment}"
resource_group_name = var.rg_name
location = var.location
account_tier = "Standard"
account_replication_type = "LRS"
}
该功能运行良好。
问题是,我现在试图在Terraform中进行的任何更改都会在应用过程中出现以下错误:
2020-08-25T06:31:23.256Z [DEBUG] plugin.terraform-provider-azurerm_v2.24.0_x5: {"Code":"Conflict","Message":"Server farm 'asp-staging' cannot be deleted because it has web app(s) function-staging assigned to it.","Target":null,"Details":[{"Message":"Server farm 'asp-staging' cannot be deleted because it has web app(s) function-staging assigned to it."},{"Code":"Conflict"},{"ErrorEntity":{"ExtendedCode":"11003","MessageTemplate":"Server farm '{0}' cannot be deleted because it has web app(s) {1} assigned to it.","Parameters":["asp-staging","function-staging"],"Code":"Conflict","Message":"Server farm 'asp-staging' cannot be deleted because it has web app(s) function-staging assigned to it."}}],"Innererror":null}
...
Error: Error deleting App Service Plan "asp-staging" (Resource Group "my-resource-group"): web.AppServicePlansClient#Delete: Failure sending request: StatusCode=409 -- Original Error: autorest/azure: Service returned an error. Status=<nil> <nil>
我有另一个应用程序服务的服务计划,在运行时申请没有问题。我甚至试图删除对该函数及其服务计划的所有引用,但仍然会出现同样的错误。
我可以从门户删除功能及其服务计划,然后Terraform在创建功能和服务计划时应用一次。只要Terraform应用时存在这些,它就会失败。
这种手动删除功能和服务计划的解决方案从长远来看是不可行的,所以我希望有人能帮我指出问题。我创建功能或服务计划的方式是否有错误?
provider "azurerm" {
version = "~> 2.24.0"
...
编辑:正如建议的那样,这可能是一个提供者错误,所以我创建了这个问题:https://github.com/terraform-providers/terraform-provider-azurerm/issues/8241
第2版:在bug论坛上,他们声称这是一个配置错误,我缺少一个依赖项。我已经用dependens_on更新了代码,但我仍然有同样的错误。
我发现了这个问题。每次申请时,都会重新申请服务计划:
# azurerm_app_service_plan.asp must be replaced
-/+ resource "azurerm_app_service_plan" "asp" {
~ id = "/subscriptions/xxx/resourceGroups/xxx/providers/Microsoft.Web/serverfarms/asp" -> (known after apply)
- is_xenon = false -> null
~ kind = "elastic" -> "FunctionApp" # forces replacement
location = "norwayeast"
~ maximum_elastic_worker_count = 1 -> (known after apply)
~ maximum_number_of_workers = 20 -> (known after apply)
name = "asp"
- per_site_scaling = false -> null
reserved = true
resource_group_name = "xxx"
- tags = {
- "Owner" = "XXX"
- "Service" = "XXX"
- "environment" = "staging"
} -> null
尽管我将其创建为kind="FunctionApp"
,但它似乎已更改为"elastic"
我现在把它改为kind="elastic"
,Terraform已经停止在每次申请时破坏我的服务计划:(
非常感谢许的帮助!
409的原因(在我的案例中(是无法删除具有活动应用程序服务的应用程序服务计划。当terraform试图在应用程序迁移到新计划之前删除应用程序服务时,就会发生409。解决方案是在迁移期间保留现有的应用程序服务计划,并在应用程序迁移到新计划后重新运行terraform以删除旧计划。