Create a MIG with autoscaling enabled

This document describes how to create an autoscaledmanaged instance group (MIG)that automatically adds and removes VMs based on average CPU utilization acrossthe group. For example, if the group's CPU utilization is low, the groupautomatically removes VMs to save on costs.

You can automatically scale a MIG based on various kinds of autoscaling signals.For more information, see theautoscaler overview.

You can also read about otherbasic scenarios for creating a MIG.

Before you begin

Limitations

To see the full list of MIG limitations, which varies based on the configurationthat you use, seeMIG limitations.

Create a MIG and enable autoscaling

Use theGoogle Cloud console, thegcloud CLI,Terraform, orREST.

Permissions required for this task

To perform this task, you must have the followingpermissions:

  • compute.autoscalers.create on the project
  • compute.instanceGroupManagers.use on the project

Console

  1. In the console, go to theInstance groups page.

    Go to Instance groups

    The remaining steps appear in the Google Cloud console.

  2. If you have an instance group, select it and clickEdit. If you don't have an instance group, clickCreate instance group.
  3. For a new instance group, assign a name, then choose an instance template for the instance group orcreate a new one.
  4. If no autoscaling configuration exists, underAutoscaling, clickConfigure autoscaling.
  5. UnderAutoscaling mode, selectOn: add and remove instances to the group to enable autoscaling.
  6. Specify the minimum and maximum numbers of instances that you want the autoscaler to create in this group.
  7. In theAutoscaling metrics section, if an existingCPU utilization metric does not yet exist, add one:
    1. ClickAdd metric.
    2. UnderMetric type, selectCPU utilization.
    3. Enter theTarget CPU utilization that you want. This value is treated as a percentage. For example, for 75% CPU utilization, enter75.
    4. UnderPredictive autoscaling, selectOff. To learn more about predictive autoscaling, and whether it is suitable for your workload, seeScaling based on predictions.
    5. ClickDone.
  8. You can use theInitialization period to set the initialization period, which tells theautoscaler how long it takes for your application to initialize. Specifying an accurateinitialization period improves autoscaler decisions. For example, when scaling out, theautoscaler ignores data from VMs that are still initializing because those VMsmight not yet represent normal usage of your application. The default initializationperiod is 60 seconds.
  9. To create the MIG, clickCreate.

gcloud

Before you can enable autoscaling, you must create a MIG. Follow theinstructions tocreate a MIG with VMs confined to a single zoneorcreate a MIG with VMs spread across multiple zones in a region.

Then use theset-autoscalingsub-command to enable autoscaling for the group. For example,the following command creates an autoscaler that has a target CPUutilization of 60%. Along with the--target-cpu-utilization parameter,the--max-num-replicas parameter is also required when creating anautoscaler.

Optionally, you can set the--min-num-replicas indicating theminimum number of VMs that you want in the group. If you don't set theminimum, by default, MIG sets this value to 2.

You can use the--cool-down-period flag to set the initialization period, which tells theautoscaler how long it takes for your application to initialize. Specifying an accurateinitialization period improves autoscaler decisions. For example, when scaling out, theautoscaler ignores data from VMs that are still initializing because those VMsmight not yet represent normal usage of your application. The default initializationperiod is 60 seconds.

Note: If autoscaling is already enabled for a managed instance group, theset-autoscalingcommand overwrites the existing autoscaler to the new specifications.
gcloud compute instance-groups managed set-autoscaling example-managed-instance-group \  --max-num-replicas 20 \  --target-cpu-utilization 0.60 \  --cool-down-period 90

If you want, you can enable predictive autoscaling to scale out ahead ofpredicted load. To learn whether predictive autoscaling is suitable for yourworkload, seeScaling based on predictions.

You can verify that autoscaling is successfully enabled by using theinstance-groups managed describe command,which describes the corresponding MIG and provides information aboutany autoscaling features for that group:

gcloud compute instance-groups managed describe example-managed-instance-group

Terraform

Before you can enable autoscaling, you must create a MIG. Follow theinstructions tocreate a MIG with VMs confined to a single zoneorcreate a MIG with VMs spread across multiple zones in a region.

To configure autoscaling in a MIG, you can use thegoogle_compute_autoscalerresource.

The following sample configures autoscaling based on CPU utilization in azonal MIG.

resource "google_compute_autoscaler" "default" {  name   = "example-autoscaler"  zone   = "us-central1-f"  target = google_compute_instance_group_manager.default.id  autoscaling_policy {    max_replicas    = 20    min_replicas    = 0    cooldown_period = 90    cpu_utilization {      target = 0.60    }  }}

To learn how to apply or remove a Terraform configuration, seeBasic Terraform commands.

REST

Before you can enable autoscaling, you mustcreate a MIG with VMs confined to a single zoneorcreate a MIG with VMs spread across multiple zones in a region.

Note: Although autoscaling is a feature ofmanaged instance groups, it is a separate API resource. Keep that in mindwhen you construct API requests for autoscaling.

If you have a zonal MIG, make aPOST request to theautoscalers.insertmethod. If you have a regional MIG, use theregionAutoscalers.insert method.

For example:

POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/autoscalers/

Your request body must contain thename,target, andautoscalingPolicyfields. TheautoscalingPolicy field must define your targetcpuUtilization value andmaxNumReplicas value.

Optionally, you can set theminNumReplicas indicating theminimum number of VMs that you want in the group. If you don't set theminimum, by default, MIG sets this value to 2.

You can use thecoolDownPeriodSec field to set the initialization period, which tells theautoscaler how long it takes for your application to initialize. Specifying an accurateinitialization period improves autoscaler decisions. For example, when scaling out, theautoscaler ignores data from VMs that are still initializing because those VMsmight not yet represent normal usage of your application. The default initializationperiod is 60 seconds.

{  "name": "example-autoscaler",  "target": "https://www.googleapis.com/compute/v1/projects/myproject/zones/us-central1-f/instanceGroupManagers/example-managed-instance-group",  "autoscalingPolicy": {    "maxNumReplicas": 10,    "cpuUtilization": {      "utilizationTarget": 0.6    },    "coolDownPeriodSec": 90  }}

If you want, you can enable predictive autoscaling to scale out ahead ofpredicted load. To learn whether predictive autoscaling is suitable for yourworkload, seeScaling based on predictions.

For more information about enabling autoscaling based on CPU utilization, seeScaling based on CPU utilization.

What's next

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025-12-15 UTC.