Create a MIG that uses the bulk mode

This document describes how to use bulk mode in a MIG to create all of the virtual machine (VM) instances that you require at the same time. Creating instances all at once ensures that the MIG creates instances only when all necessary resources are available.

To learn how bulk mode works, see About bulk creation of instances in a MIG.

Create an instance template

Bulk mode in MIGs is only supported with instance templates that specify either a reservation-bound or flex-start provisioning model. To use bulk mode, create an instance template using one of these provisioning model as described in the following sections. For more information about the provisioning models, see Compute Engine instances provisioning models.

Use the reservation-bound provisioning model

To create an instance template, select one of the following options:

Console

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

    Go to Instance templates

  2. Click Create instance template. The Create an instance template page opens.

  3. In the Name field, enter a name for the instance template.

  4. In the Location section, select one of the following options:

    • To create a regional instance template, select Regional (recommended), and then select the region in which to create the template.

    • To create a global instance template, select Global.

  5. In the Machine configuration section, specify a machine type.

  6. In the Provisioning model section, complete the following steps:

    1. Expand VM provisioning model advanced settings.

    2. In the On VM termination list, select Delete.

  7. Optional: To change the default value boot disk type or image, in the Boot disk section, click Change. Then, follow the prompts to change the boot disk.

  8. Expand the Advanced options section, and then do the following:

    1. Expand the Management section.

    2. In the Reservations section, select Choose a reservation, and then click Choose reservation. In the pane that appears, follow the prompts to select the reservation that you want to consume.

  9. Click Create.

gcloud

To create an instance template, use the instance-templates create command with the following flag settings:

  • The --instance-termination-action flag set to DELETE.

  • The --maintenance-policy flag set to TERMINATE.

  • The --provisioning-model flag set to RESERVATION_BOUND.

  • The --reservation-affinity flag set to specific.

  • The --reservation flag.

The following example command creates a regional instance template. If you want to create a global instance template, then use the same command without the --instance-template-region flag.

gcloud compute instance-templates create INSTANCE_TEMPLATE_NAME \
    --image-project=IMAGE_PROJECT \
    --image-family=IMAGE_FAMILY \
    --instance-termination-action=DELETE \
    --instance-template-region=REGION \
    --machine-type=MACHINE_TYPE \
    --maintenance-policy=TERMINATE \
    --provisioning-model=RESERVATION_BOUND \
    --reservation-affinity=specific \
    --reservation=RESERVATION_URL

Replace the following:

  • INSTANCE_TEMPLATE_NAME: the name of the instance template to create.

  • IMAGE_PROJECT: the image project that contains the image; for example, debian-cloud. For more information about the supported image projects, see Public images.

  • IMAGE_FAMILY: an image family. Specify an image family to use the most recent, non-deprecated OS image. For example, if you specify debian-12, the latest version in the Debian 12 image family is used. For more information about using image families, see Image families best practices.

  • REGION: the region in which to create the instance template.

  • MACHINE_TYPE: the machine type that is specified in the reservation that you want to consume.

  • RESERVATION_URL: the URL of the auto-created reservation for a future reservation in calendar mode that you want to consume. Specify one of the following values:

    • If the auto-created reservation exists in your project: RESERVATION_NAME.

    • If the auto-created reservation exists in a different project: projects/PROJECT_ID/reservations/RESERVATION_NAME.

REST

To create an instance template, make a POST request to one of the following methods:

In the request body, include the following fields:

  • The reservationAffinity.consumeReservationType field set to SPECIFIC_RESERVATION.

  • The reservationAffinity.key field set to compute.googleapis.com/reservation-name.

  • The reservationAffinity.values field set to the URL of the auto-created reservation.

  • The scheduling.instanceTerminationAction field set to DELETE.

  • The scheduling.onHostMaintenance field set to TERMINATE.

  • The scheduling.provisioningModel field set to RESERVATION_BOUND.

For example, to create a regional instance template, make a request as follows:

POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/regions/REGION/instanceTemplates

