Prevent compute instances from consuming reservations


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, set up authentication . Authentication verifies your identity 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

    1. Install the Google Cloud CLI. After installation, initialize the Google Cloud CLI 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 .

    2. 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 .

      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 .

      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 .

      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 .

      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.

      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 .

    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 permissionssection:

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

  1. Create an empty YAML file.

  2. 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.

  3. In the YAML configuration file, set the consumeReservationType to NO_RESERVATION :

     reservationAffinity:
      consumeReservationType: NO_RESERVATION 
    
  4. To update the instance and restart it, use the gcloud compute instances update-from-file command with the --most-disruptive-allowed-action flag set to RESTART :

     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

  1. To view the properties of an existing instance, make a GET request to the instances.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.

  2. Store the output from the GET request in a file or text editor. Modify the copied output to change the consumeReservationType field to NO_RESERVATION :

     {
      ...
      "reservationAffinity": {
        "consumeReservationType": "NO_RESERVATION"
      },
      ...
    } 
    
  3. To update the instance and restart it, make a PUT request to the instances.update method . In the request, do the following:

    • In the request URL, include the mostDisruptiveAllowedAction query parameter set to RESTART .

    • 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

  1. In the Google Cloud console, go to the Create an instancepage.

    Go to Create an instance

  2. In the Namefield, enter a name for the instance.

  3. In the Regionand Zonelists, select the region and zone in which to create the instance.

  4. Specify the machine type to use for the instance.

  5. In the navigation menu, click Advanced.

  6. In the Reservationssection, select Don't use a reservation.

  7. 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:

  import 
  
 ( 
  
 "context" 
  
 "fmt" 
  
 "io" 
  
 compute 
  
 "cloud.google.com/go/compute/apiv1" 
  
 computepb 
  
 "cloud.google.com/go/compute/apiv1/computepb" 
  
 "google.golang.org/protobuf/proto" 
 ) 
 // createInstanceNotConsumeReservation creates VM, without consuming reservation 
 func 
  
 createInstanceNotConsumeReservation 
 ( 
 w 
  
 io 
 . 
 Writer 
 , 
  
 projectID 
 , 
  
 zone 
 , 
  
 instanceName 
  
 string 
 ) 
  
 error 
  
 { 
  
 ctx 
  
 := 
  
 context 
 . 
 Background 
 () 
  
 machineType 
  
 := 
  
 fmt 
 . 
 Sprintf 
 ( 
 "zones/%s/machineTypes/%s" 
 , 
  
 zone 
 , 
  
 "n2-standard-32" 
 ) 
  
 sourceImage 
  
 := 
  
 "projects/debian-cloud/global/images/family/debian-12" 
  
 instancesClient 
 , 
  
 err 
  
 := 
  
 compute 
 . 
  NewInstancesRESTClient 
 
 ( 
 ctx 
 ) 
  
 if 
  
 err 
  
 != 
  
 nil 
  
 { 
  
 return 
  
 fmt 
 . 
 Errorf 
 ( 
 "NewInstancesRESTClient: %w" 
 , 
  
 err 
 ) 
  
 } 
  
 defer 
  
 instancesClient 
 . 
 Close 
 () 
  
 req 
  
 := 
  
& computepb 
 . 
 InsertInstanceRequest 
 { 
  
 Project 
 : 
  
 projectID 
 , 
  
 Zone 
 : 
  
 zone 
 , 
  
 InstanceResource 
 : 
  
& computepb 
 . 
 Instance 
 { 
  
 Disks 
 : 
  
 [] 
 * 
 computepb 
 . 
 AttachedDisk 
 { 
  
 { 
  
 InitializeParams 
 : 
  
& computepb 
 . 
 AttachedDiskInitializeParams 
 { 
  
 DiskSizeGb 
 : 
  
 proto 
 . 
 Int64 
 ( 
 10 
 ), 
  
 SourceImage 
 : 
  
 proto 
 . 
 String 
 ( 
 sourceImage 
 ), 
  
 }, 
  
 AutoDelete 
 : 
  
 proto 
 . 
 Bool 
 ( 
 true 
 ), 
  
 Boot 
 : 
  
 proto 
 . 
 Bool 
 ( 
 true 
 ), 
  
 Type 
 : 
  
 proto 
 . 
 String 
 ( 
 computepb 
 . 
  AttachedDisk_PERSISTENT 
 
 . 
 String 
 ()), 
  
 }, 
  
 }, 
  
 MachineType 
 : 
  
 proto 
 . 
 String 
 ( 
 machineType 
 ), 
  
 MinCpuPlatform 
 : 
  
 proto 
 . 
 String 
 ( 
 "Intel Cascade Lake" 
 ), 
  
 Name 
 : 
  
 proto 
 . 
 String 
 ( 
 instanceName 
 ), 
  
 NetworkInterfaces 
 : 
  
 [] 
 * 
 computepb 
 . 
 NetworkInterface 
 { 
  
 { 
  
 Name 
 : 
  
 proto 
 . 
 String 
 ( 
 "global/networks/default" 
 ), 
  
 }, 
  
 }, 
  
 ReservationAffinity 
 : 
  
& computepb 
 . 
 ReservationAffinity 
 { 
  
 ConsumeReservationType 
 : 
  
 proto 
 . 
 String 
 ( 
 "NO_RESERVATION" 
 ), 
  
 }, 
  
 }, 
  
 } 
  
 op 
 , 
  
 err 
  
 := 
  
 instancesClient 
 . 
 Insert 
 ( 
 ctx 
 , 
  
 req 
 ) 
  
 if 
  
 err 
  
 != 
  
 nil 
  
 { 
  
 return 
  
 fmt 
 . 
 Errorf 
 ( 
 "unable to create instance: %w" 
 , 
  
 err 
 ) 
  
 } 
  
 if 
  
 err 
  
 = 
  
 op 
 . 
 Wait 
 ( 
 ctx 
 ); 
  
 err 
  
 != 
  
 nil 
  
 { 
  
 return 
  
 fmt 
 . 
 Errorf 
 ( 
 "unable to wait for the operation: %w" 
 , 
  
 err 
 ) 
  
 } 
  
 fmt 
 . 
 Fprintf 
 ( 
 w 
 , 
  
 "Instance created\n" 
 ) 
  
 return 
  
 nil 
 } 
 

