google cloud platform - schedule autoscaler in terraform for GCP? - Stack Overflow

I am trying to schedule an autoscaler in GCP using terraform. I want to schedule it using cronjob and a

I am trying to schedule an autoscaler in GCP using terraform. I want to schedule it using cronjob and according to google documentation but it's not working.

This is part of my main.tf as per the google documentation:

resource "google_compute_instance_group_manager" "mig" {
  name = "${var.gcp_env}-${var.gcp_project}-mig"
  version {
    instance_template = google_compute_instance_template.debian12_template.self_link
  }

  base_instance_name = "${var.gcp_env}-${var.gcp_project}-instance"
  target_size = 2
  zone = "${var.gcp_region}-a"

  named_port {
    name = "http"
    port = 80
  }
}

resource "google_compute_autoscaler" "autoscaler" {
  name   = "${var.gcp_env}-${var.gcp_project}-autoscaler"
  zone   = "${var.gcp_region}-a"
  target = google_compute_instance_group_manager.mig.id

  autoscaling_policy {
    mode            = "ON"
    cooldown_period = 60

    cpu_utilization {
      target = 0.9
    }

    max_replicas = 2
    min_replicas = 1
  }

    scaling_schedules {
      name                  = "every-weekday-morning"
      description           = "Increase to 2 every weekday at 7AM for 12 hours."
      min_required_replicas = 2
      schedule              = "0 9 * * MON-FRI"
      time_zone             = "utc/utc"
      duration_sec          = 32400
    }
  }

When I try to run it I get the error:

$ terraform apply -auto-approve
╷
│ Error: Unsupported block type
│ 
│   on main.tf line 115, in resource "google_compute_autoscaler" "autoscaler":
│  115:     scaling_schedules {
│ 
│ Blocks of type "scaling_schedules" are not expected here.

I don't get it. When I checked the Terraform documentation it doesn't have anything for scaling_schedule (I couldn't find it after going through it.)

Would appreciate some help on solving this

I am trying to schedule an autoscaler in GCP using terraform. I want to schedule it using cronjob and according to google documentation but it's not working.

This is part of my main.tf as per the google documentation:

resource "google_compute_instance_group_manager" "mig" {
  name = "${var.gcp_env}-${var.gcp_project}-mig"
  version {
    instance_template = google_compute_instance_template.debian12_template.self_link
  }

  base_instance_name = "${var.gcp_env}-${var.gcp_project}-instance"
  target_size = 2
  zone = "${var.gcp_region}-a"

  named_port {
    name = "http"
    port = 80
  }
}

resource "google_compute_autoscaler" "autoscaler" {
  name   = "${var.gcp_env}-${var.gcp_project}-autoscaler"
  zone   = "${var.gcp_region}-a"
  target = google_compute_instance_group_manager.mig.id

  autoscaling_policy {
    mode            = "ON"
    cooldown_period = 60

    cpu_utilization {
      target = 0.9
    }

    max_replicas = 2
    min_replicas = 1
  }

    scaling_schedules {
      name                  = "every-weekday-morning"
      description           = "Increase to 2 every weekday at 7AM for 12 hours."
      min_required_replicas = 2
      schedule              = "0 9 * * MON-FRI"
      time_zone             = "utc/utc"
      duration_sec          = 32400
    }
  }

When I try to run it I get the error:

$ terraform apply -auto-approve
╷
│ Error: Unsupported block type
│ 
│   on main.tf line 115, in resource "google_compute_autoscaler" "autoscaler":
│  115:     scaling_schedules {
│ 
│ Blocks of type "scaling_schedules" are not expected here.

I don't get it. When I checked the Terraform documentation it doesn't have anything for scaling_schedule (I couldn't find it after going through it.)

Would appreciate some help on solving this

Share Improve this question asked Nov 19, 2024 at 5:02 DarkDeadDarkDead 1653 silver badges16 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 1

The google provider must be at least version 3.69.0 to support that block in that resource as per CHANGELOG. This can be accomplished through semantic versioning specification on the provider version in your required_providers block.

terraform {
  required_providers {
    google = {
      source = "hashicorp/google"
      version = "~> 3.0"
    }
  }
}

It would probably be recommended to use ~> 5.0 (or even ~> 6.0 depending upon your situation) instead, but that depends on your environment.

For some reason it worked when I commented out the scaling_schedule part in the autoscaler. So, there's that. I thought it would accept multiple policies(?) for autoscaling. Like during a certain time and/or if there's a spike in cpu but I guess only 1 works.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745581316a4634270.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信