This document explains how to enable graceful shutdown in a new or existing Compute Engine instance.
When you enable graceful shutdown in a compute instance, you can give its guest OS up to six hours to cleanly shut down when you stop or delete the compute instance. This action helps prevent data loss or corrupted file systems when your applications need more time to finish running tasks. To learn more about the requirements and limitations that you apply when you gracefully shutdown a compute instance, see About graceful shutdown.
Before you begin
-
If you haven't already, set up authentication.
Authentication verifies your identity for access to Cloud de Confiance by S3NS 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 Cloud de Confiance console to access Cloud de Confiance by S3NS services and APIs, you don't need to set up authentication.
gcloud
-
Install the Google Cloud CLI, and then sign in to the gcloud CLI with your federated identity. After signing in, initialize the Google Cloud CLI by running the following command:
gcloud init - Set a default region and zone.
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, and then sign in to the gcloud CLI with your federated identity.
For more information, see Authenticate for using REST in the Cloud de Confiance authentication documentation.
-
Required roles
To get the permissions that
you need to enable graceful shutdown in a compute instance,
ask your administrator to grant you the
Compute Instance Admin (v1) (roles/compute.instanceAdmin.v1) IAM role on your project.
For more information about granting roles, see Manage access to projects, folders, and organizations.
This predefined role contains the permissions required to enable graceful shutdown in a compute instance. To see the exact permissions that are required, expand the Required permissions section:
Required permissions
The following permissions are required to enable graceful shutdown in a compute instance:
-
To create compute instances:
compute.instances.createon the project- To use a custom image to create the VM:
compute.images.useReadOnlyon the image - To use a snapshot to create the VM:
compute.snapshots.useReadOnlyon the snapshot - To use an instance template to create the VM:
compute.instanceTemplates.useReadOnlyon the instance template - To assign a legacy network to the VM:
compute.networks.useon the project - To specify a static IP address for the VM:
compute.addresses.useon the project - To assign an external IP address to the VM when using a legacy network:
compute.networks.useExternalIpon the project - To specify a subnet for the VM:
compute.subnetworks.useon the project or on the chosen subnet - To assign an external IP address to the VM when using a VPC network:
compute.subnetworks.useExternalIpon the project or on the chosen subnet - To set VM instance metadata for the VM:
compute.instances.setMetadataon the project - To set tags for the VM:
compute.instances.setTagson the VM - To set labels for the VM:
compute.instances.setLabelson the VM - To set a service account for the VM to use:
compute.instances.setServiceAccounton the VM - To create a new disk for the VM:
compute.disks.createon the project - To attach an existing disk in read-only or read-write mode:
compute.disks.useon the disk - To attach an existing disk in read-only mode:
compute.disks.useReadOnlyon the disk
-
To create an instance template:
compute.instanceTemplates.createon the project -
To enable graceful shutdown in an existing compute instance:
compute.instances.updateon the compute instance
You might also be able to get these permissions with custom roles or other predefined roles.
Enable graceful shutdown
To enable graceful shutdown in a compute instance, use one of the following methods:
Enable graceful shutdown in an existing compute instance
If you enable graceful shutdown in an existing compute instance, then you must restart the compute instance as described in this section.
To enable graceful shutdown in an existing compute instance, select one of the following options:
Console
In the Cloud de Confiance console, go to the VM instances page.
In the Name column, click the name of the compute instance in which you want to enable graceful shutdown. The details page of the compute instance opens.
Click Edit.
In the Management section, do the following:
Select the Gracefully shut down the VM checkbox.
In the Maximum duration field that appears, enter the number of minutes or seconds that you want your graceful shutdown to last. The value must be between one second and 360 minutes (six hours).
Click Save.
gcloud
To enable graceful shutdown in an existing compute instance, use the
gcloud compute instances update command
with the --graceful-shutdown and --graceful-shutdown-max-duration flags:
gcloud compute instances update INSTANCE_NAME \
--graceful-shutdown \
--graceful-shutdown-max-duration=MAX_DURATION \
--zone=ZONE
Replace the following:
INSTANCE_NAME: the name of the compute instance.MAX_DURATION: the maximum duration of the graceful shutdown. The value must be formatted as the number of hours, minutes, or seconds followed byh,m, andsrespectively. For example, specify1hfor one hour, or20m10sfor 20 minutes and 10 seconds. The value must be between one second and six hours.ZONE: the zone where the compute instance exists.
REST
Create an empty JSON file.
To view the properties of an existing compute instance, make a
GETrequest to theinstances.getmethod:GET https://compute.s3nsapis.fr/compute/v1/projects/PROJECT_ID/zones/ZONE/instances/INSTANCE_NAMEReplace the following:
PROJECT_ID: the ID of the project where the compute instance exists.ZONE: the zone where the compute instance exists.INSTANCE_NAME: the name of an existing compute instance.
In the empty JSON file that you created in the previous steps, do the following:
Enter the compute instance configuration details from the
GETrequest output.In the
schedulingfield, add thegracefulShutdownfield as follows:{ ... "scheduling": { ... "gracefulShutdown": { "enabled": true, "maxDuration": { "seconds": "MAX_DURATION" } } }, ... }Replace
MAX_DURATIONwith the maximum duration of the graceful shutdown, in seconds. The value must be between1(one second) and21600(21,600 seconds, or six hours).
To update the compute instance and restart it, make a
PUTrequest to theinstances.updatemethod. In the request, do the following:In the request URL, include the
mostDisruptiveAllowedActionquery parameter set toRESTART.In the request body, include the compute instance properties from the JSON file that you created and updated in the previous steps.
The
PUTrequest to update the compute instance is similar to the following:PUT https://compute.s3nsapis.fr/compute/v1/projects/PROJECT_ID/zones/ZONE/instances/INSTANCE_NAME?mostDisruptiveAllowedAction=RESTART { ... "scheduling": { ... "gracefulShutdown": { "enabled": true } }, ... }
For more information about updating the properties of a compute instance, see Update instance properties.
Enable graceful shutdown while creating a compute instance
To create a compute instance that has graceful shutdown enabled, select one of the following options:
Console
In the Cloud de Confiance console, go to the VM instances page.
Click Create instance. The Create an instance page opens.
In the Name field, enter a name for the compute instance.
In the Region and Zone lists, enter the region and zone in which to create your compute instance.
Specify the machine type for your compute instance.
Expand the VM provisioning model advanced settings section, and then do the following:
Select the Gracefully shut down the VM checkbox.
In the Maximum duration field that appears, enter the number of minutes or seconds that you want your graceful shutdown to last. The value must be between one second and 360 minutes (six hours).
Click Create.
gcloud
To create a compute instance that has graceful shutdown enabled, use the
gcloud compute instances create command
with the --graceful-shutdown flag:
gcloud compute instances create INSTANCE_NAME \
--graceful-shutdown \
--graceful-shutdown-max-duration=MAX_DURATION \
--machine-type=MACHINE_TYPE \
--zone=ZONE
Replace the following:
INSTANCE_NAME: the name of the compute instance.MAX_DURATION: the maximum duration of the graceful shutdown. The value must be formatted as the number of hours, minutes, or seconds followed byh,m, andsrespectively. For example, specify1hfor one hour, or20m10sfor 20 minutes and 10 seconds. The value must be between one second and six hours.MACHINE_TYPE: the machine type to use for the compute instance.ZONE: the zone in which to create the compute instance.
REST
To create a compute instance that has graceful shutdown enabled, make a POST
request to the
instances.insert method.
In the request body, include the gracefulShutdown field:
POST https://compute.s3nsapis.fr/compute/v1/projects/PROJECT_ID/zones/ZONE/instances
{
"name": "INSTANCE_NAME",
"machineType": "zones/ZONE/machineTypes/MACHINE_TYPE",
"disks": [
{
"boot": true,
"initializeParams": {
"sourceImage": "projects/IMAGE_PROJECT/global/images/IMAGE"
}
}
],
"networkInterfaces": [
{
"network": "global/networks/default"
}
],
"scheduling": {
"gracefulShutdown": {
"enabled": true,
"maxDuration": {
"seconds": "MAX_DURATION"
}
}
}
}
Replace the following:
PROJECT_ID: the ID of the project in which to create the compute instance.ZONE: the zone in which to create the compute instance.INSTANCE_NAME: the name of the compute instance.MACHINE_TYPE: the machine type for the compute instance.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. This formatting specifies the most recent, non-deprecated OS image. For example, if you specifyfamily/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.
MAX_DURATION: the maximum duration of the graceful shutdown, in seconds. The value must be between1(one second) and21600(21,600 seconds, or six hours).
For more information about creating a compute instance, see Create and start a Compute Engine instance.
Enable graceful shutdown while creating an instance template
When you create an instance template that has graceful shutdown enabled, the default period for graceful shutdown is 10 minutes. Optionally, you can specify a custom period between one second and six hours. All compute instances that you create by using the instance template have graceful shutdown enabled.
To create an instance template that has graceful shutdown enabled, select one of the following options:
Console
In the Cloud de Confiance console, go to the Instance templates page.
Click Create instance template. The Create an instance template page appears.
In the Name field, enter a name for the instance template.
In the Location section, select whether you want to create a regional or global instance template based on your use case:
To reduce cross-region dependency, create a regional instance template by completing the following steps:
Select Regional.
In the Region field, select the region in which to create your instance template.
To use the instance template across regions, select Global.
In the Machine configuration section, specify a machine type.
Expand the VM provisioning model advanced settings section, and then do the following:
Select the Gracefully shut down the VM checkbox.
In the Maximum duration field that appears, enter the number of minutes or seconds that you want your graceful shutdown to last. The value must be between one second and 360 minutes (six hours).
Click Create.
gcloud
To create an instance template that has graceful shutdown enabled, use the
gcloud compute instance-templates create command
with the --graceful-shutdown flag.
For example, to create a regional instance template that has graceful
shutdown enabled, run the following command. 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 \
--graceful-shutdown \
--graceful-shutdown-max-duration=MAX_DURATION
--instance-template-region=REGION \
--machine-type=MACHINE_TYPE
Replace the following:
INSTANCE_TEMPLATE_NAME: the name of the compute instance template.MAX_DURATION: the maximum duration of the graceful shutdown. The value must be formatted as the number of hours, minutes, or seconds followed byh,m, andsrespectively. For example, specify1hfor one hour, or20m10sfor 20 minutes and 10 seconds. The value must be between one second and six hours.REGION: the region in which to create the compute instance template.MACHINE_TYPE: the machine type for the compute instances that you create by using the instance template.
REST
To create an instance template that has graceful shutdown enabled, make a
POST request to one of the following methods:
To create a global instance template:
instanceTemplates.insertmethod.To create a regional instance template:
regionInstanceTemplates.insertmethod.
For example, to create a regional instance template that has graceful
shutdown enabled, make a POST request as follows:
POST https://compute.s3nsapis.fr/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"
}
],
"scheduling": {
"gracefulShutdown": {
"enabled": true,
"maxDuration": {
"seconds": "MAX_DURATION"
}
}
}
}
}
Replace the following:
PROJECT_ID: the ID of the project where to create the instance template.REGION: the region where to create the compute instance template.INSTANCE_TEMPLATE_NAME: the name of the compute instance template.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. This formatting specifies the most recent, non-deprecated OS image. For example, if you specifyfamily/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 for the compute instances that you create by using the instance template.MAX_DURATION: the maximum duration of the graceful shutdown, in seconds. The value must be between1(one second) and21600(21,600 seconds, or six hours).
For more configuration options when creating an instance template, see Create instance templates.