Stopping and starting an instance


This page describes how to stop and start a VM instance. To suspend and resume an instance, read Suspending and resuming an instance. For more information about stopping and suspending an instance, see Instance life cycle.

You can stop an instance temporarily if you no longer need it and restart it at a later time. A stopped instance retains its persistent disks, its internal IPs, and its MAC addresses. However, the instance shuts down the guest OS and loses its application state. Essentially, a stopped instance resets to its power-on state and no data is saved. Stop an instance if you want to change the machine type, add or remove attached disks, change the minimum CPU platform, add or remove GPUs, or apply sizing recommendations.

Stopping an instance causes Compute Engine to send the ACPI shutdown signal to the instance. Modern guest operating systems are configured to perform a clean shutdown before powering off in response to the power off signal. Compute Engine waits a short time for the guest to finish shutting down and then transitions the instance to the TERMINATED state.

A stopped instance does not incur charges, but all of the resources that are attached to the instance continue to incur charges. For example, you are charged for persistent disks and external IP addresses according to the price sheet, even if an instance is stopped. To stop being charged for attached resources, you can reconfigure a stopped instance to not use those resources, and then delete the resources.

If you need to retain the guest OS and application state, suspend the instance instead.

Before you begin

Restrictions

You cannot stop an instance with a local SSD attached. Compute Engine does not prevent you from shutting down an instance from inside the guest operating system if the instance has a local SSD, so take precautions.

Local SSDs

You cannot stop an instance that has a local SSD attached. Instead, you must migrate your critical data off of the local SSD to a persistent disk or to another instance before you delete the instance completely. Compute Engine does not prevent you from shutting down the guest operating system on an instance with a local SSD, so take precautions.

Billing

Instances that are in a TERMINATED state are not charged for per-second usage and do not count toward your regional CPU quota, so you can choose to stop instances that you are not using, saving you from being charged for instances that aren't active. After you are ready, you can come back and start the same instances again, with the same instance properties, metadata, and resources.

Your instances are not charged for per-second usage while in the TERMINATED state but any resources attached to the virtual machine, such as static IPs and persistent disks, are charged until they are deleted.

Stopping an instance

To stop an instance, use the Google Cloud Console, the gcloud tool, or the Compute Engine API.

Console

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

    Go to the VM instances page

  2. Select one or more instances that you want to stop.

  3. Click Stop.

gcloud

Use the instances stop command and specify one or more instances that you want to stop.

gcloud compute instances stop example-instance-1 example-instance-2

API

In the API, construct a POST request to stop an instance.

POST https://compute.googleapis.com/compute/v1/projects/myproject/zones/us-central1-f/instances/example-instance/stop

A TERMINATED instance still exists with its configuration settings and instance metadata, but it loses its in-memory data and virtual machine state. Any resources that are attached to the terminated instance remain attached until you manually detach those resources or delete the instance.

After the instance is in the TERMINATED state, you can restart the instance or delete it. You can also leave an instance in a TERMINATED state indefinitely. However, if you do not plan to restart the instance, delete it instead.

Stopping an instance through the OS

Optionally, you can stop an instance through the guest operating system by using the sudo shutdown -h now or sudo poweroff command. Run one of these commands while you are logged into the virtual machine:

me@example-instance:~$ sudo shutdown -h now
me@example-instance:~$ sudo poweroff

Restarting a stopped instance that doesn't have an encrypted disk

To start a stopped instance, use the instances().start method. This boots up a stopped virtual machine instance that is currently in the TERMINATED state.

The start method restarts an instance in a TERMINATED state, whereas methods such as reset() and sudo reboot only work on instances that are currently running. Almost all instances can be restarted, as long as the instance is in a TERMINATED state.

Console

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

    Go to the VM instances page

  2. Select the boxes next to one or more instances to start.

  3. Click Start.

gcloud

To reset your instance using gcloud compute:

gcloud compute instances start example-instance

API

In the API, make a POST request to the following URI, replacing the project, zone, and instance name appropriately:

https://www.googleapis.com/compute/v1/projects/myproject/zones/us-central1-f/instances/example-instance/start