{
  "name": "INSTANCE_TEMPLATE_NAME",
  "properties": {
    "disks": [
      {
        "boot": true,
        "initializeParams": {
          "sourceImage": "projects/IMAGE_PROJECT/global/images/IMAGE"
        }
      }
    ],
    "machineType": "MACHINE_TYPE",
    "networkInterfaces": [
      {
        "network": "global/networks/default"
      }
    ],
    "reservationAffinity": {
      "consumeReservationType": "SPECIFIC_RESERVATION",
      "key": "compute.googleapis.com/reservation-name",
      "values": [
        "RESERVATION_URL"
      ]
    },
    "scheduling": {
      "instanceTerminationAction": "DELETE",
      "onHostMaintenance": "TERMINATE",
      "provisioningModel": "RESERVATION_BOUND"
    }
  }
}

Replace the following:

  • PROJECT_ID: the ID of the project in which to create the instance template.

  • REGION: the region in which to create the instance template.

  • INSTANCE_TEMPLATE_NAME: the name of the instance template to create.

  • IMAGE_PROJECT: the image project that contains the image; for example, debian-cloud. For more information about the supported image projects, see Public images.

  • IMAGE: specify one of the following:

    • A specific version of the OS image—for example, debian-12-bookworm-v20240617.

    • An image family, which must be formatted as family/IMAGE_FAMILY. Specify an image family to use the most recent, non-deprecated OS image. For example, if you specify family/debian-12, the latest version in the Debian 12 image family is used. For more information about using image families, see Image families best practices.

  • MACHINE_TYPE: the machine type that is specified in the reservation that you want to consume.

  • RESERVATION_URL: the URL of the auto-created reservation for a future reservation in calendar mode that you want to consume. Specify one of the following values:

    • If the auto-created reservation exists in your project: RESERVATION_NAME

    • If the auto-created reservation exists in a different project: projects/PROJECT_ID/reservations/RESERVATION_NAME

For more information about creating an instance template, see Create instance templates.

Use the flex-start provisioning model

To create an instance template, select one of the following options:

Console

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

    Go to Instance templates

  2. Click Create instance template. The Create an instance template page opens.

  3. In the Name field, enter a name for the instance template.

  4. In the Location section, select one of the following options:

    • To create a regional instance template, select Regional (recommended), and then select the region in which to create the template.

    • To create a global instance template, select Global.

  5. In the Machine configuration section, specify a machine type.

  6. In the Provisioning model section, complete the following steps:

    1. In the VM provisioning model field, select Flex-start.

    2. Expand VM provisioning model advanced settings.

    3. Select the Set a time limit for the VM checkbox.

    4. To set a run duration for the VMs that are created by using the instance template, in the Enter number of hours field, enter the number of hours. The value must be between 36 seconds (0.01) and seven days (168).

  7. Optional: To change the default value boot disk type or image, in the Boot disk section, click Change. Then, follow the prompts to change the boot disk.

  8. Click Create.

gcloud

To create an instance template, use the instance-templates create command with the following flag settings:

  • The --instance-termination-action flag set to DELETE.

  • The --maintenance-policy flag set to TERMINATE.

  • The --max-run-duration flag.

  • The --provisioning-model flag set to FLEX_START.

  • The --reservation-affinity flag set to none.

The following example command creates a regional instance template. If you want to create a global instance template, then use the same command without the --instance-template-region flag.

gcloud compute instance-templates create INSTANCE_TEMPLATE_NAME \
    --image-project=IMAGE_PROJECT \
    --image-family=IMAGE_FAMILY \
    --instance-template-region=REGION \
    --instance-termination-action=DELETE \
    --machine-type=MACHINE_TYPE \
    --maintenance-policy=TERMINATE \
    --max-run-duration=RUN_DURATION \
    --provisioning-model=FLEX_START \
    --reservation-affinity=none

