Create a MIG that uses preemptible VMs Stay organized with collections Save and categorize content based on your preferences.
This document describes how to create amanaged instance group (MIG)that uses preemptible virtual machine (VM) instances.Preemptible VMs are useful if yourworkload can tolerate disruptions and you want to take advantage of thecost-savings associated with preemptible VMs.
You can also read about otherbasic scenarios for creating a MIG.
Before you begin
- 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 an instance template with preemptible VMs
You can use a zonal or regional MIG to quickly create multiplepreemptible VMs, which can reducethe costs of the VMs in your managed instance groups. For example, youcan create a group of preemptible VMs, use them to run a batch processingtask, and then delete the group when the task is complete.
To create a group of preemptible VMs, set the preemptible option in aninstance template, and then use the templateto create the MIG.
Permissions required for this task
To perform this task, you must have the followingpermissions:
- All permissions required tocreate a new instance
compute.instanceTemplates.useon the instance template
Console
In the console, go to theInstance templates page.
The remaining steps appear in the Google Cloud console.
- Click
Create instance template . - Fill in the properties that you want for your instance template.
- Click
Advanced options and expand theManagement section. - UnderAvailability policy, in the
VM provision model list, chooseSpot. - Click
Create to create the template.
gcloud
Create an instance template by using theinstance-templates create command.Include the--preemptible flag.
gcloud compute instance-templates createINSTANCE_TEMPLATE \ --preemptible
Terraform
The following sample creates a global instance template. To provide thepreemptible option, include thescheduling block.For more information about the resource used in the sample, seegoogle_compute_instance_template resource. To create a regional instancetemplate, use thegoogle_compute_region_instance_template resource.
resource "google_compute_instance_template" "default" { name = "preemptible-template" machine_type = "n1-standard-1" disk { source_image = "debian-cloud/debian-11" } network_interface { network = "default" } scheduling { preemptible = "true" automatic_restart = "false" }}To learn how to apply or remove a Terraform configuration, seeBasic Terraform commands.
REST
Call theinstanceTemplates.insert methodto create a new instance template. Include thescheduling.preemptibleproperty and set it totrue.
{"name": "INSTANCE_TEMPLATE","properties": { "machineType": "zones/ZONE/machineTypes/MACHINE_TYPE", "networkInterfaces": [ { "network": "global/networks/default", "accessConfigs": [ { "name": "external-IP", "type": "ONE_TO_ONE_NAT" } ] } ], "scheduling": { "preemptible": true }, "disks": [ { "type": "PERSISTENT", "boot": true, "mode": "READ_WRITE", "initializeParams": { "sourceImage": "projects/debian-cloud/global/images/family/debian-9" } } ] }}After you create the instance template, use it to create a MIGwith VMs confined to a single zoneorwith VMs spread across multiple zones in a region.
What's next
- 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.
- Enable autoscaling to automatically add or deleteVMs from your MIG based on increases or decreases in load.
- 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.
- Preserve disks, metadata, and IP addresses across disruptive events like VMrecreation, autohealing, and updates byadding stateful configuration.
- Learn aboutworking with managed instances,for example, to delete, abandon, and recreate VMs in a MIG.
- View info aboutMIGs and their VMs.
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.