Terraform 上的 Azure 应用服务自动缩放错误

Posted

技术标签:

【中文标题】Terraform 上的 Azure 应用服务自动缩放错误【英文标题】:Azure App Service autoscale Error on Terraform 【发布时间】:2021-10-30 22:58:26 【问题描述】:

尝试使用 terraform for azure app service plan 创建自动缩放配置 但低于错误,

指标名称已验证,看起来不错,但不确定此错误,因为 terraform 计划中没有提供太多详细信息

地形代码

resource "azurerm_monitor_autoscale_setting" "example" 
  name                = "autoscaleappservice"
  resource_group_name = "$var.env-bsai"
  location            = var.region
  target_resource_id  = azurerm_app_service_plan.mldockers_plan.id

  profile 
    name = "defaultProfile"

    capacity 
      default = 1
      minimum = 1
      maximum = 10
    

    rule 
      metric_trigger 
        metric_name        = "CpuPercentage"
        metric_resource_id = azurerm_app_service_plan.mldockers_plan.id
        time_grain         = "PT1M"
        statistic          = "Average"
        time_window        = "PT5M"
        time_aggregation   = "Average"
        operator           = "GreaterThan"
        threshold          = 75
        metric_namespace   = "Microsoft.Web/serverfarms"
        dimensions 
          name     = "AppName"
          operator = "Equals"
          values   = ["App1"]
        
      

      scale_action 
        direction = "Increase"
        type      = "ChangeCount"
        value     = "1"
        cooldown  = "PT1M"
      
    

    rule 
      metric_trigger 
        metric_name        = "CpuPercentage"
        metric_resource_id = azurerm_app_service_plan.mldockers_plan.id
        time_grain         = "PT1M"
        statistic          = "Average"
        time_window        = "PT5M"
        time_aggregation   = "Average"
        operator           = "LessThan"
        threshold          = 25
      

      scale_action 
        direction = "Decrease"
        type      = "ChangeCount"
        value     = "1"
        cooldown  = "PT1M"
      
    
  


地形执行

执行 terraform plan 后出错

  # azurerm_monitor_autoscale_setting.example will be created
  + resource "azurerm_monitor_autoscale_setting" "example" 
      + enabled             = true
      + id                  = (known after apply)
      + location            = "centralindia"
      + name                = "autoscaleappservice"
      + resource_group_name = "dev-bsai"
      + target_resource_id  = "/subscriptions/759c1d90-b599-44a7-b677-94ac1ea9e369/resourceGroups/dev-bsai/providers/Microsoft.Web/serverfarms/dev_app_service_mldocker_plan"

      + profile 
          + name = "defaultProfile"

          + capacity 
              + default = 1
              + maximum = 10
              + minimum = 1
            

          + rule 
              + metric_trigger 
                  + metric_name        = "CpuPercentage"
                  + metric_namespace   = "Microsoft.Web/serverfarms"
                  + metric_resource_id = "/subscriptions/759c1d90-b599-44a7-b677-94ac1ea9e369/resourceGroups/dev-bsai/providers/Microsoft.Web/serverfarms/dev_app_service_mldocker_plan"
                  + operator           = "GreaterThan"
                  + statistic          = "Average"
                  + threshold          = 75
                  + time_aggregation   = "Average"
                  + time_grain         = "PT1M"
                  + time_window        = "PT5M"

                  + dimensions 
                      + name     = "AppName"
                      + operator = "Equals"
                      + values   = [
                          + "App1",
                        ]
                    
                

              + scale_action 
                  + cooldown  = "PT1M"
                  + direction = "Increase"
                  + type      = "ChangeCount"
                  + value     = 1
                
            
          + rule 
              + metric_trigger 
                  + metric_name        = "CpuPercentage"
                  + metric_resource_id = "/subscriptions/759c1d90-b599-44a7-b677-94ac1ea9e369/resourceGroups/dev-bsai/providers/Microsoft.Web/serverfarms/dev_app_service_mldocker_plan"
                  + operator           = "LessThan"
                  + statistic          = "Average"
                  + threshold          = 25
                  + time_aggregation   = "Average"
                  + time_grain         = "PT1M"
                  + time_window        = "PT5M"
                

              + scale_action 
                  + cooldown  = "PT1M"
                  + direction = "Decrease"
                  + type      = "ChangeCount"
                  + value     = 1
                
            
        
    

Plan: 1 to add, 2 to change, 0 to destroy.

------------------------------------------------------------------------

This plan was saved to: .manifests.ind.dev.tfplan

To perform exactly these actions, run the following command to apply:
    terraform apply ".manifests.ind.dev.tfplan"

manjur@MacBook-Pro terraform % APP_REGION=ind APP_ENV=dev bin/deploy manifests
azurerm_app_service_plan.fileprocess_plan: Modifying... [id=/subscriptions/759c1d90-b599-44a7-b677-94ac1ea9e369/resourceGroups/dev-bsai/providers/Microsoft.Web/serverfarms/dev_fileprocessing_plan]
azurerm_app_service_plan.mldockers_plan: Modifying... [id=/subscriptions/759c1d90-b599-44a7-b677-94ac1ea9e369/resourceGroups/dev-bsai/providers/Microsoft.Web/serverfarms/dev_app_service_mldocker_plan]
azurerm_app_service_plan.mldockers_plan: Modifications complete after 3s [id=/subscriptions/759c1d90-b599-44a7-b677-94ac1ea9e369/resourceGroups/dev-bsai/providers/Microsoft.Web/serverfarms/dev_app_service_mldocker_plan]
azurerm_monitor_autoscale_setting.example: Creating...
azurerm_app_service_plan.fileprocess_plan: Modifications complete after 4s [id=/subscriptions/759c1d90-b599-44a7-b677-94ac1ea9e369/resourceGroups/dev-bsai/providers/Microsoft.Web/serverfarms/dev_fileprocessing_plan]
azurerm_monitor_autoscale_setting.example: Still creating... [10s elapsed]
azurerm_monitor_autoscale_setting.example: Still creating... [20s elapsed]

**Error: Error creating AutoScale Setting "autoscaleappservice" (Resource Group "dev-bsai"): insights.AutoscaleSettingsClient#CreateOrUpdate: Failure sending request: StatusCode=409 -- Original Error: autorest/azure: Service returned an error. Status=<nil> <nil>
  on manifests/backend.tf line 545, in resource "azurerm_monitor_autoscale_setting" "example":
 545: resource "azurerm_monitor_autoscale_setting" "example" **

【问题讨论】:

【参考方案1】:

它发生在最初在门户中创建自动缩放计划时, 因此,您可以尝试在应用计划设置 > 横向扩展下的门户中删除 JSON Autoscale setting name,然后重试 terraform 应用。

请点击以下链接供参考:

.Autoscaling setting for app service is throwing conflict error| Git-Hub

.Error creating auto-scaling rule for app service using terraform in azure | SO THREAD

【讨论】:

以上是关于Terraform 上的 Azure 应用服务自动缩放错误的主要内容,如果未能解决你的问题,请参考以下文章

Terraform DSC 配置与节点配置中的 Azure 自动化

使用 Terraform 导入 Azure 上的现有资源

在 Terraform 中将 SSL 证书附加到 Azure 应用程序网关

使用 terraform 创建 azure 自动化 dsc 配置和 dsc 配置节点似乎不起作用

如何在 Azure Pipeline 上使用 Terraform 将文件上传到 Azure 存储?

Terraform - 长时间运行 Azure 部署错误