Java

To create an instance that can't consume reservations, use the following code sample:

  import static 
  
 com.google.cloud.compute.v1. ReservationAffinity 
. ConsumeReservationType 
. NO_RESERVATION 
 
 ; 
 import 
  
 com.google.api.gax.longrunning. OperationFuture 
 
 ; 
 import 
  
 com.google.cloud.compute.v1. AttachedDisk 
 
 ; 
 import 
  
 com.google.cloud.compute.v1. AttachedDiskInitializeParams 
 
 ; 
 import 
  
 com.google.cloud.compute.v1. InsertInstanceRequest 
 
 ; 
 import 
  
 com.google.cloud.compute.v1. Instance 
 
 ; 
 import 
  
 com.google.cloud.compute.v1. InstancesClient 
 
 ; 
 import 
  
 com.google.cloud.compute.v1. NetworkInterface 
 
 ; 
 import 
  
 com.google.cloud.compute.v1. Operation 
 
 ; 
 import 
  
 com.google.cloud.compute.v1. ReservationAffinity 
 
 ; 
 import 
  
 java.io.IOException 
 ; 
 import 
  
 java.util.concurrent.ExecutionException 
 ; 
 import 
  
 java.util.concurrent.TimeUnit 
 ; 
 import 
  
 java.util.concurrent.TimeoutException 
 ; 
 public 
  
 class 
 CreateInstanceWithoutConsumingReservation 
  
 { 
  
 public 
  
 static 
  
 void 
  
 main 
 ( 
 String 
 [] 
  
 args 
 ) 
  
 throws 
  
 IOException 
 , 
  
 ExecutionException 
 , 
  
 InterruptedException 
 , 
  
 TimeoutException 
  
 { 
  
 // TODO(developer): Replace these variables before running the sample. 
  
 // Project ID or project number of the Cloud project you want to use. 
  
 String 
  
 projectId 
  
 = 
  
 "YOUR_PROJECT_ID" 
 ; 
  
 // Name of the zone you want to use. 
  
 String 
  
 zone 
  
 = 
  
 "us-central1-a" 
 ; 
  
 // Name of the VM instance you want to query. 
  
 String 
  
 instanceName 
  
 = 
  
 "YOUR_INSTANCE_NAME" 
 ; 
  
 // machineType: machine type of the VM being created. 
  
 // *   This value uses the format zones/{zone}/machineTypes/{type_name}. 
  
 // *   For a list of machine types, see https://cloud.google.com/compute/docs/machine-types 
  
 String 
  
 machineTypeName 
  
 = 
  
 "n1-standard-1" 
 ; 
  
 // sourceImage: path to the operating system image to mount. 
  
 // *   For details about images you can mount, see https://cloud.google.com/compute/docs/images 
  
 String 
  
 sourceImage 
  
 = 
  
 "projects/debian-cloud/global/images/family/debian-11" 
 ; 
  
 // diskSizeGb: storage size of the boot disk to attach to the instance. 
  
 long 
  
 diskSizeGb 
  
 = 
  
 10L 
 ; 
  
 // networkName: network interface to associate with the instance. 
  
 String 
  
 networkName 
  
 = 
  
 "default" 
 ; 
  
 createInstanceWithoutConsumingReservationAsync 
 ( 
 projectId 
 , 
  
 zone 
 , 
  
 instanceName 
 , 
  
 machineTypeName 
 , 
  
 sourceImage 
 , 
  
 diskSizeGb 
 , 
  
 networkName 
 ); 
  
 } 
  
 // Create a virtual machine that explicitly doesn't consume reservations 
  
 public 
  
 static 
  
  Instance 
 
  
 createInstanceWithoutConsumingReservationAsync 
 ( 
  
 String 
  
 project 
 , 
  
 String 
  
 zone 
 , 
  
 String 
  
 instanceName 
 , 
  
 String 
  
 machineTypeName 
 , 
  
 String 
  
 sourceImage 
 , 
  
 long 
  
 diskSizeGb 
 , 
  
 String 
  
 networkName 
 ) 
  
 throws 
  
 IOException 
 , 
  
 InterruptedException 
 , 
  
 ExecutionException 
 , 
  
 TimeoutException 
  
 { 
  
 String 
  
 machineType 
  
 = 
  
 String 
 . 
 format 
 ( 
 "zones/%s/machineTypes/%s" 
 , 
  
 zone 
 , 
  
 machineTypeName 
 ); 
  
 // Initialize client that will be used to send requests. This client only needs to be created 
  
 // once, and can be reused for multiple requests. 
  
 try 
  
 ( 
  InstancesClient 
 
  
 instancesClient 
  
 = 
  
  InstancesClient 
 
 . 
 create 
 ()) 
  
 { 
  
  AttachedDisk 
 
  
 disk 
  
 = 
  
  AttachedDisk 
 
 . 
 newBuilder 
 () 
  
 . 
 setBoot 
 ( 
 true 
 ) 
  
 . 
 setAutoDelete 
 ( 
 true 
 ) 
  
 . 
 setType 
 ( 
  AttachedDisk 
 
 . 
 Type 
 . 
 PERSISTENT 
 . 
 toString 
 ()) 
  
 . 
 setDeviceName 
 ( 
 "disk-1" 
 ) 
  
 . 
  setInitializeParams 
 
 ( 
  
  AttachedDiskInitializeParams 
 
 . 
 newBuilder 
 () 
  
 . 
 setSourceImage 
 ( 
 sourceImage 
 ) 
  
 . 
 setDiskSizeGb 
 ( 
 diskSizeGb 
 ) 
  
 . 
 build 
 ()) 
  
 . 
 build 
 (); 
  
  NetworkInterface 
 
  
 networkInterface 
  
 = 
  
  NetworkInterface 
 
 . 
 newBuilder 
 () 
  
 . 
 setName 
 ( 
 networkName 
 ) 
  
 . 
 build 
 (); 
  
  ReservationAffinity 
 
  
 reservationAffinity 
  
 = 
  
  ReservationAffinity 
 
 . 
 newBuilder 
 () 
  
 . 
  setConsumeReservationType 
 
 ( 
  NO_RESERVATION 
 
 . 
 toString 
 ()) 
  
 . 
 build 
 (); 
  
  Instance 
 
  
 instanceResource 
  
 = 
  
  Instance 
 
 . 
 newBuilder 
 () 
  
 . 
 setName 
 ( 
 instanceName 
 ) 
  
 . 
 setMachineType 
 ( 
 machineType 
 ) 
  
 . 
 addDisks 
 ( 
 disk 
 ) 
  
 . 
 addNetworkInterfaces 
 ( 
 networkInterface 
 ) 
  
 . 
 setReservationAffinity 
 ( 
 reservationAffinity 
 ) 
  
 . 
 build 
 (); 
  
  InsertInstanceRequest 
 
  
 insertInstanceRequest 
  
 = 
  
  InsertInstanceRequest 
 
 . 
 newBuilder 
 () 
  
 . 
 setProject 
 ( 
 project 
 ) 
  
 . 
 setZone 
 ( 
 zone 
 ) 
  
 . 
 setInstanceResource 
 ( 
 instanceResource 
 ) 
  
 . 
 build 
 (); 
  
 OperationFuture<Operation 
 , 
  
 Operation 
>  
 operation 
  
 = 
  
 instancesClient 
 . 
 insertAsync 
 ( 
  
 insertInstanceRequest 
 ); 
  
 // Wait for the operation to complete. 
  
  Operation 
 
  
 response 
  
 = 
  
 operation 
 . 
 get 
 ( 
 3 
 , 
  
 TimeUnit 
 . 
 MINUTES 
 ); 
  
 if 
  
 ( 
 response 
 . 
  hasError 
 
 ()) 
  
 { 
  
 return 
  
 null 
 ; 
  
 } 
  
 return 
  
 instancesClient 
 . 
 get 
 ( 
 project 
 , 
  
 zone 
 , 
  
 instanceName 
 ); 
  
 } 
  
 } 
 } 
 

