Terraform 删除应用程序服务计划时出错 StatusCode=409 服务器场 [asp] 无法删除,因为它分配了 Web 应用程序 [azure-function]

Sti*_*ian 8 azure terraform azure-app-service-plans azure-functions

我有一个 Azure Function 和一个 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"
}

Run Code Online (Sandbox Code Playgroud)

该功能运行良好。

问题是,我现在尝试在 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>
Run Code Online (Sandbox Code Playgroud)

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

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

这种手动删除功能和服务计划的解决方法从长远来看是不可行的,所以我希望有人能帮我指出这个问题。我创建功能或服务计划的方式是否存在错误?

provider "azurerm" {
  version = "~> 2.24.0"
...
Run Code Online (Sandbox Code Playgroud)

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

Edit2:在错误论坛上,他们声称这是一个配置错误,并且我缺少依赖项。我已经用depends_on更新了代码,但仍然有同样的错误。

Sti*_*ian 5

我发现了这个问题。每次应用时,每次都会重新应用服务计划:

# 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

Run Code Online (Sandbox Code Playgroud)

尽管我创建了它,但kind="FunctionApp"它似乎已更改为"elastic"

我现在将其更改为kind="elastic",Terraform 已停止在每次应用时破坏我的服务计划:)

非常感谢 Charles Xu 的大力帮助!