Create and use preemptible VMs

This page explains how to create and use a preemptible virtual machine (VM) instance. Preemptible VMs are available at a 60-91% discount compared to the price of standard VMs. However, Compute Engine might stop (preempt) these instances if it needs to reclaim those resources for other tasks. Preemptible instances always stop after 24 hours. To learn more about preemptible instances, read the preemptible instances documentation.

Preemptible instances are recommended only for fault-tolerant applications that can withstand instance preemptions. Make sure your application can handle preemptions before you decide to create a preemptible instance. To understand the risks and value of preemptible instances, read the preemptible instances documentation.

Before you begin

Creating a preemptible instance

Create a preemptible instance through the Google Cloud Console, the gcloud tool, or the API.


Creating a preemptible instance is the same as creating a normal instance, but you enable the preemptible property.

  1. In the Google Cloud Console, go to the Create an instance page.

    Go to Create an instance

  2. Specify the VM details.

  3. Expand the Networking, disks, security, management, sole tenancy section, and do the following:

    1. Expand the Management section.
    2. For Availability policy, set the Preemptibility option to On. This setting disables automatic restart for the VM, and sets the host maintenance action to Terminate.
  4. To create and start the VM, click Create.


With gcloud compute, use the same instances create command that you would use to create a normal instance, but add the --preemptible flag.

gcloud compute instances create [VM_NAME] --preemptible

where [VM_NAME] is the name of the instance.


In the API, construct a normal request to create an instance, but include the preemptible property under scheduling and set it to true. For example:


  'machineType': 'zones/[ZONE]/machineTypes/[MACHINE_TYPE]',
  'name': '[INSTANCE_NAME]',
    'preemptible': true

Preemptible CPU quotas

Preemptible instances require available CPU quotas like regular instances. To avoid preemptible instances consuming the CPU quotas for your regular instances, you can request a special "Preemptible CPU" quota. After Compute Engine grants you preemptible CPU quota in that region, all preemptible instances count against that quota, and all regular instances continue to count against the regular CPU quota.

In regions where you don't have preemptible CPU quota, you can use regular CPU quota to launch preemptible instances. You also need sufficient IP and disk quota, as usual. Preemptible CPU quota is not visible in the gcloud tool or Cloud Console quota pages unless Compute Engine has granted the quota.

For more information about quotas, visit the Resource Quotas page.

Starting a preempted VM

Like any other VM, if a preemptible VM is stopped or preempted, you can start the VM again and bring it back to the RUNNING state. Starting a preemptible VM resets the 24-hour counter but as it is still a preemptible VM, Compute Engine can preempt before 24 hours. It isn't possible to convert a preemptible instance to a standard instance while it's running.

If Compute Engine stops a preemptible VM in an autoscaling managed instance group (MIG) or Google Kubernetes Engine (GKE) cluster, the group restarts the VM when the resources become available again.

Handling preemption with a shutdown script

When your instance is preempted, you can use a shutdown script to perform cleanup actions before the instance stops. For example, you can gracefully stop a running process and copy a checkpoint file to Cloud Storage.

The following is a shutdown script that you can add to a running preemptible instance or add to a new preemptible instance when you create it. This script runs when the instance starts to shut down, before the operating system's normal kill command stops all remaining processes. After gracefully stopping the desired program, the script performs a parallel upload of a checkpoint file to a Cloud Storage bucket.


MY_PROGRAM="[PROGRAM_NAME]" # For example, "apache2" or "nginx"
GSUTIL_OPTS="-m -o GSUtil:parallel_composite_upload_threshold=32M"
BUCKET_NAME="[BUCKET_NAME]" # For example, "my-checkpoint-files" (without gs://)

echo "Shutting down!  Seeing if ${MY_PROGRAM} is running."

# Find the newest copy of $MY_PROGRAM
PID="$(pgrep -n "$MY_PROGRAM")"

if [[ "$?" -ne 0 ]]; then
  echo "${MY_PROGRAM} not running, shutting down immediately."
  exit 0

