DarkDead
DarkDead

Reputation: 155

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

Upvotes: 1

Views: 62

Answers (2)

DarkDead
DarkDead

Reputation: 155

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.

Upvotes: 0

Matthew Schuchard
Matthew Schuchard

Reputation: 28854

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.

Upvotes: 1

Related Questions