Node.js

To create an instance that can't consume reservations, use the following code sample:

  // Import the Compute library 
 const 
  
 computeLib 
  
 = 
  
 require 
 ( 
 ' @google-cloud/compute 
' 
 ); 
 const 
  
 compute 
  
 = 
  
 computeLib 
 . 
 protos 
 . 
 google 
 . 
 cloud 
 . 
 compute 
 . 
 v1 
 ; 
 // Instantiate a reservationsClient 
 const 
  
 instancesClient 
  
 = 
  
 new 
  
 computeLib 
 . 
  InstancesClient 
 
 (); 
 // Instantiate a zoneOperationsClient 
 const 
  
 zoneOperationsClient 
  
 = 
  
 new 
  
 computeLib 
 . 
  ZoneOperationsClient 
 
 (); 
 /** 
 * TODO(developer): Update/uncomment these variables before running the sample. 
 */ 
 // The ID of the project where you want to create instance. 
 const 
  
 projectId 
  
 = 
  
 await 
  
 instancesClient 
 . 
 getProjectId 
 (); 
 // The zone in which to create instance. 
 const 
  
 zone 
  
 = 
  
 'us-central1-a' 
 ; 
 // The name of the instance to create. 
 // const instanceName = 'instance-01'; 
 // Machine type to use for VM. 
 const 
  
 machineType 
  
 = 
  
 'n1-standard-4' 
 ; 
 // Create a VM that explicitly doesn't consume reservations 
 async 
  
 function 
  
 callCreateInstanceToNotConsumeReservation 
 () 
  
 { 
  
 // Describe the size and source image of the boot disk to attach to the instance. 
  
 const 
  
 disk 
  
 = 
  
 new 
  
 compute 
 . 
  Disk 
 
 ({ 
  
 boot 
 : 
  
 true 
 , 
  
 autoDelete 
 : 
  
 true 
 , 
  
 type 
 : 
  
 'PERSISTENT' 
 , 
  
 initializeParams 
 : 
  
 { 
  
 diskSizeGb 
 : 
  
 '10' 
 , 
  
 sourceImage 
 : 
  
 'projects/debian-cloud/global/images/family/debian-12' 
 , 
  
 }, 
  
 }); 
  
 //  Define networkInterface 
  
 const 
  
 networkInterface 
  
 = 
  
 new 
  
 compute 
 . 
  NetworkInterface 
 
 ({ 
  
 name 
 : 
  
 'global/networks/default' 
 , 
  
 }); 
  
 // Define reservationAffinity 
  
 const 
  
 reservationAffinity 
  
 = 
  
 new 
  
 compute 
 . 
  ReservationAffinity 
 
 ({ 
  
 consumeReservationType 
 : 
  
 ' NO_RESERVATION 
' 
 , 
  
 }); 
  
 // Create an instance 
  
 const 
  
 instance 
  
 = 
  
 new 
  
 compute 
 . 
  Instance 
 
 ({ 
  
 name 
 : 
  
 instanceName 
 , 
  
 machineType 
 : 
  
 `zones/ 
 ${ 
 zone 
 } 
 /machineTypes/ 
 ${ 
 machineType 
 } 
 ` 
 , 
  
 minCpuPlatform 
 : 
  
 'Intel Skylake' 
 , 
  
 disks 
 : 
  
 [ 
 disk 
 ], 
  
 networkInterfaces 
 : 
  
 [ 
 networkInterface 
 ], 
  
 reservationAffinity 
 , 
  
 }); 
  
 const 
  
 [ 
 response 
 ] 
  
 = 
  
 await 
  
 instancesClient 
 . 
 insert 
 ({ 
  
 project 
 : 
  
 projectId 
 , 
  
 instanceResource 
 : 
  
 instance 
 , 
  
 zone 
 , 
  
 }); 
  
 let 
  
 operation 
  
 = 
  
 response 
 . 
 latestResponse 
 ; 
  
 // Wait for the create instance operation to complete. 
  
 while 
  
 ( 
 operation 
 . 
 status 
  
 !== 
  
 'DONE' 
 ) 
  
 { 
  
 [ 
 operation 
 ] 
  
 = 
  
 await 
  
 zoneOperationsClient 
 . 
 wait 
 ({ 
  
 operation 
 : 
  
 operation 
 . 
 name 
 , 
  
 project 
 : 
  
 projectId 
 , 
  
 zone 
 : 
  
 operation 
 . 
 zone 
 . 
 split 
 ( 
 '/' 
 ). 
 pop 
 (), 
  
 }); 
  
 } 
  
 console 
 . 
 log 
 ( 
 `Instance 
 ${ 
 instanceName 
 } 
 created.` 
 ); 
 } 
 await 
  
 callCreateInstanceToNotConsumeReservation 
 (); 
 

