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

Terraform资源块内用for_each循环批量生成Azure自动扩缩容规则

Terraform Azure 自动扩缩容规则批量配置方案

1. variables.tf 嵌套变量正确类型声明

以下类型声明完全对齐azurerm_monitor_autoscale_setting资源rule块的参数规范,可按需添加可选字段:

variable "autoscale_rules" {
  type = list(object({
    #  metric_trigger 嵌套对象定义
    metric_trigger = object({
      metric_name         = string
      metric_resource_id  = string
      time_grain          = string
      statistic           = string
      time_window         = string
      time_aggregation    = string
      operator            = string
      threshold           = number
    })
    #  scale_action 嵌套对象定义
    scale_action = object({
      direction = string
      type      = string
      value     = string
      cooldown  = string
    })
  }))
  description = "统一配置的自动扩缩容规则列表"
  default     = []
}

2. 资源内动态生成rule块的for_each用法

通过Terraform动态块语法,遍历变量批量生成rule块:

resource "azurerm_monitor_autoscale_setting" "example" {
  name                = "example-autoscale"
  resource_group_name = azurerm_resource_group.example.name
  location            = azurerm_resource_group.example.location
  target_resource_id  = azurerm_app_service_plan.example.id

  profile {
    name = "default-profile"

    capacity {
      default = 1
      minimum = 1
      maximum = 10
    }

    # 批量生成rule块
    dynamic "rule" {
      for_each = var.autoscale_rules
      content {
        metric_trigger {
          metric_name         = rule.value.metric_trigger.metric_name
          metric_resource_id  = rule.value.metric_trigger.metric_resource_id
          time_grain          = rule.value.metric_trigger.time_grain
          statistic           = rule.value.metric_trigger.statistic
          time_window         = rule.value.metric_trigger.time_window
          time_aggregation    = rule.value.metric_trigger.time_aggregation
          operator            = rule.value.metric_trigger.operator
          threshold           = rule.value.metric_trigger.threshold
        }

        scale_action {
          direction = rule.value.scale_action.direction
          type      = rule.value.scale_action.type
          value     = rule.value.scale_action.value
          cooldown  = rule.value.scale_action.cooldown
        }
      }
    }
  }
}

3. 变量传值示例

调用模块或给变量赋值时参考以下格式:

autoscale_rules = [
  # 扩容规则:CPU使用率超过70%新增1个实例
  {
    metric_trigger = {
      metric_name         = "CpuPercentage"
      metric_resource_id  = azurerm_app_service_plan.example.id
      time_grain          = "PT1M"
      statistic           = "Average"
      time_window         = "PT5M"
      time_aggregation    = "Average"
      operator            = "GreaterThan"
      threshold           = 70
    }
    scale_action = {
      direction = "Increase"
      type      = "ChangeCount"
      value     = "1"
      cooldown  = "PT5M"
    }
  },
  # 缩容规则:CPU使用率低于30%减少1个实例
  {
    metric_trigger = {
      metric_name         = "CpuPercentage"
      metric_resource_id  = azurerm_app_service_plan.example.id
      time_grain          = "PT1M"
      statistic           = "Average"
      time_window         = "PT5M"
      time_aggregation    = "Average"
      operator            = "LessThan"
      threshold           = 30
    }
    scale_action = {
      direction = "Decrease"
      type      = "ChangeCount"
      value     = "1"
      cooldown  = "PT5M"
    }
  }
]

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.25 02:27:05