Replace the following:

  • INSTANCE_TEMPLATE_NAME: the name of the instance template to create.

  • IMAGE_PROJECT: the image project that contains the image; for example, debian-cloud. For more information about the supported image projects, see Public images.

  • IMAGE_FAMILY: an image family. Specify an image family to use the most recent, non-deprecated OS image. For example, if you specify debian-12, the latest version in the Debian 12 image family is used. For more information about using image families, see Image families best practices.

  • REGION: the region in which to create the instance template.

  • MACHINE_TYPE: the machine type to use for the Flex-start VMs. If you specify an N1 machine type, then you must include the --accelerator flag to define the number and type of GPUs to attach to your VMs.

  • RUN_DURATION: the duration you want the requested VMs to run. You must format the value as the number of days, hours, minutes, or seconds followed by d, h, m, and s respectively. For example, specify 30m for 30 minutes or 1d2h3m4s for one day, two hours, three minutes, and four seconds. The value must be between 10 minutes and seven days.

REST

To create an instance template, make a POST request to one of the following methods:

In the request body, include the following fields:

  • The reservationAffinity.consumeReservationType field set to NO_RESERVATION.

  • The scheduling.instanceTerminationAction field set to DELETE.

  • The scheduling.maxRunDuration field.

  • The scheduling.onHostMaintenance field set to TERMINATE.

  • The scheduling.provisioningModel field set to FLEX_START.

The following example request creates a regional instance template:

POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/regions/REGION/instanceTemplates

{
  "name": "INSTANCE_TEMPLATE_NAME",
  "properties": {
    "disks": [
      {
        "boot": true,
        "initializeParams": {
          "sourceImage": "projects/IMAGE_PROJECT/global/images/IMAGE"
        }
      }
    ],
    "machineType": "MACHINE_TYPE",
    "networkInterfaces": [
      {
        "network": "global/networks/default"
      }
    ],
    "reservationAffinity": {
      "consumeReservationType": "NO_RESERVATION"
    },
    "scheduling": {
      "instanceTerminationAction": "DELETE",
      "maxRunDuration": {
        "seconds": RUN_DURATION
      },
      "onHostMaintenance": "TERMINATE",
      "provisioningModel": "FLEX_START"
    }
  }
}

Replace the following:

  • PROJECT_ID: the ID of the project in which to create the instance template.

  • REGION: the region in which to create the instance template.

  • INSTANCE_TEMPLATE_NAME: the name of the instance template to create.

  • IMAGE_PROJECT: the image project that contains the image; for example, debian-cloud. For more information about the supported image projects, see Public images.

  • IMAGE: specify one of the following:

    • A specific version of the OS image—for example, debian-12-bookworm-v20240617.

    • An image family, which must be formatted as family/IMAGE_FAMILY. Specify an image family to use the most recent, non-deprecated OS image. For example, if you specify family/debian-12, the latest version in the Debian 12 image family is used. For more information about using image families, see Image families best practices.

  • MACHINE_TYPE: the machine type to use for the VMs. If you specify an N1 machine type, then you must include the guestAccelerators field to define the number and type of GPUs to attach to your VMs.

  • RUN_DURATION: the duration, in seconds, you want the requested VMs to run. The value must be between 600, which is 600 seconds (10 minutes), and 604800, which is 604,800 seconds (seven days).

For more information about creating an instance template, see Create instance templates.

Create a MIG that uses bulk mode

To enable bulk creation of instances in a MIG, set the targetSizePolicy.mode field to BULK in the MIG's configuration. When you use this bulk mode, the MIG waits for full resource availability and then creates instances all at once.

Console

  1. Go to the Instance groups page.

    Go to Instance groups

  2. Click Create instance group. The Create instance group page opens.

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

  4. Before you select an instance template, you must do the following:

    1. If you use the flex-start provisioning model, then you must turn off repairs as follows:
      1. In the VM instance lifecycle section, clear the Allow repair of VMs in an alternate zone checkbox.
      2. In the Default action on failure list, select No action.
    2. In the Location section, do the following:
      • To create a zonal MIG, select Single zone.
      • To create a regional MIG, select Multiple zones. In the Target distribution shape, select Any single zone. In the dialog that appears, select Disable instance redistribution.
  5. Go back to the Instance template field. In the Instance template list, select the instance template that you created in the previous section.

  6. In the Number of instances field, specify the number of instances that you want to create all at once.

  7. Select the Obtain VMs all at once with bulk target size policy checkbox.

  8. Click Create.