Python

To create an instance that can't consume reservations, use the following code sample:

  from 
  
 __future__ 
  
 import 
 annotations 
 import 
  
 sys 
 from 
  
 typing 
  
 import 
 Any 
 from 
  
 google.api_core.extended_operation 
  
 import 
 ExtendedOperation 
 from 
  
 google.cloud 
  
 import 
  compute_v1 
 
 def 
  
 wait_for_extended_operation 
 ( 
 operation 
 : 
 ExtendedOperation 
 , 
 verbose_name 
 : 
 str 
 = 
 "operation" 
 , 
 timeout 
 : 
 int 
 = 
 300 
 ) 
 - 
> Any 
 : 
  
 """ 
 Waits for the extended (long-running) operation to complete. 
 If the operation is successful, it will return its result. 
 If the operation ends with an error, an exception will be raised. 
 If there were any warnings during the execution of the operation 
 they will be printed to sys.stderr. 
 Args: 
 operation: a long-running operation you want to wait on. 
 verbose_name: (optional) a more verbose name of the operation, 
 used only during error and warning reporting. 
 timeout: how long (in seconds) to wait for operation to finish. 
 If None, wait indefinitely. 
 Returns: 
 Whatever the operation.result() returns. 
 Raises: 
 This method will raise the exception received from `operation.exception()` 
 or RuntimeError if there is no exception set, but there is an `error_code` 
 set for the `operation`. 
 In case of an operation taking longer than `timeout` seconds to complete, 
 a `concurrent.futures.TimeoutError` will be raised. 
 """ 
 result 
 = 
 operation 
 . 
 result 
 ( 
 timeout 
 = 
 timeout 
 ) 
 if 
 operation 
 . 
 error_code 
 : 
 print 
 ( 
 f 
 "Error during 
 { 
 verbose_name 
 } 
 : [Code: 
 { 
 operation 
 . 
 error_code 
 } 
 ]: 
 { 
 operation 
 . 
 error_message 
 } 
 " 
 , 
 file 
 = 
 sys 
 . 
 stderr 
 , 
 flush 
 = 
 True 
 , 
 ) 
 print 
 ( 
 f 
 "Operation ID: 
 { 
 operation 
 . 
 name 
 } 
 " 
 , 
 file 
 = 
 sys 
 . 
 stderr 
 , 
 flush 
 = 
 True 
 ) 
 raise 
 operation 
 . 
 exception 
 () 
 or 
 RuntimeError 
 ( 
 operation 
 . 
 error_message 
 ) 
 if 
 operation 
 . 
 warnings 
 : 
 print 
 ( 
 f 
 "Warnings during 
 { 
 verbose_name 
 } 
 : 
 \n 
 " 
 , 
 file 
 = 
 sys 
 . 
 stderr 
 , 
 flush 
 = 
 True 
 ) 
 for 
 warning 
 in 
 operation 
 . 
 warnings 
 : 
 print 
 ( 
 f 
 " - 
 { 
 warning 
 . 
 code 
 } 
 : 
 { 
 warning 
 . 
 message 
 } 
 " 
 , 
 file 
 = 
 sys 
 . 
 stderr 
 , 
 flush 
 = 
 True 
 ) 
 return 
 result 
 def 
  
 create_vm_not_consume_reservation 
 ( 
 project_id 
 : 
 str 
 , 
 zone 
 : 
 str 
 , 
 instance_name 
 : 
 str 
 , 
 machine_type 
 : 
 str 
 = 
 "n2-standard-2" 
 ) 
 - 
> compute_v1 
 . 
 Instance 
 : 
  
 """Creates a VM that explicitly doesn't consume reservations 
 Args: 
 project_id (str): The ID of the Google Cloud project. 
 zone (str): The zone where the VM will be created. 
 instance_name (str): The name of the instance to create. 
 machine_type (str, optional): The machine type for the instance. 
 Returns: 
 compute_v1.Instance: The created instance. 
 """ 
 instance 
 = 
  compute_v1 
 
 . 
  Instance 
 
 () 
 instance 
 . 
 name 
 = 
 instance_name 
 instance 
 . 
 machine_type 
 = 
 f 
 "zones/ 
 { 
 zone 
 } 
 /machineTypes/ 
 { 
 machine_type 
 } 
 " 
 instance 
 . 
 zone 
 = 
 zone 
 instance 
 . 
  disks 
 
 = 
 [ 
  compute_v1 
 
 . 
  AttachedDisk 
 
 ( 
 boot 
 = 
 True 
 , 
 # Indicates that this is a boot disk 
 auto_delete 
 = 
 True 
 , 
 # The disk will be deleted when the instance is deleted 
 initialize_params 
 = 
  compute_v1 
 
 . 
  AttachedDiskInitializeParams 
 
 ( 
 source_image 
 = 
 "projects/debian-cloud/global/images/family/debian-11" 
 , 
 disk_size_gb 
 = 
 10 
 , 
 ), 
 ) 
 ] 
 instance 
 . 
 network_interfaces 
 = 
 [ 
  compute_v1 
 
 . 
  NetworkInterface 
 
 ( 
 network 
 = 
 "global/networks/default" 
 , 
 # The network to use 
 access_configs 
 = 
 [ 
  compute_v1 
 
 . 
  AccessConfig 
 
 ( 
 name 
 = 
 "External NAT" 
 , 
 # Name of the access configuration 
 type 
 = 
 "ONE_TO_ONE_NAT" 
 , 
 # Type of access configuration 
 ) 
 ], 
 ) 
 ] 
 # Set the reservation affinity to not consume any reservation 
 instance 
 . 
 reservation_affinity 
 = 
  compute_v1 
 
 . 
  ReservationAffinity 
 
 ( 
 consume_reservation_type 
 = 
 "NO_RESERVATION" 
 , 
 # Prevents the instance from consuming reservations 
 ) 
 # Create a request to insert the instance 
 request 
 = 
  compute_v1 
 
 . 
  InsertInstanceRequest 
 
 () 
 request 
 . 
 zone 
 = 
 zone 
 request 
 . 
 project 
 = 
 project_id 
 request 
 . 
 instance_resource 
 = 
 instance 
 vm_client 
 = 
  compute_v1 
 
 . 
  InstancesClient 
 
 () 
 operation 
 = 
 vm_client 
 . 
  insert 
 
 ( 
 request 
 ) 
 wait_for_extended_operation 
 ( 
 operation 
 , 
 "Instance creation" 
 ) 
 print 
 ( 
 f 
 "Creating the 
 { 
 instance_name 
 } 
 instance in 
 { 
 zone 
 } 
 ..." 
 ) 
 return 
 vm_client 
 . 
  get 
 
 ( 
 project 
 = 
 project_id 
 , 
 zone 
 = 
 zone 
 , 
 instance 
 = 
 instance_name 
 ) 
 

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 specify family/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, using instance-# for the name pattern generates instances with names starting with instance-1 , instance-2 , and continuing up to the number of instances specified by COUNT .

  • 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, using instance-# for the name pattern generates instances with names starting with instance-1 , instance-2 , and continuing up to the number of instances specified by COUNT .

  • 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 specify family/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:

