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, its static external 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 Power Off
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.
Before you begin
- If you want to use the command-line examples in this guide:
- Install or update to the latest version of the gcloud command-line tool.
- Set a default region and zone.
- If you want to use the API examples in this guide, set up API access.
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 extra 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 extra precautions if you shut down the guest operating systems on your instances.
Billing
Google does not bill for VM resources after the VMs are in a TERMINATED
state.
Specifically, when a VM is RUNNING
and is in the process of being
stopped (but is not yet TERMINATED
), Google bills for vCPUs, memory, GPUs,
and local SSD usage. After the STOPPING
state ends and a VM enters the
TERMINATED
state, then Google stops billing for the above-mentioned resources.
Keep in mind that other resources that are attached to a TERMINATED
VM
continue to incur charges, for example, persistent disks and static external
IP addresses.
For more information about instance states, read the Instance life cycle documentation.
Stopping an instance
To stop an instance, use the Google Cloud Console, the
gcloud
tool, or the
API.
Console
- In the Google Cloud Console, go to the VM instances page.
- Select one or more instances that you want to stop.
- 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/zone/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 still 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.
Linux
Use the sudo shutdown -h now
or sudo poweroff
commands. Execute one of
these commands while you are logged into the virtual machine:
sudo shutdown -h now
sudo poweroff
Windows
To stop a VM instance based on the Windows OS, on the Start menu, click the arrow next to Log off, and then click Shut down.
Starting a stopped instance
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. Most instances can be restarted, as long as
the instance is in a TERMINATED
state, with the exception of instances with
local SSDs attached, which cannot be restarted.
Console
- In the Google Cloud Console, go to the VM Instances page.
- Select the instances that you want to restart.
- Click the Start button.
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:
POST https://compute.googleapis.com/compute/v1/projects/myproject/zones/zone/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="zone", instance="example-instance") response = request.execute(auth_http) print response
For more information about this method, see the
instances().start
reference documentation.
Starting a stopped 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
- In the Google Cloud Console, go to the VM instances page.
- Click the name of the instance that you want to start. This opens the instance details page.
- Click Start. A window opens where you can specify encryption keys for the devices that are attached to this instance.
- Specify encryption keys for each of the encrypted disks that are attached to this instance.
- Click Start to start the instance.
gcloud
Provide the key using the --csek-key-file
flag and the name of the disk
when you start the instance. 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 using 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
: Your project ID.zone
: The zone for this instance.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.disk-name
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.
When you reset an instance, none of the instance properties change. The instance retains properties like its ephemeral IP address, its static external IP address, its persistent disk, and its machine type. However, any data stored in memory is reset. To keep any data stored in memory, you must back up the data.
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
- In the Google Cloud Console, go to the VM instances page.
- Check the boxes next to one or more instances that you want to reset.
- Click the Reset button to reset the instances.
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:
POST https://compute.googleapis.com/compute/v1/projects/myproject/zones/zone/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="zone", instance="example-instance") response = request.execute(auth_http) print response
For more information about this method, see the
instances().reset
reference documentation.
After running the reset
command, the subsequent
zone operation returns
as DONE
after the instance has completely shut down and started to reboot.
Using other restart methods
Alternatively, you can reset your instance using the following commands:
sudo reboot
(Linux only): Called from within the instance. Wipes the memory and re-initializes the instance with the original metadata, image, and persistent disks.sudo reboot
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.gcloud compute instances delete
followed bygcloud compute instances create
: This is a completely destructive restart that initializes the instance with any information passed intogcloud 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
- Use the interactive serial console to troubleshoot your instance.
- Learn how to change the machine type.