Create a MIG with autoscaling enabled Stay organized with collections Save and categorize content based on your preferences.
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
- Create an instance template, which is required in order to create a managed instance group.
- If you haven't already, set upauthentication. Authentication verifies your identity for access to Google Cloud services and APIs. To run code or samples from a local development environment, you can authenticate to Compute Engine by selecting one of the following options:
Select the tab for how you plan to use the samples on this page:
Console
When you use the Google Cloud console to access Google Cloud services and APIs, you don't need to set up authentication.
gcloud
Install the Google Cloud CLI. After installation,initialize the Google Cloud CLI by running the following command:
gcloudinit
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
Note: If you installed the gcloud CLI previously, make sure you have the latest version by runninggcloud components update.- Set a default region and zone.
Terraform
To use the Terraform samples on this page in a local development environment, install and initialize the gcloud CLI, and then set up Application Default Credentials with your user credentials.
Install the Google Cloud CLI.
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
Note: If you installed the gcloud CLI previously, make sure you have the latest version by runninggcloud components update.If you're using a local shell, then create local authentication credentials for your user account:
gcloudauthapplication-defaultlogin
You don't need to do this if you're using Cloud Shell.
If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.
For more information, see Set up authentication for a local development environment.
REST
To use the REST API samples on this page in a local development environment, you use the credentials you provide to the gcloud CLI.
Install the Google Cloud CLI.
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
Note: If you installed the gcloud CLI previously, make sure you have the latest version by runninggcloud components update.For more information, seeAuthenticate for using REST in the Google Cloud authentication documentation.
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.createon the projectcompute.instanceGroupManagers.useon the project
Console
In the console, go to theInstance groups page.
The remaining steps appear in the Google Cloud console.
- If you have an instance group, select it and clickEdit. If you don't have an instance group, click
Create instance group . - For a new instance group, assign a name, then choose an instance template for the instance group orcreate a new one.
- If no autoscaling configuration exists, underAutoscaling, clickConfigure autoscaling.
- Under
Autoscaling mode , selectOn: add and remove instances to the group to enable autoscaling. - Specify the minimum and maximum numbers of instances that you want the autoscaler to create in this group.
- In theAutoscaling metrics section, if an existingCPU utilization metric does not yet exist, add one:
- Click
Add metric . - Under
Metric type , selectCPU utilization. - Enter theTarget CPU utilization that you want. This value is treated as a percentage. For example, for 75% CPU utilization, enter
75. - UnderPredictive autoscaling, select
Off . To learn more about predictive autoscaling, and whether it is suitable for your workload, seeScaling based on predictions. - ClickDone.
- Click
- 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.
- To create the MIG, click
Create .
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.
set-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
- Learn more aboutautoscaling and the differentkinds of scaling signals that you can add to anautoscaling policy.
- Read aboutManaging autoscalers.
- Set up application-based autohealing,which periodically verifies that your application responds as expected oneach of the MIG's VMs and automatically recreates unresponsive VMs.
- Learn how toapply a new configurationto all or to a subset of the VMs in a MIG by setting and applying a newinstance template, all-instances configuration, or per-instance configuration.
- Learn how toadd an external HTTP(S) load balancer frontend to your instancegroup. Forinformation about other types of load balancers, see theLoad balancingoverview.
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.