To create an instance template that configures instances to not consume reservations, select one of the following options:

Console

  1. In the Google Cloud console, go to the Create an instance templatepage.

    Go to Create an instance template

  2. In the Namefield, enter a name for the instance template.

  3. In the Locationsection, specify whether you want to create a regional (default) or global instance template.

  4. In the Machine configurationsection, specify the machine type to use for the instances created using the template.

  5. Expand the Advanced optionssection, and then do the following:

    1. Expand the Managementsection.

    2. In the Reservationssection, select Don't use a reservation.

  6. 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:

  import 
  
 ( 
  
 "context" 
  
 "fmt" 
  
 "io" 
  
 compute 
  
 "cloud.google.com/go/compute/apiv1" 
  
 computepb 
  
 "cloud.google.com/go/compute/apiv1/computepb" 
  
 "google.golang.org/protobuf/proto" 
 ) 
 // createInstanceNotConsumeReservation creates a new instance template, which won't consume reservations 
 func 
  
 createTemplateNotConsumeReservation 
 ( 
 w 
  
 io 
 . 
 Writer 
 , 
  
 projectID 
 , 
  
 templateName 
  
 string 
 ) 
  
 error 
  
 { 
  
 // projectID := "your_project_id" 
  
 // templateName := "your_template_name" 
  
 ctx 
  
 := 
  
 context 
 . 
 Background 
 () 
  
 instanceTemplatesClient 
 , 
  
 err 
  
 := 
  
 compute 
 . 
  NewInstanceTemplatesRESTClient 
 
 ( 
 ctx 
 ) 
  
 if 
  
 err 
  
 != 
  
 nil 
  
 { 
  
 return 
  
 fmt 
 . 
 Errorf 
 ( 
 "NewInstanceTemplatesRESTClient: %w" 
 , 
  
 err 
 ) 
  
 } 
  
 defer 
  
 instanceTemplatesClient 
 . 
 Close 
 () 
  
 req 
  
 := 
  
& computepb 
 . 
 InsertInstanceTemplateRequest 
 { 
  
 Project 
 : 
  
 projectID 
 , 
  
 InstanceTemplateResource 
 : 
  
& computepb 
 . 
 InstanceTemplate 
 { 
  
 Name 
 : 
  
 proto 
 . 
 String 
 ( 
 templateName 
 ), 
  
 Properties 
 : 
  
& computepb 
 . 
 InstanceProperties 
 { 
  
 // The template describes the size and source image of the boot disk 
  
 // to attach to the instance. 
  
 Disks 
 : 
  
 [] 
 * 
 computepb 
 . 
 AttachedDisk 
 { 
  
 { 
  
 InitializeParams 
 : 
  
& computepb 
 . 
 AttachedDiskInitializeParams 
 { 
  
 DiskSizeGb 
 : 
  
 proto 
 . 
 Int64 
 ( 
 250 
 ), 
  
 SourceImage 
 : 
  
 proto 
 . 
 String 
 ( 
 "projects/debian-cloud/global/images/family/debian-11" 
 ), 
  
 }, 
  
 AutoDelete 
 : 
  
 proto 
 . 
 Bool 
 ( 
 true 
 ), 
  
 Boot 
 : 
  
 proto 
 . 
 Bool 
 ( 
 true 
 ), 
  
 }, 
  
 }, 
  
 MachineType 
 : 
  
 proto 
 . 
 String 
 ( 
 "e2-standard-4" 
 ), 
  
 // The template connects the instance to the `default` network, 
  
 // without specifying a subnetwork. 
  
 NetworkInterfaces 
 : 
  
 [] 
 * 
 computepb 
 . 
 NetworkInterface 
 { 
  
 { 
  
 Name 
 : 
  
 proto 
 . 
 String 
 ( 
 "global/networks/default" 
 ), 
  
 // The template lets the instance use an external IP address. 
  
 AccessConfigs 
 : 
  
 [] 
 * 
 computepb 
 . 
 AccessConfig 
 { 
  
 { 
  
 Name 
 : 
  
 proto 
 . 
 String 
 ( 
 "External NAT" 
 ), 
  
 Type 
 : 
  
 proto 
 . 
 String 
 ( 
 computepb 
 . 
  AccessConfig_ONE_TO_ONE_NAT 
 
 . 
 String 
 ()), 
  
 NetworkTier 
 : 
  
 proto 
 . 
 String 
 ( 
 computepb 
 . 
  AccessConfig_PREMIUM 
 
 . 
 String 
 ()), 
  
 }, 
  
 }, 
  
 }, 
  
 }, 
  
 ReservationAffinity 
 : 
  
& computepb 
 . 
 ReservationAffinity 
 { 
  
 ConsumeReservationType 
 : 
  
 proto 
 . 
 String 
 ( 
 "NO_RESERVATION" 
 ), 
  
 }, 
  
 }, 
  
 }, 
  
 } 
  
 op 
 , 
  
 err 
  
 := 
  
 instanceTemplatesClient 
 . 
 Insert 
 ( 
 ctx 
 , 
  
 req 
 ) 
  
 if 
  
 err 
  
 != 
  
 nil 
  
 { 
  
 return 
  
 fmt 
 . 
 Errorf 
 ( 
 "unable to create instance template: %w" 
 , 
  
 err 
 ) 
  
 } 
  
 if 
  
 err 
  
 = 
  
 op 
 . 
 Wait 
 ( 
 ctx 
 ); 
  
 err 
  
 != 
  
 nil 
  
 { 
  
 return 
  
 fmt 
 . 
 Errorf 
 ( 
 "unable to wait for the operation: %w" 
 , 
  
 err 
 ) 
  
 } 
  
 fmt 
 . 
 Fprintf 
 ( 
 w 
 , 
  
 "Instance template created\n" 
 ) 
  
 return 
  
 nil 
 } 
 