echo "Sending SIGINT to $PID"
kill -2 "$PID"

# Portable waitpid equivalent
while kill -0 "$PID"; do
   sleep 1

echo "$PID is done, copying ${CHECKPOINT} to gs://${BUCKET_NAME} as ${MY_USER}"

su "${MY_USER}" -c "gsutil $GSUTIL_OPTS cp $CHECKPOINT gs://${BUCKET_NAME}/"

echo "Done uploading, shutting down."

To add this script to an instance, configure the script to work with an application on your instance and add it to your instance metadata.

  1. Copy or download the shutdown script to your local workstation.
  2. Open the file for edit and change the following variables:
    • [PROGRAM_NAME] is the name of the process or program you want to shut down. For example, apache2 or nginx.
    • [LOCAL_USER] is the username you are logged into the virtual machine as.
    • [BUCKET_NAME] is the name of the Cloud Storage bucket where you want to save the program's checkpoint file. Note the bucket name does not start with gs:// in this case.
  3. Save your changes.
  4. Add the shutdown script to a new instance or an existing instance.

This script assumes the following:

  • The instance was created with at least read/write access to Cloud Storage. See the authentication documentation for instructions about how to create an instance with the appropriate scopes.

  • You have an existing Cloud Storage bucket and permission to write to it.

Checking if an instance is preemptible

You can check if an instance is configured to be preemptible using the Cloud Console , the gcloud tool, or the API.


Check if an instance is preemptible by viewing the instance properties.

  1. In the Google Cloud Console, go to the VM instances page.

    Go to VM instances

  2. Select your project and click Continue.

  3. Click the name of the instance that you want to check. This opens the instance details page.

  4. The preemptible status is specified in the Availability policies section of the instance details.


In gcloud compute, use instances describe to get information about an instance, including whether the instance is preemptible.

gcloud compute instances describe [INSTANCE_NAME]

where [INSTANCE_NAME] is the name of the instance.

The response information includes the preemptible status in the scheduling section.

  automaticRestart: false
  onHostMaintenance: TERMINATE
  preemptible: true


To check if an instance is preemptible, use the API to send a GET request to the URI of the instance.


The response information includes the preemptible status under scheduling.

    "kind": "compute#instance",
    "id": "4468501694759003918",
    "creationTimestamp": "2015-04-15T15:40:59.004-07:00",
    "zone": "[PROJECT_ID]/zones/us-central1-f",
    "status": "RUNNING",
    "name": "example-instance",
       "preemptible": true

Alternatively, you can determine if an instance is preemptible from inside the instance itself. Simply check the metadata server for the scheduling/preemptible value in your instance's default instance metadata.

For example, use curl from within your instance to obtain the value for scheduling/preemptible:

curl "" -H "Metadata-Flavor: Google"

If this value is TRUE, the instance is preemptible.

Detecting if an instance was preempted

Determine if an instance was preempted with the Google Cloud Console, the gcloud tool, or the API.


You can check if an instance was preempted by checking the system activity logs.

  1. In the Google Cloud Console, go to the Logs page.

    Go to Logs

  2. Select your project and click Continue.

  3. Add compute.instances.preempted to the filter by label or text search field.

  4. Optionally, you can also enter an instance name if you want to see preemption operations for a specific instance.

  5. Press enter to apply the specified filters. The Cloud Console updates the list of logs to show only the operations where an instance was preempted.

  6. Select an operation in the list to see details about the instance that was preempted.


Use the gcloud compute operations list command with a filter parameter to get a list of preemption events in your project.

gcloud compute operations list \

You can use the filter param to further scope the results. For example, to see preemption events only for instances within a managed instance group:

gcloud compute operations list \
    --filter="operationType=compute.instances.preempted AND targetLink:instances/[BASE_INSTANCE_NAME]"

gcloud returns a response similar to:

