Add instance flexibility


This document describes how to add instance flexibility that lets you set multiple machine types in a MIG.

You can add instance flexibility when creating a MIG or by editing an existing MIG. To add the flexibility, configure an instance flexibility policy in the MIG. The policy overrides the machine type specified in the instance template. Each time the MIG creates a virtual machine (VM) instance, it automatically selects one of the machine types you've listed in the policy based on resource availability. You can also assign ranks to the lists of machine types to indicate your preference.

To know how instance flexibility works in a MIG, see About instance flexibility.

Before you begin

  • Make sure you choose machine types that are supported in the region where your MIG is located. To see the machine types in a region, see Available regions and zones.
  • If you haven't already, then set up authentication. Authentication is the process by which your identity is verified 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

    1. Install the Google Cloud CLI, then initialize it by running the following command:

      gcloud init
    2. 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.

    1. Install the Google Cloud CLI.
    2. To initialize the gcloud CLI, run the following command:

      gcloud init
    3. If you're using a local shell, then create local authentication credentials for your user account:

      gcloud auth application-default login

      You don't need to do this if you're using Cloud Shell.

    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, then initialize it by running the following command:

      gcloud init

    For more information, see Authenticate for using REST in the Google Cloud authentication documentation.

Create a MIG with instance flexibility

Before creating a MIG, you must first create an instance template, if you don't already have one. You can then create a MIG with instance flexibility 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

  1. In the Google Cloud console, go to the Instance groups page.

    Go to Instance groups

  2. Click Create instance group.

  3. In the Name field, enter a name for the MIG.

  4. In the Instance template list, select the instance template that you want to use for the MIG.

  5. Before you can specify the Number of instances and add Instance selections, you need to do the following:

    1. Set a location as follows:

      1. Scroll further to the Location section and select Multiple zones.

      2. In the Regions and the Zones drop-downs, select a region and the zones in which you want to create the VMs in the MIG. If you selected a regional instance template, then the region of that template is selected by default.

      3. In the Target distribution shape field, select Balanced or Any single zone.

      4. In the dialog that opens, click Disable instance redistribution.

    2. Delete the autoscaling configuration as follows:

      1. In the Autoscaling section, from the Autoscaling mode drop-down, click Delete autoscaling configuration.

      2. In the dialog that opens, click Delete.

    3. Scroll back to the Number of instances field.

  6. In the Number of instances field, specify the number of VMs that you want in the group.

  7. In the Instance selections section, click Add instance selections.

    The Instance selections window opens.

    1. Click Add instance selection.

    2. In the New instance selection section, do the following:

      1. In the Name field, enter a name for the instance selection.

      2. In the Machine types section, click Add machine type, select a machine type that you want to add in the instance selection, and then click Done.

        Repeat this step for each machine type that you want to add to the instance selection.

      3. After you add the machine types to the instance selection, click Done.

  8. In the Instance selections window, click Done.

  9. Leave the other fields in their default settings or modify as needed.

  10. Click Create.

gcloud

To create a regional MIG with multiple machine types, use the instance-groups managed create command as follows:

gcloud compute instance-groups managed create INSTANCE_GROUP_NAME \
    --region REGION \
    --size TARGET_SIZE \
    --template INSTANCE_TEMPLATE \
    --target-distribution-shape SHAPE \
    --instance-redistribution-type none \
    --instance-selection-machine-types MACHINE_TYPE,MACHINE_TYPE,...

Zonal MIGs don't support instance flexibility. However, if you want to create a MIG in a single zone, set the target distribution shape to any-single-zone. Furthermore, if you want a specific zone, use the any-single-zone distribution shape and include the --zones ZONE 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 to create and maintain.
  • INSTANCE_TEMPLATE: the name of the instance template.
  • SHAPE: the target distribution shape. The value can be balanced or any-single-zone. The other target distribution shapes are not supported.
  • MACHINE_TYPE: the machine types that you want to 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 the VM properties that you want for each VM in your MIG, create an instance template.

To create a regional MIG with multiple machine types, use the google_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, see Basic Terraform commands.

REST

To create a regional MIG with multiple machine types, make a POST request to the regionInstanceGroupManagers.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 create a MIG in a single zone, set the target distribution shape to ANY_SINGLE_ZONE. Furthermore, if you want a specific zone, use the ANY_SINGLE_ZONE distribution shape and include the distributionPolicy.zones[].zone field in the request.