Java

To create an instance template that configures instances to not consume reservations, use the following code sample:

  import static 
  
 com.google.cloud.compute.v1. ReservationAffinity 
. ConsumeReservationType 
. NO_RESERVATION 
 
 ; 
 import 
  
 com.google.cloud.compute.v1. AccessConfig 
 
 ; 
 import 
  
 com.google.cloud.compute.v1. AttachedDisk 
 
 ; 
 import 
  
 com.google.cloud.compute.v1. AttachedDiskInitializeParams 
 
 ; 
 import 
  
 com.google.cloud.compute.v1. InsertInstanceTemplateRequest 
 
 ; 
 import 
  
 com.google.cloud.compute.v1. InstanceProperties 
 
 ; 
 import 
  
 com.google.cloud.compute.v1. InstanceTemplate 
 
 ; 
 import 
  
 com.google.cloud.compute.v1. InstanceTemplatesClient 
 
 ; 
 import 
  
 com.google.cloud.compute.v1. NetworkInterface 
 
 ; 
 import 
  
 com.google.cloud.compute.v1. Operation 
 
 ; 
 import 
  
 com.google.cloud.compute.v1. ReservationAffinity 
 
 ; 
 import 
  
 java.io.IOException 
 ; 
 import 
  
 java.util.concurrent.ExecutionException 
 ; 
 import 
  
 java.util.concurrent.TimeUnit 
 ; 
 import 
  
 java.util.concurrent.TimeoutException 
 ; 
 public 
  
 class 
 CreateTemplateWithoutConsumingReservation 
  
 { 
  
 public 
  
 static 
  
 void 
  
 main 
 ( 
 String 
 [] 
  
 args 
 ) 
  
 throws 
  
 IOException 
 , 
  
 ExecutionException 
 , 
  
 InterruptedException 
 , 
  
 TimeoutException 
  
 { 
  
 // TODO(developer): Replace these variables before running the sample. 
  
 // Project ID or project number of the Cloud project you want to use. 
  
 String 
  
 projectId 
  
 = 
  
 "YOUR_PROJECT_ID" 
 ; 
  
 // Name of the template you want to query. 
  
 String 
  
 templateName 
  
 = 
  
 "YOUR_INSTANCE_TEMPLATE_NAME" 
 ; 
  
 String 
  
 machineType 
  
 = 
  
 "e2-standard-4" 
 ; 
  
 String 
  
 sourceImage 
  
 = 
  
 "projects/debian-cloud/global/images/family/debian-11" 
 ; 
  
 createTemplateWithoutConsumingReservationAsync 
 ( 
  
 projectId 
 , 
  
 templateName 
 , 
  
 machineType 
 , 
  
 sourceImage 
 ); 
  
 } 
  
 // Create a template that explicitly doesn't consume any reservations. 
  
 public 
  
 static 
  
  InstanceTemplate 
 
  
 createTemplateWithoutConsumingReservationAsync 
 ( 
  
 String 
  
 projectId 
 , 
  
 String 
  
 templateName 
 , 
  
 String 
  
 machineType 
 , 
  
 String 
  
 sourceImage 
 ) 
  
 throws 
  
 IOException 
 , 
  
 ExecutionException 
 , 
  
 InterruptedException 
 , 
  
 TimeoutException 
  
 { 
  
 // Initialize client that will be used to send requests. This client only needs to be created 
  
 // once, and can be reused for multiple requests. 
  
 try 
  
 ( 
  InstanceTemplatesClient 
 
  
 instanceTemplatesClient 
  
 = 
  
  InstanceTemplatesClient 
 
 . 
 create 
 ()) 
  
 { 
  
  AttachedDisk 
 
  
 attachedDisk 
  
 = 
  
  AttachedDisk 
 
 . 
 newBuilder 
 () 
  
 . 
  setInitializeParams 
 
 ( 
  AttachedDiskInitializeParams 
 
 . 
 newBuilder 
 () 
  
 . 
 setSourceImage 
 ( 
 sourceImage 
 ) 
  
 . 
 setDiskType 
 ( 
 "pd-balanced" 
 ) 
  
 . 
 setDiskSizeGb 
 ( 
 250 
 ) 
  
 . 
 build 
 ()) 
  
 . 
 setAutoDelete 
 ( 
 true 
 ) 
  
 . 
 setBoot 
 ( 
 true 
 ) 
  
 . 
 build 
 (); 
  
  NetworkInterface 
 
  
 networkInterface 
  
 = 
  
  NetworkInterface 
 
 . 
 newBuilder 
 () 
  
 . 
 setName 
 ( 
 "global/networks/default" 
 ) 
  
 . 
  addAccessConfigs 
 
 ( 
  AccessConfig 
 
 . 
 newBuilder 
 () 
  
 . 
 setName 
 ( 
 "External NAT" 
 ) 
  
 . 
 setType 
 ( 
  AccessConfig 
 
 . 
 Type 
 . 
 ONE_TO_ONE_NAT 
 . 
 toString 
 ()) 
  
 . 
 setNetworkTier 
 ( 
  AccessConfig 
 
 . 
 NetworkTier 
 . 
 PREMIUM 
 . 
 toString 
 ()) 
  
 . 
 build 
 ()) 
  
 . 
 build 
 (); 
  
  ReservationAffinity 
 
  
 reservationAffinity 
  
 = 
  
  ReservationAffinity 
 
 . 
 newBuilder 
 () 
  
 . 
  setConsumeReservationType 
 
 ( 
  NO_RESERVATION 
 
 . 
 toString 
 ()) 
  
 . 
 build 
 (); 
  
  InstanceProperties 
 
  
 instanceProperties 
  
 = 
  
  InstanceProperties 
 
 . 
 newBuilder 
 () 
  
 . 
 addDisks 
 ( 
 attachedDisk 
 ) 
  
 . 
 setMachineType 
 ( 
 machineType 
 ) 
  
 . 
 setReservationAffinity 
 ( 
 reservationAffinity 
 ) 
  
 . 
 addNetworkInterfaces 
 ( 
 networkInterface 
 ) 
  
 . 
 build 
 (); 
  
  InsertInstanceTemplateRequest 
 
  
 insertInstanceTemplateRequest 
  
 = 
  
  InsertInstanceTemplateRequest 
 
  
 . 
 newBuilder 
 () 
  
 . 
 setProject 
 ( 
 projectId 
 ) 
  
 . 
 setInstanceTemplateResource 
 ( 
  InstanceTemplate 
 
 . 
 newBuilder 
 () 
  
 . 
 setName 
 ( 
 templateName 
 ) 
  
 . 
 setProperties 
 ( 
 instanceProperties 
 ) 
  
 . 
 build 
 ()) 
  
 . 
 build 
 (); 
  
  Operation 
 
  
 response 
  
 = 
  
 instanceTemplatesClient 
 . 
 insertAsync 
 ( 
 insertInstanceTemplateRequest 
 ) 
  
 . 
 get 
 ( 
 3 
 , 
  
 TimeUnit 
 . 
 MINUTES 
 ); 
  
 if 
  
 ( 
 response 
 . 
  hasError 
 
 ()) 
  
 { 
  
 return 
  
 null 
 ; 
  
 } 
  
 return 
  
 instanceTemplatesClient 
 . 
 get 
 ( 
 projectId 
 , 
  
 templateName 
 ); 
  
 } 
  
 } 
 } 
 

