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

Terraform为Azure App Service Plan加Scaleout Rules选data或resource

结论

必须将当前的data类型声明转换为resource类型后再配置横向扩展规则,不能保留原有的data声明。

核心原因
  • Terraform中data数据源的作用是只读拉取已存在的、不属于当前Terraform栈管理的资源信息,不支持对引用的资源做任何配置修改,自然也无法通过data块给App Service Plan添加扩缩容规则。
  • 横向扩展规则是App Service Plan资源本身的内置配置属性,只有当该资源被当前Terraform栈以resource类型纳入管理范围后,才能在配置块中定义、修改扩缩容规则,执行terraform apply时配置才会生效。
实操步骤(避免重建现有App Service Plan)

如果你要复用已经创建好的App Service Plan、不触发资源重建,不要直接替换配置块,按以下步骤操作:

  1. 将原有的data块替换为resource块,补全App Service Plan的必填基础配置,同时新增你需要的横向扩展规则(对应autoscale_settings配置块),示例配置结构:
resource "azurerm_app_service_plan" "example" {
  name                = "search-app-service-plan"
  resource_group_name = "search-service"
  location            = "替换为App Service Plan实际所在的Azure区域"
  kind                = "替换为计划实际类型,如Windows/Linux"
  # Linux类型App Service Plan需要加reserved = true
  sku {
    tier = "替换为实际SKU层级,如Standard"
    size = "替换为实际SKU规格,如S1"
  }

  # 在此处定义横向扩展规则
  autoscale_settings {
    # 填写具体的扩缩容规则配置
  }
}
  1. 执行terraform import命令,将已经存在的App Service Plan导入当前Terraform状态,避免资源被重建:
terraform import azurerm_app_service_plan.example /subscriptions/替换为你的Azure订阅ID/resourceGroups/search-service/providers/Microsoft.Web/serverfarms/search-app-service-plan
  1. 执行terraform plan预览变更,确认输出中没有资源重建的提示,仅存在自动扩缩容规则相关的变更,确认无误后执行terraform apply即可完成规则配置。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.26 11:03:16