To restart your instance using the client libraries, construct a request to the instances().start method:

def restartInstance(auth_http, gce_service):
  request = gce_service.instances().start(project="myproject", zone="us-central1-a", instance="example-instance")
  response = request.execute(auth_http)

  print response

For more information about this method, see the instances().start reference documentation.

Restarting an instance that has encrypted disks

If the instance you want to restart uses customer-supplied encryption keys, you must provide those keys when trying to restart the instance.

Console

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

    Go to the VM Instances page

  2. Click the name of the instance that you want to start. This opens the instance details page.

  3. Click the Start button. A window opens where you can specify encryption keys for the devices that are attached to this instance.

  4. Specify encryption keys for each of the encrypted disks that are attached to this instance.

  5. Click Start to start the instance.

gcloud

When you start the instance, provide the key using the --csek-key-file flag and the name of the disk. If you are using an RSA-wrapped key, use the gcloud beta component:

 gcloud compute instances start INSTANCE_NAME \
     --csek-key-file ENCRYPTION_KEY

Replace the following:

  • INSTANCE_NAME: the name of the instance
  • ENCRYPTION_KEY: the encryption key that you use to encrypt persistent disks that are attached to the instance

API

In the API, construct a POST request to start the instance that uses an encryption key. If you are using an RSA-wrapped key, make the request to the beta API instead of the v1 API.

POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instances/INSTANCE_NAME/startWithEncryptionKey
{
  "instanceEncryptionKey": {
    "rsaEncryptedKey": "ENCRYPTION_KEY
  },
  "disk": [
    {
       "source": "DISK_NAME",
       "diskEncryptionKey": {
         "rsaEncryptedKey": "ENCRYPTION_KEY"
       }
    }
  ]
}

Replace the following:

  • PROJECT_ID is your project ID
  • ZONE is the zone for this instance
  • INSTANCE_NAME is the name of the instance
  • ENCRYPTION_KEY is the encryption key that you use to encrypt persistent disks that are attached to the instance
  • DISK_NAME is the attached disk that is encrypted with a customer-supplied encryption key

Resetting an instance

Performing a reset on your instance is similar to doing a hard reset on your computer where you might press a reset button or press and hold the power button. Resetting an instance forcibly wipes the memory contents of the machine and resets the virtual machine to its initial state. The instance does not perform a clean shutdown of the guest OS. Throughout this process, the instance remains in RUNNING state.

You can perform a reset on a running instance by using the Reset button in the Cloud Console, the instances reset command in gcloud, or by making a POST request in the API.

Console

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

    Go to the VM instances page

  2. Select one or more instances to reset.

  3. Click Reset.

gcloud

To reset your instance using gcloud compute:

gcloud compute instances reset example-instance

API

In the API, make a POST request to the following URI, replacing the project, zone, and instance name appropriately:

https://www.googleapis.com/compute/v1/projects/myproject/zones/us-central1-f/instances/example-instance/reset

To reset your instance using the client libraries, construct a request to the instances().reset method:

def resetInstance(auth_http, gce_service):
  request = gce_service.instances().reset(project="myproject", zone="us-central1-a", instance="example-instance")

  print response

For more information about this method, see the instances().reset reference documentation.

After running the reset command, the subsequent zone operation returns DONE after the instance has completely shut down.

Using other restart methods

You can also choose to reset your instance using the following methods as well:

  • sudo reboot (Linux only): Call this method from within the instance. This method wipes the memory and re-initializes the instance with the original metadata, image, and persistent disks. This command does not pick up any updated versions of the image, and the instance retains the same ephemeral IP address. This is similar to restarting your computer.
  • Rebooting a Windows instance: You can reboot a Windows instance, similar to sudo reboot above, by using the Start menu. In the Start menu, click the arrow next to Log off, and click Restart.
  • gcloud compute instances delete followed by gcloud compute instances create: This is a completely destructive restart that initializes the instance with any information passed into gcloud compute instances create. You can then select any new images or other resources you'd like to use. The restarted instance will probably have a different IP address. This method potentially swaps the physical machine hosting the instance.

What's next