Node.js

To create an instance template that configures instances to not consume reservations, use the following code sample:

  // Import the Compute library 
 const 
  
 computeLib 
  
 = 
  
 require 
 ( 
 ' @google-cloud/compute 
' 
 ); 
 const 
  
 compute 
  
 = 
  
 computeLib 
 . 
 protos 
 . 
 google 
 . 
 cloud 
 . 
 compute 
 . 
 v1 
 ; 
 // Instantiate an instanceTemplatesClient 
 const 
  
 instanceTemplatesClient 
  
 = 
  
 new 
  
 computeLib 
 . 
  InstanceTemplatesClient 
 
 (); 
 // Instantiate a globalOperationsClient 
 const 
  
 globalOperationsClient 
  
 = 
  
 new 
  
 computeLib 
 . 
  GlobalOperationsClient 
 
 (); 
 /** 
 * TODO(developer): Update/uncomment these variables before running the sample. 
 */ 
 // The ID of the project where you want to create template. 
 const 
  
 projectId 
  
 = 
  
 await 
  
 instanceTemplatesClient 
 . 
 getProjectId 
 (); 
 // The name of the template to create. 
 // const templateName = 'instance-01'; 
 // Create an instance template that creates VMs that don't explicitly consume reservations 
 async 
  
 function 
  
 callCreateTemplateToNotConsumeReservation 
 () 
  
 { 
  
 // Define the boot disk for the instance template 
  
 const 
  
 disk 
  
 = 
  
 new 
  
 compute 
 . 
  AttachedDisk 
 
 ({ 
  
 initializeParams 
 : 
  
 new 
  
 compute 
 . 
  AttachedDiskInitializeParams 
 
 ({ 
  
 sourceImage 
 : 
  
 'projects/debian-cloud/global/images/debian-12-bookworm-v20240815' 
 , 
  
 diskSizeGb 
 : 
  
 '100' 
 , 
  
 diskType 
 : 
  
 'pd-balanced' 
 , 
  
 }), 
  
 autoDelete 
 : 
  
 true 
 , 
  
 boot 
 : 
  
 true 
 , 
  
 type 
 : 
  
 'PERSISTENT' 
 , 
  
 }); 
  
 // Define the network interface for the instance template 
  
 const 
  
 network 
  
 = 
  
 new 
  
 compute 
 . 
  NetworkInterface 
 
 ({ 
  
 network 
 : 
  
 `projects/ 
 ${ 
 projectId 
 } 
 /global/networks/default` 
 , 
  
 }); 
  
 // Define reservationAffinity 
  
 const 
  
 reservationAffinity 
  
 = 
  
 new 
  
 compute 
 . 
  ReservationAffinity 
 
 ({ 
  
 consumeReservationType 
 : 
  
 ' NO_RESERVATION 
' 
 , 
  
 }); 
  
 // Define instance template 
  
 const 
  
 instanceTemplate 
  
 = 
  
 new 
  
 compute 
 . 
  InstanceTemplate 
 
 ({ 
  
 name 
 : 
  
 templateName 
 , 
  
 properties 
 : 
  
 { 
  
 disks 
 : 
  
 [ 
 disk 
 ], 
  
 machineType 
 : 
  
 'e2-medium' 
 , 
  
 // The template connects the instance to the `default` network, 
  
 // without specifying a subnetwork. 
  
 networkInterfaces 
 : 
  
 [ 
 network 
 ], 
  
 reservationAffinity 
 , 
  
 }, 
  
 }); 
  
 const 
  
 [ 
 response 
 ] 
  
 = 
  
 await 
  
 instanceTemplatesClient 
 . 
 insert 
 ({ 
  
 project 
 : 
  
 projectId 
 , 
  
 instanceTemplateResource 
 : 
  
 instanceTemplate 
 , 
  
 }); 
  
 let 
  
 operation 
  
 = 
  
 response 
 . 
 latestResponse 
 ; 
  
 // Wait for the create instance operation to complete. 
  
 while 
  
 ( 
 operation 
 . 
 status 
  
 !== 
  
 'DONE' 
 ) 
  
 { 
  
 [ 
 operation 
 ] 
  
 = 
  
 await 
  
 globalOperationsClient 
 . 
 wait 
 ({ 
  
 operation 
 : 
  
 operation 
 . 
 name 
 , 
  
 project 
 : 
  
 projectId 
 , 
  
 }); 
  
 } 
  
 console 
 . 
 log 
 ( 
 `Template 
 ${ 
 templateName 
 } 
 created.` 
 ); 
 } 
 await 
  
 callCreateTemplateToNotConsumeReservation 
 (); 
 

Python

