This document explains how to consume reservations in Compute Engine. To learn how to consume reservations in other Google Cloud products, see the following documentation:
- Batch
- Dataflow
- Dataproc
- Google Kubernetes Engine
- Vertex AI:
After you create a reservation, or Compute Engine automatically creates a reservation for you to fulfill a future reservation, Compute Engine holds your reserved resources for you. You can then use those reserved resources to create Compute Engine instances that match the reservation's properties. This action is known as consuming a reservation. You can use your reserved capacity for creating instances until the reservation is fully consumed.
Limitations
You can't consume a reservation to create the following Compute Engine resources:
Spot VMs or preemptible instances
Sole-tenant nodes
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
-
After installing the Google Cloud CLI, initialize it by running the following command:
gcloud init
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
- 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.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
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.
If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.
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.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
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.
If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.
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.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
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.
If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.
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.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
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.
If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.
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.
After installing the Google Cloud CLI, initialize it by running the following command:
gcloud init
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
For more information, see Authenticate for using REST in the Google Cloud authentication documentation.
-
Required roles
To get the permissions that
you need to consume 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 consume reservations. To see the exact permissions that are required, expand the Required permissions section:
Required permissions
The following permissions are required to consume 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.
Consume a reservation
The examples in the following sections show how to consume a reservation by creating a single compute instance. You can also consume reservations by creating instances that match the reservations' properties using a different deployment option, or by updating the properties of existing instances to match automatically consumed reservations.
To consume a reservation, use one of the following methods:
To create an instance that consumes an automatic reservation, regardless of how that reservation was created, see Consume an automatically consumed reservation.
To create an instance that consumes a specific reservation that you created, see Consume a specifically targeted reservation.
Consume an automatically consumed reservation
When you create an automatically consumed reservation, compute instances that match the reservation's properties automatically consume it. This consumption behavior applies to both new and existing, running instances. When you create reservations, or Compute Engine automatically creates a reservation to fulfill a future reservation, this reservation type is the default setting.
If the properties of a single-project, automatic reservation and a shared, automatic reservation match, then the instances in your project consume the single-project reservation first, and then they consume the shared reservation. For more information, see the consumption order for reservations.
To create and consume an example automatic reservation, select one of the following options:
Console
The following example shows how to create an automatic reservation in zone
us-central1-a
for three N2 instances with 32 vCPUs, and Intel Cascade Lake
as the minimum CPU platform. It also shows how to create a single instance
to consume the reservation.
To create the example automatic reservation and consume it, do the following:
To create an example reservation, complete the following steps:
In the Google Cloud console, go to the Reservations page.
On the On-demand reservation tab (default), click
Create reservation. The Create a reservation page appears.In the Name field, enter a name for the reservation. For example, enter
reservation-01
.Select the Region and Zone where to reserve resources. For example, select us-central1 and us-central1-a respectively.
In the Share type section, do one of the following:
To create a single-project reservation, select Local.
To create a shared reservation, select Shared, and then specify the projects that you want to share the reservation with.
In the Use with VM instance section, select Use reservation automatically, if it isn't already selected.
In the Number of VM instances field, enter
3
.On the General purpose tab, select N2.
In the Machine type section, on the Preset tab (default), select n2-standard-32.
Expand CPU platform and GPU, and then, in the CPU platform field, select Intel Cascade Lake or later.
Click Create.
To create an instance that consumes the example reservation, complete the following steps:
In the Google Cloud console, go to the Create an instance page.
The Create an instance page appears and displays the Machine configuration pane.
In the Machine configuration pane, do the following:
In the Name field, enter a name for the instance. For this example, enter
instance-01
.Specify the Region and Zone where to reserve resources. For this example, select us-central1 and us-central1-a respectively.
On the General purpose tab, select N2.
In the Machine type section, on the Preset tab (default), select n2-standard-32.
Expand the Advanced options section, and then, in the CPU platform field, select Intel Cascade Lake or later.
In the navigation menu, click Advanced.
In the Reservations section of the Advanced pane, select Use automatic selection, if it isn't already selected.
Click Create.
gcloud
The following example shows how to create an automatic reservation in zone
us-central1-a
for three N2 instances with 32 vCPUs, and Intel Cascade Lake
as the minimum CPU platform. It also shows how to create a single instance
to consume the reservation.
To create the example automatic reservation and consume it, do the following:
To create the example reservation, use the
gcloud compute reservations create
command:gcloud compute reservations create reservation-01 \ --machine-type=n2-standard-32 \ --min-cpu-platform="Intel Cascade Lake" \ --vm-count=3 \ --zone=us-central1-a
To create an instance that consumes the example reservation, use the
gcloud compute instances create
command with the--reservation-affinity
flag set toany
. Becauseany
is the default configuration, you can also omit this flag.gcloud compute instances create instance-01 \ --machine-type=n2-standard-32 \ --min-cpu-platform="Intel Cascade Lake" \ --reservation-affinity=any \ --zone=us-central1-a
Go
To create the example automatic reservation using an instance template, and create an instance to consume the reservation using the same template, use the following code sample:
Java
The following example shows how to create an N1 instance with four vCPUs,
and Intel Skylake as the minimum CPU platform in zone us-central1-a
. The
instance automatically consumes a matching reservation.
To create the example instance, use the following code sample:
Node.js
The following example shows how to create an N1 instance with four vCPUs,
and Intel Skylake as the minimum CPU platform in zone us-central1-a
. The
instance automatically consumes a matching reservation.
To create the example instance, use the following code sample:
Python
The following example shows how to create an automatic reservation in zone
us-central1-a
for three N1 instances with one vCPU, and Intel Ivy as the
minimum CPU platform. It also shows how to create a single instance to
consume the reservation.
To create the example automatic reservation and consume it, do the following:
REST
The following example shows how to create an automatic reservation in zone
us-central1-a
for three N2 instances with 32 vCPUs, and Intel Cascade Lake
as the minimum CPU platform. It also shows how to create a single instance
to consume the reservation.
To create the example automatic reservation and consume it, do the following:
To create the example reservation, make a
POST
request toreservations.insert
method:POST https://compute.googleapis.com/compute/v1/projects/example-project/zones/us-central1-a/reservations { "name": "reservation-01", "specificReservation": { "count": "3", "instanceProperties": { "machineType": "n2-standard-32", "minCpuPlatform": "Intel Cascade Lake", } } }
To create an instance that consumes the example reservation, make a
POST
request to theinstances.insert
method. In the request body, include theconsumeReservationType
field set toANY_RESERVATION
. However, becauseANY_RESERVATION
is the default configuration, you can also omit the field.POST https://compute.googleapis.com/compute/v1/projects/example-project/zones/us-central1-a/instances { "name": "instance-01", "machineType": "zones/us-central1-a/machineTypes/n2-standard-32", "minCpuPlatform": "Intel Cascade Lake", "disks": [ { "boot": true, "initializeParams": { "sourceImage": "projects/debian-cloud/global/images/family/debian-12" } } ], "networkInterfaces": [ { "network": "global/networks/default" } ], "reservationAffinity": { "consumeReservationType": "ANY_RESERVATION" } }
Consume a specifically targeted reservation
Specifically targeted reservations allow new compute instances to consume a matching reservation only if the instances target the reservation. Instances are successfully created only if their properties match the reservation properties. Otherwise, you encounter errors.
Based on the creation method that you want to use, create specific reservations and instances that target them as follows:
Creation method | When you create a reservation | When you create instances |
---|---|---|
Google Cloud console | In the Use with VM instance section, select Select specific reservation. | In the Advanced pane, in the Reservations section, select Choose a reservation. |
Google Cloud CLI | Include the --require-specific-reservation flag. |
Include the following flags:
|
Go | Include the SpecificReservationRequired field set to
true . |
In the ReservationAffinity field, include the following
fields:
|
Java | Include the setSpecificReservationRequired field set to
true . |
In the ReservationAffinity field, include the following
fields:
|
Node.js and REST API | Include the specificReservationRequired field set to
true . |
In the reservationAffinity field, include the following
fields:
|
Python and Terraform | Include the specific_reservation_required field set to
true . |
In the reservation_affinity field, include the following
fields:
|
To create an example specific reservation and an instance to consume it, select one of the following options:
Console
The following example shows how to create a specific reservation in zone
us-central1-a
for three N2 instances with 32 vCPUs, and Intel Cascade Lake
as the minimum CPU platform. It also shows how to create a single instance
to consume the reservation.
To create the example specific reservation and consume it, do the following:
To create an example reservation, complete the following steps:
In the Google Cloud console, go to the Reservations page.
On the On-demand reservation tab (default), click
Create reservation. The Create a reservation page appears.In the Name field, enter a name for the reservation. For example, enter
reservation-02
.Specify the Region and Zone where to reserve resources. For this example, select us-central1 and us-central1-a respectively.
In the Share type section, do one of the following:
To create a single-project reservation, select Local.
To create a shared reservation, select Shared, and then specify the projects that you want to share the reservation with.
In the Use with VM instance section, select Select specific reservation.
In the Number of VM instances field, enter
3
.On the General purpose tab, select N2.
In the Machine type section, on the Preset tab (default), select n2-standard-32.
Expand CPU platform and GPU, and then, in the CPU platform field, select Intel Cascade Lake or later.
Click Create.
To create an instance that consumes the example reservation, complete the following steps:
In the Google Cloud console, go to the Create an instance page.
The Create an instance page appears and displays the Machine configuration pane.
In the Machine configuration pane, do the following:
In the Name field, enter a name for the instance. For this example, enter
instance-02
.Specify the Region and Zone where to reserve resources. For this example, select us-central1 and us-central1-a respectively.
On the General purpose tab, select N2.
In the Machine type section, on the Preset tab (default), select n2-standard-32.
Expand the Advanced options section, and then, in the CPU platform field, select Intel Cascade Lake or later.
In the navigation menu, click Advanced.
In the Reservations section of the Advanced pane, select Choose a reservation, and then click Choose reservation.
On the Choose a reservation pane that appears, do the following:
Select the specific reservation that you created in the previous steps. If you want to consume a shared reservation that exists in a different project, then, in the Project list, select the project in which the reservation exists.
Click Choose.
Click Create.
gcloud
The following example shows how to create a specific reservation in zone
us-central1-a
for three N2 instances with 32 vCPUs, and Intel Cascade Lake
as the minimum CPU platform. It also shows how to create a single instance
to consume the reservation.
To create the example specific reservation and consume it, do the following:
To create the example reservation, use the
gcloud compute reservations create
command with the--require-specific-reservation
flag:gcloud compute reservations create reservation-02 \ --machine-type=n2-standard-32 \ --min-cpu-platform="Intel Cascade Lake" \ --require-specific-reservation \ --vm-count=3 \ --zone=us-central1-a
To create an instance that consumes the example reservation, use the
gcloud compute instances create
command with the--reservation
and--reservation-affinity=specific
flags:gcloud compute instances create instance-02 \ --machine-type=n2-standard-32 \ --min-cpu-platform="Intel Cascade Lake" \ --reservation-affinity=specific \ --reservation=RESERVATION_URL \ --zone=us-central1-a
Replace
RESERVATION_URL
with the URL of the reservation. Specify one of the following values:If you created the reservation in the same project:
reservation-02
If the reservation is in a different project:
projects/PROJECT_ID/reservations/reservation-02
Go
The following examples show how to create an N2 instance with 32 vCPUs, and
Intel Cascade Lake as the minimum CPU platform, in zone us-central1-a
to
consume a specific, matching reservation:
To create the example instance to consume a single-project, specific reservation, use the following code sample:
To create the example instance to consume a shared, specific reservation, use the following code sample:
Java
The following examples show how to create an N1 instance with four vCPUs,
and Intel Skylake as the minimum CPU platform, in zone us-central1-a
to
consume a specific, matching reservation:
To create an example reservation as a single-project reservation, and create an instance to consume it, use the following code sample:
To create an example reservation as a shared reservation, and create an instance to consume it, use the following code sample:
Node.js
The following examples show how to create an N1 instance with 4 vCPUs, and
Intel Skylake as the minimum CPU platform, in zone us-central1-a
to
consume a specific, matching reservation:
Python
The following examples show how to create an N2 instance with 32 vCPUs, and
Intel Cascade Lake as the minimum CPU platform, in zone us-central1-a
to
consume a specific, matching reservation:
To create the example specific reservation and consume it, do the following:
To create and consume the example reservation as a single-project reservation, use the following code sample:
To create and consume the example reservation as a shared reservation, use the following code sample:
REST
The following example shows how to create a specific reservation in zone
us-central1-a
for three N2 instances with 32 vCPUs, and Intel Cascade Lake
as the minimum CPU platform. It also shows how to create a single instance
to consume the reservation.
To create the example specific reservation and consume it, do the following:
To create the example reservation, make a
POST
request to theinstances.insert
method. In the request body, include thespecificReservationRequired
field set totrue
:POST https://compute.googleapis.com/compute/v1/projects/example-project/zones/us-central1-a/reservations { "name": "reservation-02", "specificReservation": { "count": "3", "instanceProperties": { "machineType": "n2-standard-32", "minCpuPlatform": "Intel Cascade Lake", } }, "specificReservationRequired": true }
To create an instance that consumes the example reservation, make a
POST
request to theinstances.insert
method. In the request body, in thereservationAffinity
field, include the following:The
consumeReservationType
field set toSPECIFIC_RESERVATION
.The
key
field set tocompute.googleapis.com/reservation-name
.The
values
field set to the URL of the reservation.
The request is similar to the following:
POST https://compute.googleapis.com/compute/v1/projects/example-project/zones/us-central1-a/instances { "name": "instance-02", "machineType": "zones/us-central1-a/machineTypes/n2-standard-32", "minCpuPlatform": "Intel Cascade Lake", "disks": [ { "boot": true, "initializeParams": { "sourceImage": "projects/debian-cloud/global/images/family/debian-12" } } ], "networkInterfaces": [ { "network": "global/networks/default" } ], "reservationAffinity": { "consumeReservationType": "SPECIFIC_RESERVATION", "key": "compute.googleapis.com/reservation-name", "values": [ "RESERVATION_URL" ] } }
Replace
RESERVATION_URL
with the URL of the reservation. Specify one of the following values:If you created the reservation in the same project:
reservation-02
If the reservation is in a different project:
projects/PROJECT_ID/reservations/reservation-02
Test that instance properties match an automatically consumed reservation
To test if a compute instance's properties match an automatically consumed reservation, do the following:
Create a copy of the reservation as a specifically targeted reservation for a single instance.
Create a test instance to consume the reservation.
If you can create the test instance, then its properties match the properties of the test reservation. Otherwise, creating the instance fails.
After you confirm that the properties of your test instance and test reservation match, delete the reservation and the test instance.
Verify reservations consumption
To verify reservations consumption, you can do one or more of the following:
To view the current number of compute instances that are consuming your reservations, and how many more instances can consume them, view reservations.
To monitor reservations consumption data updated every 30 minutes, and receive alerts when reservations are consumed or unconsumed, monitor reservations consumption.
To view reservations consumption data updated every 24 hours, do one of the following:
- To receive consumption reports in a Cloud Storage bucket to analyze consumption trends and identify unconsumed reservations, view reservations usage report.
- To view past and forecasted reservations consumption to analyze consumption trends and plan for future capacity needs, use Capacity Planner.