NAME                  TYPE                         TARGET                                   HTTP_STATUS STATUS TIMESTAMP
systemevent-xxxxxxxx  compute.instances.preempted  us-central1-f/instances/example-instance-xxx  200         DONE   2015-04-02T12:12:10.881-07:00

An operation type of compute.instances.preempted indicates that the instance was preempted. You can use the operations describe command to get more information about a specific preemption operation.

gcloud compute operations describe \

gcloud returns a response similar to:

operationType: compute.instances.preempted
progress: 100
startTime: '2015-04-02T12:12:10.881-07:00'
status: DONE
statusMessage: Instance was preempted.


To get a list of recent system operations, send a GET request to the URI of zone operations.


The response contains a list of recent operations.

  "kind": "compute#operation",
  "id": "15041793718812375371",
  "name": "systemevent-xxxxxxxx",
  "zone": "[PROJECT_ID]/zones/us-central1-f",
  "operationType": "compute.instances.preempted",
  "targetLink": "[PROJECT_ID]/zones/us-central1-f/instances/example-instance",
  "targetId": "12820389800990687210",
  "status": "DONE",
  "statusMessage": "Instance was preempted.",

To scope the response to show only preemption operations, you can add a filter to your API request: operationType="compute.instances.preempted". To see preemption operations for a specific instance, add a targetLink param to the filter: operationType="compute.instances.preempted" AND targetLink="[PROJECT_ID]/zones/[ZONE]/instances/[INSTANCE_NAME]".

Alternatively, you can determine if an instance was preempted from inside the instance itself. This is useful if you want to handle a shutdown due to a Compute Engine preemption differently from a normal shutdown in a shutdown script. To do this, simply check the metadata server for the preempted value in your instance's default instance metadata.

For example, use curl from within your instance to obtain the value for preempted:

curl "" -H "Metadata-Flavor: Google"

If this value is TRUE, the instance was preempted by Compute Engine, otherwise it is FALSE.

If you want to use this outside of a shutdown script, you can append ?wait_for_change=true to the URL. This performs a hanging HTTP GET request that only returns when the metadata has changed and the instance has been preempted.

curl "" -H "Metadata-Flavor: Google"

Testing preemption settings

You can run simulated maintenance events on your instances to force them to preempt. Use this feature to test how your apps handle preemptible instances. Read testing your availability policies to learn how to test maintenance events on your instances.

You can also simulate an instance preemption by stopping the instance, which can be used instead of simulating a maintenance event and which avoids quota limits.

Best practices

Here are some best practices to help you get the most out of preemptible VM instances.

Using the bulk instance API

Rather than creating single VMs, you can use the bulk instance API.

Pick smaller machine shapes

Resources for preemptible VM instances come out of excess and backup Google Cloud capacity. It's often easier to get lots of preemptible capacity with smaller machine types than larger ones.

You might also get more spare capacity by using a custom machine type that is in between the predefined types. For example, there's likely more capacity for a custom machine type with 48 vCPUs than there are n1-standard-64s.

Run large preemptible VM clusters during off peak times

The load on Google Cloud data centers varies with location and time of day, but generally lowest on nights and weekends. As such, nights and weekends are the best times to run large preemptible VM clusters.

Design your applications to be fault and preemption tolerant

It's important to be prepared for the fact that there are changes in preemption patterns at different points in time. For example, if a zone suffers a partial outage, large numbers of preemptible instances could be preempted to make room for regular instances that need to be moved as part of the recovery. In that small window of time, the preemption rate would look very different than on any other day. If your application assumes that preemptions are always done in small groups, you might not be prepared for such an event. You can test your application's behavior under a preemption event by stopping the VM instance.

Retry creating instances that have been preempted

If your VM instance been preempted, try creating new preemptible instances once or twice before falling back to regular instances. Depending on your requirements, it might be a good idea to combine regular and preemptible instances in your clusters to ensure that work proceeds at an adequate pace.

Use shutdown scripts

Manage shutdown and preemption notices with a shutdown script that can save a job's progress so that it can pick up where it left off, rather than start over from scratch.

What's next?