To create an instance template that configures instances to not consume reservations, use the following code sample:

  from 
  
 __future__ 
  
 import 
 annotations 
 import 
  
 sys 
 from 
  
 typing 
  
 import 
 Any 
 from 
  
 google.api_core.extended_operation 
  
 import 
 ExtendedOperation 
 from 
  
 google.cloud 
  
 import 
  compute_v1 
 
 def 
  
 wait_for_extended_operation 
 ( 
 operation 
 : 
 ExtendedOperation 
 , 
 verbose_name 
 : 
 str 
 = 
 "operation" 
 , 
 timeout 
 : 
 int 
 = 
 300 
 ) 
 - 
> Any 
 : 
  
 """ 
 Waits for the extended (long-running) operation to complete. 
 If the operation is successful, it will return its result. 
 If the operation ends with an error, an exception will be raised. 
 If there were any warnings during the execution of the operation 
 they will be printed to sys.stderr. 
 Args: 
 operation: a long-running operation you want to wait on. 
 verbose_name: (optional) a more verbose name of the operation, 
 used only during error and warning reporting. 
 timeout: how long (in seconds) to wait for operation to finish. 
 If None, wait indefinitely. 
 Returns: 
 Whatever the operation.result() returns. 
 Raises: 
 This method will raise the exception received from `operation.exception()` 
 or RuntimeError if there is no exception set, but there is an `error_code` 
 set for the `operation`. 
 In case of an operation taking longer than `timeout` seconds to complete, 
 a `concurrent.futures.TimeoutError` will be raised. 
 """ 
 result 
 = 
 operation 
 . 
 result 
 ( 
 timeout 
 = 
 timeout 
 ) 
 if 
 operation 
 . 
 error_code 
 : 
 print 
 ( 
 f 
 "Error during 
 { 
 verbose_name 
 } 
 : [Code: 
 { 
 operation 
 . 
 error_code 
 } 
 ]: 
 { 
 operation 
 . 
 error_message 
 } 
 " 
 , 
 file 
 = 
 sys 
 . 
 stderr 
 , 
 flush 
 = 
 True 
 , 
 ) 
 print 
 ( 
 f 
 "Operation ID: 
 { 
 operation 
 . 
 name 
 } 
 " 
 , 
 file 
 = 
 sys 
 . 
 stderr 
 , 
 flush 
 = 
 True 
 ) 
 raise 
 operation 
 . 
 exception 
 () 
 or 
 RuntimeError 
 ( 
 operation 
 . 
 error_message 
 ) 
 if 
 operation 
 . 
 warnings 
 : 
 print 
 ( 
 f 
 "Warnings during 
 { 
 verbose_name 
 } 
 : 
 \n 
 " 
 , 
 file 
 = 
 sys 
 . 
 stderr 
 , 
 flush 
 = 
 True 
 ) 
 for 
 warning 
 in 
 operation 
 . 
 warnings 
 : 
 print 
 ( 
 f 
 " - 
 { 
 warning 
 . 
 code 
 } 
 : 
 { 
 warning 
 . 
 message 
 } 
 " 
 , 
 file 
 = 
 sys 
 . 
 stderr 
 , 
 flush 
 = 
 True 
 ) 
 return 
 result 
 def 
  
 create_instance_template_not_consume_reservation 
 ( 
 project_id 
 : 
 str 
 , 
 template_name 
 : 
 str 
 , 
 machine_type 
 : 
 str 
 = 
 "n1-standard-1" 
 , 
 ) 
 - 
> compute_v1 
 . 
 InstanceTemplate 
 : 
  
 """ 
 Creates an instance template that creates VMs that don't explicitly consume reservations 
 Args: 
 project_id: project ID or project number of the Cloud project you use. 
 template_name: name of the new template to create. 
 machine_type: machine type for the instance. 
 Returns: 
 InstanceTemplate object that represents the new instance template. 
 """ 
 template 
 = 
  compute_v1 
 
 . 
  InstanceTemplate 
 
 () 
 template 
 . 
 name 
 = 
 template_name 
 template 
 . 
 properties 
 . 
 machine_type 
 = 
 machine_type 
 # The template describes the size and source image of the boot disk 
 # to attach to the instance. 
 template 
 . 
 properties 
 . 
  disks 
 
 = 
 [ 
  compute_v1 
 
 . 
  AttachedDisk 
 
 ( 
 boot 
 = 
 True 
 , 
 auto_delete 
 = 
 True 
 , 
 # The disk will be deleted when the instance is deleted 
 initialize_params 
 = 
  compute_v1 
 
 . 
  AttachedDiskInitializeParams 
 
 ( 
 source_image 
 = 
 "projects/debian-cloud/global/images/family/debian-11" 
 , 
 disk_size_gb 
 = 
 10 
 , 
 ), 
 ) 
 ] 
 # The template connects the instance to the `default` network, 
 template 
 . 
 properties 
 . 
 network_interfaces 
 = 
 [ 
  compute_v1 
 
 . 
  NetworkInterface 
 
 ( 
 network 
 = 
 "global/networks/default" 
 , 
 access_configs 
 = 
 [ 
  compute_v1 
 
 . 
  AccessConfig 
 
 ( 
 name 
 = 
 "External NAT" 
 , 
 type 
 = 
 "ONE_TO_ONE_NAT" 
 , 
 ) 
 ], 
 ) 
 ] 
 # The template doesn't explicitly consume reservations 
 template 
 . 
 properties 
 . 
 reservation_affinity 
 = 
  compute_v1 
 
 . 
  ReservationAffinity 
 
 ( 
 consume_reservation_type 
 = 
 "NO_RESERVATION" 
 ) 
 template_client 
 = 
  compute_v1 
 
 . 
  InstanceTemplatesClient 
 
 () 
 operation 
 = 
 template_client 
 . 
  insert 
 
 ( 
 project 
 = 
 project_id 
 , 
 instance_template_resource 
 = 
 template 
 ) 
 wait_for_extended_operation 
 ( 
 operation 
 , 
 "instance template creation" 
 ) 
 return 
 template_client 
 . 
  get 
 
 ( 
 project 
 = 
 project_id 
 , 
 instance_template 
 = 
 template_name 
 ) 
 

REST

To create an instance template that configures instances to not consume reservations, make a POST request to one of the following methods:

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 specify family/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 .

What's next

Create a Mobile Website
View Site in Mobile | Classic
Share by: