This document explains how to prevent Compute Engine instances from consuming reservations. To learn more about reservations, see Reservations of Compute Engine zonal resources.
Automatically consumed reservations allow instances with properties that match the reservations to automatically consume them. To prevent instances from consuming a reservation, do one of the following:
Configure instances to not consume reservations, as described in this document.
Create or update instances with properties that don't match the reservation.
You can avoid consuming reservations when you want to use your instances for tasks like testing, debugging, or isolated deployments.
Limitations
You can only update an existing instance to not consume reservations if the instance is configured to automatically consume matching reservations.
Before you begin
-
If you haven't already, then set up authentication.
Authentication is
the process by which your identity is verified 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, then initialize it by running the following command:
gcloud init
- Set a default region and zone.
Go
To use the Go 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.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
If you're using a local shell, then create local authentication credentials for your user account:
gcloud auth application-default login
You don't need to do this if you're using Cloud Shell.
For more information, see Set up authentication for a local development environment.
Java
To use the Java 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.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
If you're using a local shell, then create local authentication credentials for your user account:
gcloud auth application-default login
You don't need to do this if you're using Cloud Shell.
For more information, see Set up authentication for a local development environment.
Node.js
To use the Node.js 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.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
If you're using a local shell, then create local authentication credentials for your user account:
gcloud auth application-default login
You don't need to do this if you're using Cloud Shell.
For more information, see Set up authentication for a local development environment.
Python
To use the Python 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.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
If you're using a local shell, then create local authentication credentials for your user account:
gcloud auth application-default login
You don't need to do this if you're using Cloud Shell.
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, then initialize it by running the following command:
gcloud init
For more information, see Authenticate for using REST in the Google Cloud authentication documentation.
-
Required roles
To get the permissions that
you need to prevent a compute instance from consuming reservations,
ask your administrator to grant you the
Compute Instance Admin (v1) (roles/compute.instanceAdmin.v1
) IAM role on the project.
For more information about granting roles, see Manage access to projects, folders, and organizations.
This predefined role contains the permissions required to prevent a compute instance from consuming reservations. To see the exact permissions that are required, expand the Required permissions section:
Required permissions
The following permissions are required to prevent a compute instance from consuming reservations:
-
To create reservations:
compute.reservations.create
on the project -
To create instances:
compute.instances.create
on the project- To use a custom image to create the VM:
compute.images.useReadOnly
on the image - To use a snapshot to create the VM:
compute.snapshots.useReadOnly
on the snapshot - To use an instance template to create the VM:
compute.instanceTemplates.useReadOnly
on the instance template - To assign a legacy network to the VM:
compute.networks.use
on the project - To specify a static IP address for the VM:
compute.addresses.use
on the project - To assign an external IP address to the VM when using a legacy network:
compute.networks.useExternalIp
on the project - To specify a subnet for the VM:
compute.subnetworks.use
on the project or on the chosen subnet - To assign an external IP address to the VM when using a VPC network:
compute.subnetworks.useExternalIp
on the project or on the chosen subnet - To set VM instance metadata for the VM:
compute.instances.setMetadata
on the project - To set tags for the VM:
compute.instances.setTags
on the VM - To set labels for the VM:
compute.instances.setLabels
on the VM - To set a service account for the VM to use:
compute.instances.setServiceAccount
on the VM - To create a new disk for the VM:
compute.disks.create
on the project - To attach an existing disk in read-only or read-write mode:
compute.disks.use
on the disk - To attach an existing disk in read-only mode:
compute.disks.useReadOnly
on the disk
-
To create instance templates:
compute.instanceTemplates.create
on the project
You might also be able to get these permissions with custom roles or other predefined roles.
Prevent reservation consumption
To prevent a compute instance from consuming reservations, set its reservation
affinity (reservationAffinity
) property to not consume reservations. This
property controls if an instance can consume matching reservations, a specific
reservation, or no reservations.
To prevent one or more instances from consuming reservations, use one of the following methods:
Prevent consumption in an existing instance
You can update a running instance so that it no longer automatically consumes reservations. You must restart the instance to make the changes effective, as described in this section.
To prevent an existing instance from consuming reservations, select one of the following options:
gcloud
Create an empty YAML file.
To export the properties of an instance into the YAML file that you've just created, use the
gcloud compute instances export
command:gcloud compute instances export INSTANCE_NAME \ --destination=YAML_FILE \ --zone=ZONE
Replace the following:
INSTANCE_NAME
: the name of the instance.YAML_FILE
: the path to the empty YAML file that you created in the previous step.ZONE
: the zone where the instance exists.
In the YAML configuration file, set the
consumeReservationType
toNO_RESERVATION
:reservationAffinity: consumeReservationType: NO_RESERVATION
To update the instance and restart it, use the
gcloud compute instances update-from-file
command with the--most-disruptive-allowed-action
flag set toRESTART
:gcloud compute instances update-from-file INSTANCE_NAME \ --most-disruptive-allowed-action=RESTART \ --source=YAML_FILE \ --zone=ZONE
Replace the following:
INSTANCE_NAME
: the name of the instance.YAML_FILE
: the path to the YAML file with the configuration data that you modified in the previous step.ZONE
: the zone where the instance exists.
REST
To view the properties of an existing instance, make a
GET
request to theinstances.get
method:GET https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instances/INSTANCE_NAME
Replace the following:
PROJECT_ID
: the ID of the project where you created the instance.ZONE
: the zone where the instance exists.INSTANCE_NAME
: the name of the instance.
Store the output from the
GET
request in a file or text editor. Modify the copied output to change theconsumeReservationType
field toNO_RESERVATION
:{ ... "reservationAffinity": { "consumeReservationType": "NO_RESERVATION" }, ... }
To update the instance and restart it, make a
PUT
request to theinstances.update
method. In the request, do the following:In the request URL, include the
mostDisruptiveAllowedAction
query parameter set toRESTART
.For the request body, use the output from the
GET
request that you edited in a previous step.
The request is similar to the following example:
PUT https://compute.googleapis.com/compute/v1/projects/example-project/zones/us-central-1/instances/instance-01?mostDisruptiveAllowedAction=RESTART { ... "reservationAffinity": { "consumeReservationType": "NO_RESERVATION" }, ... }
For more information about updating an instance, see Update instance properties.
Prevent consumption while creating an instance
To create a compute instance that can't consume reservations, select one of the following options:
Console
In the Google Cloud console, go to the Create an instance page.
In the Name field, enter a name for the instance.
In the Region and Zone lists, select the region and zone in which to create the instance.
Specify the machine type to use for the instance.
In the navigation menu, click Advanced.
In the Reservations section, select Don't use a reservation.
Click Create.
gcloud
To create an instance that can't consume reservations, use the
gcloud compute instances create
command
with the --reservation-affinity
flag set to none
:
gcloud compute instances create INSTANCE_NAME \
--machine-type=MACHINE_TYPE \
--reservation-affinity=none \
--zone=ZONE
Replace the following:
INSTANCE_NAME
: the name of the instance.MACHINE_TYPE
: the machine type to use for the instance.ZONE
: the zone in which to create the instance.
Go
To create an instance that can't consume reservations, use the following code sample:
Java
To create an instance that can't consume reservations, use the following code sample:
Node.js
To create an instance that can't consume reservations, use the following code sample:
Python
To create an instance that can't consume reservations, use the following code sample:
REST
To create an instance that can't consume reservations, make a POST
request
to the
instances.insert
method.
In the request body, include the consumeReservationType
field set to
NO_RESERVATION
:
POST https://compute.googleapis.com/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"
}
],
"reservationAffinity": {
"consumeReservationType": "NO_RESERVATION"
}
}
Replace the following:
PROJECT_ID
: the ID of the project where you want to create the instance.ZONE
: the zone in which to create the instance.INSTANCE_NAME
: the name of the instance.MACHINE_TYPE
: the machine type to use for the instance.IMAGE_PROJECT
: the image project that contains the OS 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 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.
For more information about creating an instance, see Create and start a Compute Engine instance.
Prevent consumption while creating instances in bulk
To create compute instances in bulk that can't consume reservations, select one of the following options:
gcloud
To create instances in bulk that can't consume reservations, use the
gcloud compute instances bulk create
command
with the --reservation-affinity
flag set to none
.
For example, to create instances in bulk in a single zone and specify a name pattern, run the following command:
gcloud compute instances bulk create \
--count=COUNT \
--machine-type=MACHINE_TYPE \
--name-pattern="NAME_PATTERN" \
--reservation-affinity=none \
--zone=ZONE
Replace the following:
COUNT
: the number of instances to create.MACHINE_TYPE
: the machine type to use for the instances.NAME_PATTERN
: the name pattern for the instances. To replace a sequence of numbers in a instance name, use a sequence of hash (#
) characters. For example, usinginstance-#
for the name pattern generates instances with names starting withinstance-1
,instance-2
, and continuing up to the number of instances specified byCOUNT
.ZONE
: the zone in which to create instances in bulk.
REST
To create instances in bulk that can't consume reservations, make a POST
request to the
instances.bulkInsert
method.
In the request body, include the consumeReservationType
field set to
NO_RESERVATION
.
For example, to create instances in bulk in a single zone and specify a name pattern, make a request as follows:
POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instances/bulkInsert
{
"count": COUNT,
"namePattern": "NAME_PATTERN",
"instanceProperties": {
"machineType": "MACHINE_TYPE",
"disks": [
{
"boot": true,
"initializeParams": {
"sourceImage": "projects/IMAGE_PROJECT/global/images/IMAGE"
}
}
],
"networkInterfaces": [
{
"network": "global/networks/default"
}
],
"reservationAffinity": {
"consumeReservationType": "NO_RESERVATION"
}
}
}
Replace the following:
PROJECT_ID
: the ID of the project in which to create instances in bulk.ZONE
: the zone in which to create instances in bulk.COUNT
: the number of instances to create.NAME_PATTERN
: the name pattern for the instances. To replace a sequence of numbers in a instance name, use a sequence of hash (#
) characters. For example, usinginstance-#
for the name pattern generates instances with names starting withinstance-1
,instance-2
, and continuing up to the number of instances specified byCOUNT
.MACHINE_TYPE
: the machine type to use for the instances.IMAGE_PROJECT
: the image project that contains the OS 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 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.
For more information about creating instances in bulk, see Create VMs in bulk.
Prevent consumption while creating an instance template
After you create an instance template that configures instances to not consume reservations, you can use the template to do the following:
Prevent the compute instances in a managed instance group (MIG) from consuming reservations when you do the following:
To create an instance template that configures instances to not consume reservations, select one of the following options:
Console
In the Google Cloud console, go to the Create an instance template page.
In the Name field, enter a name for the instance template.
In the Location section, specify whether you want to create a regional (default) or global instance template.
In the Machine configuration section, specify the machine type to use for the instances created using the template.
Expand the Advanced options section, and then do the following:
Expand the Management section.
In the Reservations section, select Don't use a reservation.
Click Create.
gcloud
To create an instance template that configures instances to to not consume
reservations, use the
gcloud compute instances-templates create
command
with the --reservation-affinity
flag set to none
.
To create a regional instance template that configures instances to not
consume reservations, 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 \
--instance-template-region=REGION \
--machine-type=MACHINE_TYPE \
--reservation-affinity=none
Replace the following:
INSTANCE_TEMPLATE_NAME
: the name of the instance template.REGION
: the region in which to create the instance template.MACHINE_TYPE
: the machine type to use for the instances created using the instance template.
Go
To create an instance template that configures instances to not consume reservations, use the following code sample:
Java
To create an instance template that configures instances to not consume reservations, use the following code sample:
Node.js
To create an instance template that configures instances to not consume reservations, use the following code sample:
Python
To create an instance template that configures instances to not consume reservations, use the following code sample:
REST
To create an instance template that configures instances to not consume
reservations, make a POST
request to one of the following methods:
To create a global instance template:
instanceTemplates.insert
method.To create a regional instance template:
regionInstanceTemplates.insert
method.
In the request body, include the consumeReservationType
field and set it
to NO_RESERVATION
.
For example, to create a regional instance template and specify to not consume reservations, make a request as follows:
POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/regions/REGION/InstanceTemplates
{
"name": "INSTANCE_TEMPLATE_NAME",
"properties": {
"machineType": "MACHINE_TYPE",
"disks": [
{
"boot": true,
"initializeParams": {
"sourceImage": "projects/IMAGE_PROJECT/global/images/IMAGE"
}
}
],
"networkInterfaces": [
{
"network": "global/networks/default"
}
],
"reservationAffinity": {
"consumeReservationType": "NO_RESERVATION"
}
}
}
Replace the following:
PROJECT_ID
: the ID of the project in which to create the instance template.INSTANCE_TEMPLATE_NAME
: the name of the instance template.MACHINE_TYPE
: the machine type to use for the instances created using the instance template.IMAGE_PROJECT
: the image project that contains the OS 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 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.
For more information about creating instance templates, see Create instance templates.