google-cloud-platformterraformgoogle-compute-engineterraform-provider-gcp

schedule autoscaler in terraform for GCP?


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


Solution

  • 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.