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

问题描述

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

Edit2: 在Bug论坛上,他们声称这是配置错误,并且我缺少依赖项。我已使用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停止在每次申请时销毁我的服务计划:)

非常感谢许旭东的帮助!

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...