Terraform Error deleting App Service Plan StatusCode=409 服务器场 [asp] 无法删除,因为它分配有 Web 应用程序 [azure-function]

Terraform Error deleting App Service Plan StatusCode=409 Server farm [asp] cannot be deleted because it has web app(s) [azure-function] assigned to it

我有一个 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>

我有另一个带有应用程序服务的服务计划,并且在 运行ning 期间申请时没有遇到任何问题。 我什至尝试删除对该函数及其服务计划的所有引用,但仍然出现相同的错误。

我可以从门户中删除功能及其服务计划,然后 Terraform 在创建功能和服务计划时应用一次。只要这些在应用 Terraform 时存在,它就会失败。

这种手动删除功能和服务计划的解决方法在很长一段时间内是行不通的运行,所以希望有人能帮我指出问题。我创建功能或服务计划的方式是否有错误?

provider "azurerm" {
  version = "~> 2.24.0"
...

编辑: 正如所建议的那样,这可能是一个提供商错误,所以我创建了这个问题:https://github.com/terraform-providers/terraform-provider-azurerm/issues/8241

编辑2: 在错误论坛上,他们声称这是一个配置错误,并且我缺少依赖项。我用 depends_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 已停止在每次申请时破坏我的服务计划:)

非常感谢 Charles Xu 的帮助!