使用Terraform配置Azure App Service Plan自动缩放时无法切换为规则模式
问题描述
使用Terraform的azurerm_monitor_autoscale_setting资源为Azure App Service Plan创建横向扩展自动缩放规则后,规则已成功创建,但无法通过代码将App Service Plan的缩放模式从「Automatic」切换为「Rule Based」。手动在Azure门户切换后规则可正常生效,但生产环境不允许手动操作,需完全通过Terraform实现该切换。
代码片段如下:
// autoscaling rules: resource "azurerm_monitor_autoscale_setting" "autoscaling" { name = "${var.azurerm_webapp_service_plan}-autoscale" resource_group_name = var.rg_name location = var.location target_resource_id = var.plan_type == "web_app" ? azurerm_service_plan.webapp_svcplan[0].id : azurerm_service_plan.fa_svcplan[0].id # Autoscaling Profile: Default profile { // scaling values for lower envs, change these values for Uat & Prod name = "defaultProfile" capacity { default = 1 minimum = 1 maximum = 5 } # Scale out when CPU, Memory, or SocketOutboundAll exceed thresholds rule { metric_trigger { metric_name = "CpuPercentage" metric_resource_id = var.plan_type == "web_app" ? azurerm_service_plan.webapp_svcplan[0].id : azurerm_service_plan.fa_svcplan[0].id operator = "GreaterThan" statistic = "Average" threshold = 85 time_grain = "PT1M" time_window = "PT5M" time_aggregation = "Average" }
解决方案
要通过Terraform将App Service Plan的缩放模式切换为「Rule Based」,需确保azurerm_monitor_autoscale_setting配置完整且正确,关键注意点如下:
- 显式启用自动缩放设置:添加
enabled = true配置,确保自动缩放规则处于激活状态 - 补全规则的动作部分:每个
rule块必须包含metric_trigger和scale_action,否则规则不完整,无法触发模式切换 - 确保唯一关联:确认目标App Service Plan没有关联其他自动缩放设置(一个资源仅能绑定一个自动缩放设置)
修改后的完整代码示例(补全关键部分):
resource "azurerm_monitor_autoscale_setting" "autoscaling" { name = "${var.azurerm_webapp_service_plan}-autoscale" resource_group_name = var.rg_name location = var.location target_resource_id = var.plan_type == "web_app" ? azurerm_service_plan.webapp_svcplan[0].id : azurerm_service_plan.fa_svcplan[0].id enabled = true // 显式启用自动缩放 # Autoscaling Profile: Default profile { name = "defaultProfile" capacity { default = 1 minimum = 1 maximum = 5 } # 扩展规则:CPU超过85%时增加实例 rule { metric_trigger { metric_name = "CpuPercentage" metric_resource_id = var.plan_type == "web_app" ? azurerm_service_plan.webapp_svcplan[0].id : azurerm_service_plan.fa_svcplan[0].id operator = "GreaterThan" statistic = "Average" threshold = 85 time_grain = "PT1M" time_window = "PT5M" time_aggregation = "Average" } scale_action { direction = "Increase" type = "ChangeCount" value = "1" cooldown = "PT5M" } } # 可添加更多规则(如内存阈值) rule { metric_trigger { metric_name = "MemoryPercentage" metric_resource_id = var.plan_type == "web_app" ? azurerm_service_plan.webapp_svcplan[0].id : azurerm_service_plan.fa_svcplan[0].id operator = "GreaterThan" statistic = "Average" threshold = 80 time_grain = "PT1M" time_window = "PT5M" time_aggregation = "Average" } scale_action { direction = "Increase" type = "ChangeCount" value = "1" cooldown = "PT5M" } } # 收缩规则:CPU低于20%时减少实例 rule { metric_trigger { metric_name = "CpuPercentage" metric_resource_id = var.plan_type == "web_app" ? azurerm_service_plan.webapp_svcplan[0].id : azurerm_service_plan.fa_svcplan[0].id operator = "LessThan" statistic = "Average" threshold = 20 time_grain = "PT1M" time_window = "PT10M" time_aggregation = "Average" } scale_action { direction = "Decrease" type = "ChangeCount" value = "1" cooldown = "PT10M" } } } }
执行terraform apply后,Terraform会自动将目标App Service Plan的缩放模式切换为「Rule Based」,无需手动操作。
内容的提问来源于stack exchange,提问作者JKP
相关产品推荐
相关产品推荐

