You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Terraform配置Azure App Service自动扩缩容时profile块不足报错排查

Terraform:仅生产环境配置自动缩放的问题解决方案

问题背景

我们有一套适配测试/生产环境的通用Terraform代码,要求仅在生产环境中创建azurerm_monitor_autoscale_setting资源,但执行terraform plan时触发如下错误:

Error: Insufficient profile blocks
│
│ on app-service\create-app-service.tf line 132, in resource "azurerm_monitor_autoscale_setting" "autoscale_setting":
│ 132: resource "azurerm_monitor_autoscale_setting" "autoscale_setting" {
│
│ At least 1 "profile" blocks are required.

原因是azurerm_monitor_autoscale_setting要求至少包含1个profile块,但测试环境下var.auto_scaleout为false,动态生成的profile块数量为0,违反了资源的必填约束。

可行解决方案

方法一:用count控制资源创建(推荐)

直接根据变量决定是否创建整个自动缩放资源,测试环境下该资源完全不生成,从根源避免必填项问题。

修改后的代码如下:

# 变量定义保留原配置
variable "auto_scaleout" {
  type    = bool
  default = false
}

resource "azurerm_monitor_autoscale_setting" "autoscale_setting" {
  # 仅当auto_scaleout为true时创建该资源
  count               = var.auto_scaleout ? 1 : 0
  name                = "${var.resource_group_name}-ui"
  location            = var.location
  resource_group_name = var.resource_group_name
  target_resource_id  = azurerm_app_service_plan.app-service-plan.id

  # 固定的profile块,无需动态生成
  profile {
    name = "${var.resource_group_name}-ui"

    capacity {
      default = 4
      minimum = 3
      maximum = 10
    }

    rule {
      metric_trigger {
        metric_name        = "CpuPercentage"
        metric_resource_id = azurerm_app_service_plan.app-service-plan.id
        time_grain         = "PT1M"
        statistic          = "Average"
        time_window        = "PT5M"
        time_aggregation   = "Average"
        operator           = "GreaterThan"
        threshold          = 75
      }

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

    rule {
      metric_trigger {
        metric_name        = "CpuPercentage"
        metric_resource_id = azurerm_app_service_plan.app-service-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"
      }
    }
  }

  notification {
    email {
      send_to_subscription_administrator    = false
      send_to_subscription_co_administrator = false
    }
  }
}

方法二:添加兜底空profile(不推荐)

如果需要保留资源存在但仅生产环境启用,可以添加一个兜底的空profile满足必填要求,同时通过enabled字段控制生效状态:

resource "azurerm_monitor_autoscale_setting" "autoscale_setting" {
  name                = "${var.resource_group_name}-ui"
  location            = var.location
  resource_group_name = var.resource_group_name
  target_resource_id  = azurerm_app_service_plan.app-service-plan.id
  # 仅生产环境启用该资源
  enabled             = var.auto_scaleout

  # 兜底空profile,满足资源的必填约束
  profile {
    name = "${var.resource_group_name}-ui-default"
    capacity {
      default = 1
      minimum = 1
      maximum = 1
    }
  }

  # 仅生产环境添加实际缩放规则的profile
  dynamic "profile" {
    for_each = var.auto_scaleout ? [1] : []
    content {
      name = "${var.resource_group_name}-ui"

      capacity {
        default = 4
        minimum = 3
        maximum = 10
      }

      rule {
        metric_trigger {
          metric_name        = "CpuPercentage"
          metric_resource_id = azurerm_app_service_plan.app-service-plan.id
          time_grain         = "PT1M"
          statistic          = "Average"
          time_window        = "PT5M"
          time_aggregation   = "Average"
          operator           = "GreaterThan"
          threshold          = 75
        }

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

      rule {
        metric_trigger {
          metric_name        = "CpuPercentage"
          metric_resource_id = azurerm_app_service_plan.app-service-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"
        }
      }
    }
  }

  notification {
    email {
      send_to_subscription_administrator    = false
      send_to_subscription_co_administrator = false
    }
  }
}

方案对比

  • 方法一:测试环境无多余资源,完全符合"仅生产环境配置"的需求,逻辑清晰,是最优解。
  • 方法二:资源会在测试环境创建但不生效,存在冗余,仅适用于必须保留资源实例的特殊场景。

内容的提问来源于stack exchange,提问作者navaneet39

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.11 06:30:58