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

Terraform配置GCP VM多扩缩容计划报错:重复设置name参数

问题解决:GCP实例组管理器多自动扩缩容计划的Terraform配置错误

错误原因

你在同一个scaling_schedules块内重复定义了name参数,Terraform要求每个块内的参数只能设置一次,这种写法违反了Terraform的块语法规则,导致报错。

解决方案

方案1:直接编写google_compute_autoscaler资源的正确配置

将两个扩缩容计划拆分为独立的scaling_schedules子块,每个块对应一个计划:

resource "google_compute_autoscaler" "default" {
  provider = google-beta
  name     = "my-autoscaler"
  zone     = "us-central1-f"
  target   = google_compute_instance_group_manager.default.id
  
  autoscaling_policy {
    max_replicas    = 5
    min_replicas    = 1
    cooldown_period = 60

    # 第一个扩缩容计划
    scaling_schedules {
      name                  = "every-weekday-morning-1"
      description           = "Increase to 2 every weekday at 7AM for 1 hour."
      min_required_replicas = 2
      schedule              = "0 7 * * MON-FRI"
      time_zone             = "America/New_York"
      duration_sec          = 3600
    }

    # 第二个扩缩容计划
    scaling_schedules {
      name                  = "every-weekday-morning-2"
      description           = "Increase to 3 every weekday at 8AM for 1 hour."
      min_required_replicas = 3
      schedule              = "0 8 * * MON-FRI"
      time_zone             = "America/New_York"
      duration_sec          = 3600
    }
  }
}

方案2:使用VM模块时的参数传递

如果是通过你参考的VM模块配置,需要按照变量定义的list(object)类型构造参数,将多个计划作为列表元素传入:

module "mig" {
  source = "terraform-google-modules/vm/google//modules/mig"
  # 其他必要参数(如实例模板、区域等)...

  scaling_schedules = [
    {
      name                  = "every-weekday-morning-1"
      disabled              = false
      duration_sec          = 3600
      min_required_replicas = 2
      schedule              = "0 7 * * MON-FRI"
      time_zone             = "America/New_York"
    },
    {
      name                  = "every-weekday-morning-2"
      disabled              = false
      duration_sec          = 3600
      min_required_replicas = 3
      schedule              = "0 8 * * MON-FRI"
      time_zone             = "America/New_York"
    }
  ]
}

关键说明

Terraform的嵌套块(如scaling_schedules)是多实例块,每个块代表一个独立的配置项,这和gcloud命令行通过多次添加参数或列表参数的逻辑不同,必须为每个扩缩容计划单独声明一个scaling_schedules块。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.21 21:07:19