Replace the following:

  • PROJECT_ID: your project 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 to create and maintain.
  • INSTANCE_TEMPLATE_URL: the URL of the instance template.
  • SHAPE: the target distribution shape. The value can be BALANCED or ANY_SINGLE_ZONE. The other target distribution shapes are not supported.
  • INSTANCE_SELECTION: the name for the list of machine types.
  • MACHINE_TYPE: the machine types that you want to 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

  1. In the Google Cloud console, go to the Instance groups page.

    Go to Instance groups

  2. Click Create instance group.

  3. In the Name field, enter a name for the MIG.

  4. In the Instance template list, select the instance template that you want to use for the MIG.

  5. Before you can specify the Number of instances and add Instance selections, you need to do the following:

    1. Set a location as follows:

      1. Scroll further to the Location section and select Multiple zones.

      2. In the Regions and the Zones drop-downs, select a region and the zones in which you want to create the VMs in the MIG. If you selected a regional instance template, then the region of that template is selected by default.

      3. In the Target distribution shape field, select Balanced or Any single zone.

      4. In the dialog that opens, click Disable instance redistribution.

    2. Delete the autoscaling configuration as follows:

      1. In the Autoscaling section, from the Autoscaling mode drop-down, click Delete autoscaling configuration.

      2. In the dialog that opens, click Delete.

    3. Scroll back to the Number of instances field.

  6. In the Number of instances field, specify the number of VMs that you want in the group.

  7. In the Instance selections section, click Add instance selections.

    The Instance selections window opens. Repeat the following steps for each instance selection that you want to add in the MIG.

    1. Click Add instance selection.

    2. In the New instance selection section, do the following:

      1. In the Name field, enter a name for the instance selection.

      2. In the Rank field, enter the order of preference among the instance selections that you add for the MIG.

      3. In the Machine types section, click Add machine type, select a machine type that you want to add in the instance selection, and then click Done.

        Repeat this step for each machine type that you want to add to the instance selection.

      4. After you add the machine types to the instance selection, click Done.

  8. After you complete adding the instance selections, click Done.

  9. Leave the other fields in their default settings or modify as needed.

  10. Click Create.

gcloud

To create a regional MIG with multiple machine types and preferences, use the instance-groups managed create command.

gcloud compute instance-groups managed create INSTANCE_GROUP_NAME \
    --region REGION \
    --size TARGET_SIZE \
    --template INSTANCE_TEMPLATE \
    --target-distribution-shape SHAPE \
    --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 create a MIG in a single zone, set the target distribution shape to any-single-zone. Furthermore, if you want a specific zone, use the any-single-zone distribution shape and include the --zones ZONE flag.

Replace the following:

  • INSTANCE_GROUP_NAME: the name of the MIG.
  • REGION: the region in which you want to create the MIG.
  • TARGET_SIZE: the number of VMs that you want the MIG to create and maintain.
  • INSTANCE_TEMPLATE: the name of the instance template.
  • SHAPE: the target distribution shape. The value can be balanced or any-single-zone. The other target distribution shapes are not supported.
  • INSTANCE_SELECTION: the name of the list of machine types. You can add multiple lists with different preferences.
  • MACHINE_TYPE: the machine types that you want to configure in the MIG.

Terraform

If you haven't already created an instance template, which specifies the VM 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, use the google_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, see Basic Terraform commands.

REST

To create a regional MIG with multiple machine types and preferences, make a POST request to the regionInstanceGroupManagers.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 create a MIG in a single zone, set the target distribution shape to ANY_SINGLE_ZONE. Furthermore, if you want a specific zone, use the ANY_SINGLE_ZONE distribution shape and include the distributionPolicy.zones[].zone field in the request.

Replace the following:

  • PROJECT_ID: your project 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 to create and maintain.
  • INSTANCE_TEMPLATE_URL: the URL of the instance template.
  • SHAPE: the target distribution shape. The value can be BALANCED or ANY_SINGLE_ZONE.
  • ZONE: the zone where you want to create the MIG. Use this field if you want to create a regional MIG in a particular zone. Otherwise, you can skip this field.
  • INSTANCE_SELECTION: the name of the list of machine types. You can add multiple lists with different preferences.
  • MACHINE_TYPE: the machine types that you want to configure in the MIG.
  • RANK: the number representing the order of preference. A lower value means higher preference.

