Add instance flexibility Stay organized with collections Save and categorize content based on your preferences.
This document describes how to add instance flexibility that lets you setmultiple machine types in a MIG.
You can add instance flexibility when creating a MIG or by editing an existingMIG. To add the flexibility, configure an instance flexibility policy in theMIG. The policy overrides the machine type specified in the instance template.Each time the MIG creates a virtual machine (VM) instance, it automaticallyselects one of the machine types you've listed in the policy based on resourceavailability. You can also assign ranks to the lists of machine types toindicate your preference.
To know how instance flexibility works in a MIG, seeAbout instance flexibility.
Before you begin
- Make sure you choose machine types that are supported in the regionwhere your MIG is located. To see the machine types in a region, seeAvailable regions and zones.
- 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.
Create a MIG with instance flexibility
Before creating a MIG, you must firstcreate an instance template,if you don't already have one. You can then create a MIG with instanceflexibility to override the machine type specified in the instance template.
Create a MIG with instance flexibility in one of the following ways:
Create a MIG with multiple machine types
Console
In the Google Cloud console, go to theInstance groups page.
ClickCreate instance group.
In theName field, enter a name for the MIG.
In theInstance template list, select the instance template that youwant to use for the MIG.
Before you can specify theNumber of instances and addInstance selections, you need to do the following:
Go to theLocation section on the page and set a location as follows:
SelectMultiple zones.
In theRegions and theZones drop-downs, select a region andthe zones in which you want to create the VMs in the MIG. If youselected a regional instance template, then the region of thattemplate is selected by default.
In theTarget distribution shape field, selectBalanced,Any, orAny single zone.
If you selectBalanced orAny single zone, then in thedialog that opens, clickDisable instance redistribution.
If you selectAny, then make sure that theAllow instance redistribution checkbox is not selected.
Go to theAutoscaling section on the page and delete the autoscalingconfiguration as follows:
In theAutoscaling mode drop-down, clickDelete autoscalingconfiguration.
In the dialog that opens, clickDelete.
Scroll back to theNumber of instances field.
In theNumber of instances field, specify the number of VMs that youwant in the group.
In theInstance selections section, clickAdd selections.
TheInstance selections window opens.
ClickAdd instance selection.
In theNew instance selection section, do the following:
In theName field, enter a name for the instance selection.
In theMachine types section, clickAdd machine type, selecta machine type that you want to add in the instance selection, andthen clickDone.
Repeat this step for each machine type that you want to add to theinstance selection.
After you add the machine types to the instance selection, clickDone.
In theInstance selections window, clickDone.
Leave the other fields in their default settings or modify as needed.
ClickCreate.
gcloud
To create a regional MIG with multiple machine types, use theinstance-groups managed create commandas follows:
gcloud compute instance-groups managed createINSTANCE_GROUP_NAME \ --regionREGION \ --sizeTARGET_SIZE \ --templateINSTANCE_TEMPLATE_URL \ --target-distribution-shapeSHAPE \ --instance-redistribution-type none \ --instance-selection-machine-typesMACHINE_TYPE,MACHINE_TYPE,...
Zonal MIGs don't support instance flexibility. However, if you want to createa MIG in a single zone, set the target distribution shape toany-single-zone. Furthermore, if you want a specific zone, use theany-single-zone distribution shape and include the--zonesZONE flag.
Replace the following:
INSTANCE_GROUP_NAME: the name of the MIG.REGION: the region where you want to create the MIG.TARGET_SIZE: the number of VMs you want the MIG tocreate and maintain.INSTANCE_TEMPLATE_URL: the URL of the instance template that you want touse to create VMs in the MIG. The URL can contain either theIDor name of the instance template. Specify one of the following values:- For a regional instance template:
projects/PROJECT_ID/regions/REGION/instanceTemplates/INSTANCE_TEMPLATE_ID - For a global instance template:
INSTANCE_TEMPLATE_ID
- For a regional instance template:
SHAPE: the target distribution shape. The value canbebalanced,any, orany-single-zone.MACHINE_TYPE: the machine types that you wantto configure in the MIG—for example,n1-standard-16,n2-standard-16,e2-standard-16.
Terraform
If you haven't already created an instance template, which specifies theVM properties that you want for each VM in your MIG,create an instance template.
To create a regional MIG with multiple machine types, use thegoogle_compute_region_instance_group_manager resource.
resource "google_compute_region_instance_group_manager" "default" { name = "flex-igm" base_instance_name = "tf-test-flex-igm" region = "us-central1" target_size = 3 distribution_policy_target_shape = "ANY_SINGLE_ZONE" version { instance_template = google_compute_instance_template.default.id } instance_flexibility_policy { instance_selections { name = "default-instance-selection" machine_types = ["n1-standard-16", "n2-standard-16", "e2-standard-16"] } } update_policy { instance_redistribution_type = "NONE" type = "OPPORTUNISTIC" minimal_action = "REPLACE" max_surge_fixed = 0 max_unavailable_fixed = 6 }}To learn how to apply or remove a Terraform configuration, seeBasic Terraform commands.
REST
To create a regional MIG with multiple machine types, make aPOST request totheregionInstanceGroupManagers.insert method.
POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/regions/REGION/instanceGroupManagers{ "name": "INSTANCE_GROUP_NAME", "targetSize":TARGET_SIZE, "instanceTemplate": "INSTANCE_TEMPLATE_URL", "distributionPolicy": { "targetShape": "SHAPE" }, "updatePolicy": { "instanceRedistributionType": "NONE" }, "instanceFlexibilityPolicy": { "instanceSelections": { "INSTANCE_SELECTION": { "machineTypes": [ "MACHINE_TYPE_1", "MACHINE_TYPE_2", ... ] } } }}Zonal MIGs don't support instance flexibility. However, if you want to createa MIG in a single zone, set the target distribution shape toANY_SINGLE_ZONE. Furthermore, if you want a specific zone, use theANY_SINGLE_ZONE distribution shape and include thedistributionPolicy.zones[].zone field in the request.
Replace the following:
PROJECT_ID: yourproject ID.REGION: the region where you want to create the MIG.INSTANCE_GROUP_NAME: the name of the MIG.TARGET_SIZE: the number of VMs you want the MIG tocreate and maintain.INSTANCE_TEMPLATE_URL: the URL of the instance template that you want touse to create VMs in the MIG. The URL can contain either theIDor name of the instance template. Specify one of the following values:- For a regional instance template:
projects/PROJECT_ID/regions/REGION/instanceTemplates/INSTANCE_TEMPLATE_ID - For a global instance template:
INSTANCE_TEMPLATE_ID
- For a regional instance template:
SHAPE: the target distribution shape. The value canbeBALANCED,ANY, orANY_SINGLE_ZONE.INSTANCE_SELECTION: the name for the list ofmachine types.MACHINE_TYPE: the machine types that you wantto configure in the MIG—for example,"n1-standard-16","n2-standard-16","e2-standard-16".
Create a MIG with multiple machine types and preferences
Console
In the Google Cloud console, go to theInstance groups page.
ClickCreate instance group.
In theName field, enter a name for the MIG.
In theInstance template list, select the instance template that youwant to use for the MIG.
Before you can specify theNumber of instances and addInstance selections, you need to do the following:
Go to theLocation section on the page and set a location as follows:
SelectMultiple zones.
In theRegions and theZones drop-downs, select a region andthe zones in which you want to create the VMs in the MIG. If youselected a regional instance template, then the region of thattemplate is selected by default.
In theTarget distribution shape field, selectBalanced,Any, orAny single zone.
If you selectBalanced orAny single zone, then in thedialog that opens, clickDisable instance redistribution.
If you selectAny, then make sure that theAllow instance redistribution checkbox is not selected.
Go to theAutoscaling section on the page and delete the autoscalingconfiguration as follows:
In theAutoscaling mode drop-down, clickDelete autoscalingconfiguration.
In the dialog that opens, clickDelete.
Scroll back to theNumber of instances field.
In theNumber of instances field, specify the number of VMs that youwant in the group.
In theInstance selections section, clickAdd selections.
TheInstance selections window opens.Repeat the following steps foreach instance selection that you want to add in the MIG.
ClickAdd instance selection.
In theNew instance selection section, do the following:
In theName field, enter a name for the instance selection.
In theRank field, enter the order of preference among theinstance selections that you add for the MIG.
In theMachine types section, clickAdd machine type, selecta machine type that you want to add in the instance selection, andthen clickDone.
Repeat this step for each machine type that you want to add to theinstance selection.
After you add the machine types to the instance selection, clickDone.
After you complete adding the instance selections, clickDone.
Leave the other fields in their default settings or modify as needed.
ClickCreate.
gcloud
To create a regional MIG with multiple machine types and preferences, use theinstance-groups managed create command.
gcloud compute instance-groups managed createINSTANCE_GROUP_NAME \ --regionREGION \ --sizeTARGET_SIZE \ --templateINSTANCE_TEMPLATE_URL \ --target-distribution-shapeSHAPE \ --instance-redistribution-type none \ --instance-selection "name=INSTANCE_SELECTION_1,machine-type=MACHINE_TYPE_1,machine-type=MACHINE_TYPE_2,rank=RANK_1" \ --instance-selection "name=INSTANCE_SELECTION_2,machine-type=MACHINE_TYPE_3,machine-type=MACHINE_TYPE_4,rank=RANK_2"
Zonal MIGs don't support instance flexibility. However, if you want to createa MIG in a single zone, set the target distribution shape toany-single-zone. Furthermore, if you want a specific zone, use theany-single-zone distribution shape and include the--zonesZONE flag.
Replace the following:
INSTANCE_GROUP_NAME: the name of the MIG.REGION: the region where you want to create the MIG.TARGET_SIZE: the number of VMs that you want the MIG to create and maintain.INSTANCE_TEMPLATE_URL: the URL of the instance template that you want touse to create VMs in the MIG. The URL can contain either theIDor name of the instance template. Specify one of the following values:- For a regional instance template:
projects/PROJECT_ID/regions/REGION/instanceTemplates/INSTANCE_TEMPLATE_ID - For a global instance template:
INSTANCE_TEMPLATE_ID
- For a regional instance template:
SHAPE: the target distribution shape. The value can bebalanced,any, orany-single-zone. The other target distribution shapes are not supported.INSTANCE_SELECTION: the name of the list of machine types.MACHINE_TYPE: the machine types that you want to configure in the MIG.RANK: a number representing your order of preference for instance selection. A lower value means a higher preference.
Terraform
If you haven't already created an instance template, which specifies theVM properties that you want for each VM in your MIG,create an instance template.
To create a regional MIG with multiple machine types and preferences, usethegoogle_compute_region_instance_group_manager resource.
resource "google_compute_region_instance_group_manager" "default" { name = "flex-igm" base_instance_name = "tf-test-flex-igm" region = "us-central1" target_size = 3 distribution_policy_target_shape = "ANY_SINGLE_ZONE" version { instance_template = google_compute_instance_template.default.id } instance_flexibility_policy { instance_selections { name = "best-choice" rank = 1 machine_types = ["n1-standard-1", "n1-standard-2"] } instance_selections { name = "still-ok" rank = 2 machine_types = ["n2-standard-1"] } instance_selections { name = "if-nothing-else" rank = 3 machine_types = ["e2-standard-2"] } } update_policy { instance_redistribution_type = "NONE" type = "OPPORTUNISTIC" minimal_action = "REPLACE" max_surge_fixed = 0 max_unavailable_fixed = 6 }}To learn how to apply or remove a Terraform configuration, seeBasic Terraform commands.
REST
To create a regional MIG with multiple machine types and preferences, make aPOST request to theregionInstanceGroupManagers.insert method.
POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/regions/REGION/instanceGroupManagers{ "name": "INSTANCE_GROUP_NAME", "targetSize":TARGET_SIZE, "instanceTemplate": "INSTANCE_TEMPLATE_URL", "distributionPolicy": { "targetShape": "SHAPE" }, "updatePolicy": { "instanceRedistributionType": "NONE" }, "instanceFlexibilityPolicy": { "instanceSelections": { "INSTANCE_SELECTION_1": { "machineTypes": [ "MACHINE_TYPE_1", "MACHINE_TYPE_2", ... ], "rank":RANK_1 }, "INSTANCE_SELECTION_2": { "machineTypes": [ "MACHINE_TYPE_3", "MACHINE_TYPE_4", ... ], "rank":RANK_2 }, ... } }}Zonal MIGs don't support instance flexibility. However, if you want to createa MIG in a single zone, set the target distribution shape toANY_SINGLE_ZONE. Furthermore, if you want a specific zone, use theANY_SINGLE_ZONE distribution shape and include thedistributionPolicy.zones[].zone field in the request.
Replace the following:
PROJECT_ID: yourproject ID.REGION: the region where you want to create the MIG.INSTANCE_GROUP_NAME: the name of the MIG.TARGET_SIZE: the number of VMs that you want the MIG to create and maintain.INSTANCE_TEMPLATE_URL: the URL of the instance template that you want touse to create VMs in the MIG. The URL can contain either theIDor name of the instance template. Specify one of the following values:- For a regional instance template:
projects/PROJECT_ID/regions/REGION/instanceTemplates/INSTANCE_TEMPLATE_ID - For a global instance template:
INSTANCE_TEMPLATE_ID
- For a regional instance template:
SHAPE: the target distribution shape. The value can beBALANCED,ANY, orANY_SINGLE_ZONE. The other target distribution shapes are not supported.INSTANCE_SELECTION: the name of the list of machine types.MACHINE_TYPE: the machine types that you want to configure in the MIG.RANK: a number representing your order of preference for instance selection. A lower value means a higher preference.
Add instance flexibility to an existing MIG
You can add instance flexibility to an existing MIG. You can set multiplemachine types and also set preferences for certain machine types.
If your MIG already has VMs in it, those VMs continue to use the machine typespecified in the instance template. If you want the existing VMs in the MIG touse the machine types from the instance flexibility policy, then, after you addthe policy,deletethe existing VMs and thenresize the MIGto the required number of VMs. When resizing, the new VMs will use the machinetypes from the policy.
If you want to add an instance selection to an existing instance flexibilitypolicy, seeChange instance flexibility configuration.
Add instance flexibility to a MIG in one of the following ways:
Add multiple machine types
Console
In the Google Cloud console, go to theInstance groups page.
Click the name of the MIG in which you want to set multiple machine types.
ClickEdit.
ClickInstance flexibility to expand the section.
In theInstance selections section, clickAdd selections.
TheInstance selections window opens.
ClickAdd instance selection.
In theNew instance selection section, do the following:
In theName field, enter a name for the instance selection.
In theMachine types section, clickAdd machine type, selecta machine type that you want to add in the instance selection, andthen clickDone.
Repeat this step for each machine type that you want to add to theinstance selection.
After you add the machine types to the instance selection, clickDone.
In theInstance selections window, clickDone.
ClickSave.
gcloud
To add multiple machine types to an existing MIG, use theinstance-groups managed update command.
gcloud compute instance-groups managed updateINSTANCE_GROUP_NAME \ --regionREGION \ --instance-selection-machine-typesMACHINE_TYPE,MACHINE_TYPE,...
Replace the following:
INSTANCE_GROUP_NAME: the name of the MIG.REGION: the region where the MIG is located.MACHINE_TYPE: the machine types that you want to configure in the MIG.
REST
To add multiple machine types to an existing MIG, make aPATCH request totheregionInstanceGroupManagers.patch method.
PATCH https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/regions/REGION/instanceGroupManagers/INSTANCE_GROUP_NAME{ "instanceFlexibilityPolicy": { "instanceSelections": { "INSTANCE_SELECTION": { "machineTypes": [ "MACHINE_TYPE_1", "MACHINE_TYPE_2" ... ] } } }}Replace the following:
PROJECT_ID: yourproject ID.REGION: the region where the MIG is located.INSTANCE_GROUP_NAME: the name of the MIG.INSTANCE_SELECTION: the name of the list of machine types.MACHINE_TYPE: the machine types that you want to configure in the MIG.RANK: a number representing your order of preference for instance selection. A lower value means a higher preference.
Add multiple machine types and preferences
Console
In the Google Cloud console, go to theInstance groups page.
Click the name of the MIG in which you want to set multiple machine types.
ClickEdit.
ClickInstance flexibility to expand the section.
In theInstance selections section, clickAdd selections.
TheInstance selections window opens.Repeat the following steps foreach instance selection that you want to add in the MIG.
ClickAdd instance selection.
In theNew instance selection section, do the following:
In theName field, enter a name for the instance selection.
In theRank field, enter the order of preference among theinstance selections that you add for the MIG.
In theMachine types section, clickAdd machine type, selecta machine type that you want to add in the instance selection, andthen clickDone.
Repeat this step for each machine type that you want to add to theinstance selection.
After you add the machine types to the instance selection, clickDone.
After you complete adding the instance selections, clickDone.
ClickSave.
gcloud
To add multiple machine types and preferences to an existing MIG, use theinstance-groups managed update command
gcloud compute instance-groups managed updateINSTANCE_GROUP_NAME \ --regionREGION \ --instance-selection "name=INSTANCE_SELECTION_1,machine-type=MACHINE_TYPE_1,machine-type=MACHINE_TYPE_2,rank=RANK_1" \ --instance-selection "name=INSTANCE_SELECTION_2,machine-type=MACHINE_TYPE_3,machine-type=MACHINE_TYPE_4,rank=RANK_2"
Replace the following:
INSTANCE_GROUP_NAME: the name of the MIG.REGION: the region where the MIG is located.INSTANCE_SELECTION: the name of the list of machine types.MACHINE_TYPE: the machine types that you want to configure in the MIG.RANK: a number representing your order of preference for instance selection. A lower value means a higher preference.
REST
To add multiple machine types and preferences to an existing MIG, make aPATCH request to theregionInstanceGroupManagers.patch method.
PATCH https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/regions/REGION/instanceGroupManagers/INSTANCE_GROUP_NAME{ "instanceFlexibilityPolicy": { "instanceSelections": { "INSTANCE_SELECTION_1": { "machineTypes": [ "MACHINE_TYPE_1", "MACHINE_TYPE_2" ], "rank":RANK_1 }, "INSTANCE_SELECTION_2": { "machineTypes": [ "MACHINE_TYPE_3", "MACHINE_TYPE_4" ], "rank":RANK_2 } } }}Replace the following:
PROJECT_ID: yourproject ID.REGION: the region where the MIG is located.INSTANCE_GROUP_NAME: the name of the MIG.INSTANCE_SELECTION: the name of the list of machine types.MACHINE_TYPE: the machine types that you want to configure in the MIG.RANK: a number representing your order of preference for instance selection. A lower value means a higher preference.
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-16 UTC.