gcloud

To create a MIG that has bulk creation enabled, use the gcloud compute instance-groups managed create command with the --target-size-policy-mode flag set to bulk.

If you use the flex-start provisioning model, then you must turn off repairs in the MIG by including the --default-action-on-vm-failure=do-nothing flag in the following commands.

  • To create a zonal MIG, run the following command:
gcloud compute instance-groups managed create INSTANCE_GROUP_NAME \
    --size=SIZE \
    --target-size-policy-mode=bulk \
    --template=INSTANCE_TEMPLATE_URL \
    --zone=ZONE \
  • To create a regional MIG, run the following command:
gcloud compute instance-groups managed create INSTANCE_GROUP_NAME \
    --size=SIZE \
    --target-size-policy-mode=bulk \
    --template=INSTANCE_TEMPLATE_URL \
    --zones=LIST_OF_ZONES \

Replace the following:

  • INSTANCE_GROUP_NAME: the name of the MIG.
  • SIZE: the number of instances in the MIG.
  • INSTANCE_TEMPLATE_URL: the URL of the instance template that you want to use to create instances in the MIG. The URL can contain either the ID or 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
  • ZONE: the zone where the MIG creates instances.
  • LIST_OF_ZONES: a comma-separated list of zones where the regional MIG creates instances. For example, us-central1-a, us-central1-b, us-central1-c.

REST

To create a MIG that has bulk creation enabled, make a POST request as follows. In the request body, set the targetSizePolicy.mode field to bulk.

If you use the flex-start provisioning model, then you must turn off repairs in the MIG by including the "instanceLifecyclePolicy": {"defaultActionOnFailure": "DO_NOTHING"} field in the following requests.

  • To create a zonal MIG, make a POST request to the instanceGroupManagers.insert method.

    POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instanceGroupManagers
    
    {
      "name": "INSTANCE_GROUP_NAME",
      "targetSize": SIZE,
      "targetSizePolicy": {
        "mode": "bulk"
      },
      "versions": [
        {
          "instanceTemplate": "INSTANCE_TEMPLATE_URL"
        }
      ]
    }
    
  • To create a regional MIG, 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": SIZE,
      "targetSizePolicy": {
        "mode": "bulk"
      },
      "versions": [
        {
          "instanceTemplate": "INSTANCE_TEMPLATE_URL"
        }
      ]
    }
    

Replace the following:

  • PROJECT_ID:the project ID.
  • ZONE:the zone where the MIG creates instances.
  • REGION:the region where the MIG creates instances.
  • INSTANCE_GROUP_NAME:the name of the MIG.
  • SIZE:the number of instances in the MIG.
  • INSTANCE_TEMPLATE_URL: the URL of the instance template that you want to use to create instances in the MIG. The URL can contain either the ID or 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

Add or delete instances

In a MIG that uses the bulk mode, you can add or remove instances by manually increasing or decreasing the target size of the MIG.

  • Add instances: When you increase the target size from zero, the MIG adds instances the same way as when creating a MIG. You can also create instances with specific names by using the createInstances method when there is no instance in the MIG. To create instances with specific names, see Creating instances with specific names in MIGs.

  • Delete instances: When you decrease the target size to zero, the MIG deletes all existing instances in the group and cancels any creation operation that was in progress. You can remove instances by decreasing the target size to 0. To decrease to a non-zero size, the MIG must not have a bulk instance operation in progress (bulkInstanceOperation.inProgress must be false).

To add or remove instances from a MIG by changing the target size, see Manually setting the size of a MIG.

What's next