Add instance flexibility to an existing MIG

You can add instance flexibility to an existing MIG. You can set multiple machine types and also set preferences for certain machine types.

If your MIG already has VMs in it, those VMs continue to use the machine type specified in the instance template. If you want the existing VMs in the MIG to use the machine types from the instance flexibility policy, then, after you add the policy, delete the existing VMs and then resize the MIG to the required number of VMs. When resizing, the new VMs will use the machine types from the policy.

If you want to add an instance selection to an existing instance flexibility policy, see Change instance flexibility configuration.

Add instance flexibility to a MIG in one of the following ways:

Add multiple machine types

Console

  1. In the Google Cloud console, go to the Instance groups page.

    Go to Instance groups

  2. Click the name of the MIG in which you want to set multiple machine types.

  3. Click Edit.

  4. In the Instance selections section, click Add instance selections.

    The Instance selections window opens.

    1. Click Add instance selection.

    2. In the New instance selection section, do the following:

      1. In the Name field, enter a name for the instance selection.

      2. In the Machine types section, click Add machine type, select a machine type that you want to add in the instance selection, and then click Done.

        Repeat this step for each machine type that you want to add to the instance selection.

      3. After you add the machine types to the instance selection, click Done.

  5. In the Instance selections window, click Done.

  6. Click Save.

gcloud

To add multiple machine types to an existing MIG, use the instance-groups managed update command.

gcloud compute instance-groups managed update INSTANCE_GROUP_NAME \
    --region REGION \
    --instance-selection-machine-types MACHINE_TYPE,MACHINE_TYPE,...

Replace the following:

  • INSTANCE_GROUP_NAME: the name of the MIG in which you want to add instance flexibility.
  • REGION: the region where the MIG is located.
  • MACHINE_TYPE: the machine types that you want to configure in the MIG—for example, n1-standard-16,n2-standard-16,e2-standard-16.

REST

To add multiple machine types to an existing MIG, make a PATCH request to the regionInstanceGroupManagers.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: your project ID.
  • REGION: the region where the MIG is located.
  • INSTANCE_GROUP_NAME: the name of the MIG in which you want to add instance flexibility.
  • INSTANCE_SELECTION: a name for the list of machine types.
  • MACHINE_TYPE: the machine types that you want to configure in the MIG—for example, "n1-standard-16","n2-standard-16","e2-standard-16".
  • RANK: the number representing the order of preference. A lower value means higher preference.

Add multiple machine types and preferences

Console

  1. In the Google Cloud console, go to the Instance groups page.

    Go to Instance groups

  2. Click the name of the MIG in which you want to set multiple machine types.

  3. Click Edit.

  4. In the Instance selections section, click Add instance selections.

    The Instance selections window opens. Repeat the following steps for each instance selection that you want to add in the MIG.

    1. Click Add instance selection.

    2. In the New instance selection section, do the following:

      1. In the Name field, enter a name for the instance selection.

      2. In the Rank field, enter the order of preference among the instance selections that you add for the MIG.

      3. In the Machine types section, click Add machine type, select a machine type that you want to add in the instance selection, and then click Done.

        Repeat this step for each machine type that you want to add to the instance selection.

      4. After you add the machine types to the instance selection, click Done.

  5. After you complete adding the instance selections, click Done.

  6. Click Save.

gcloud

To add multiple machine types and preferences to an existing MIG, use the instance-groups managed update command

gcloud compute instance-groups managed update INSTANCE_GROUP_NAME \
    --region REGION \
    --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 in which you want to add instance flexibility.
  • REGION: the region where the MIG is located.
  • INSTANCE_SELECTION: the name of the list of machine types. You can add multiple lists with different preferences.
  • MACHINE_TYPE: the machine types that you want to configure in the MIG.

REST

To add multiple machine types and preferences to an existing MIG, make a PATCH request to the regionInstanceGroupManagers.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: your project 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. You can add multiple lists with different preferences.
  • MACHINE_TYPE: the machine types that you want to configure in the MIG.

What's next