Create a VM with a custom machine type


Compute Engine offers predefined machine types that you can use when you create a VM instance. A predefined machine type has a preset number of vCPUs and amount of memory, and is charged at a set price. If predefined VMs don't meet your needs, you can create a VM instance with custom virtualized hardware settings. Specifically, you can create a VM instance with a custom number of vCPUs and amount of memory, effectively using a custom machine type. Custom machine types are available for the following:

Custom VMs are ideal in the following scenarios:

  • Workloads that aren't a good fit for the predefined VM types.
  • Workloads that require more processing power or more memory but don't need all upgrades that are provided by the next level machine type.
  • Workloads that have per-CPU software license costs. Customize the number of vCPUs to avoid license costs for unused processing power or to match available software license counts.

Before you begin

Required roles

To get the permissions that you need to create a VM with a custom machine type, 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 create a VM with a custom machine type. To see the exact permissions that are required, expand the Required permissions section:

Required permissions

The following permissions are required to create a VM with a custom machine type:

  • To add extended memory to an existing VM: compute.instances.setMachineType on the VM
  • To create a VM with a custom machine type:
    • 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

You might also be able to get these permissions with custom roles or other predefined roles.

Limitations

  • If your VM has Local SSD disks, you can't change the vCPU and memory configuration.
  • Extended memory is available on N4, N2, N2D, and N1 custom machine types. Predefined machine types, as well as G2 and E2 custom machine types, don't support extended memory.
  • There is a maximum amount of memory you can add to each machine type.
  • You must specify memory in 256 MB increments.
  • Extended memory does not qualify for Resource-based committed use discounts (CUDs).

Custom machine type pricing

Google charges for custom VMs based on the number of vCPUs and memory hours that the VM uses. This is different from how predefined machine types are charged. The on-demand prices for custom machine types include a 5% premium over the on-demand prices for predefined machine types.

  • When using a custom machine type, any memory up to and including the default amount per vCPU is charged at the machine family custom machine type pricing. E2 custom machine and E2 shared-core custom machine types share the same pricing scheme.

  • If you use your commitments to run custom machine types, then Compute Engine charges a 5% premium over the commitment prices. Compute Engine charges this premium for the portion and duration of your commitment that you run these custom machine type VMs.

See VM instance pricing for information by machine series.

Custom VMs are subject to the same 1-minute minimum charge as any other instance, but sustained use discounts for custom machine types are calculated differently. For more information, see sustained use discounts for custom VMs.

Extended memory pricing

For memory above the default, Compute Engine charges for extended memory based on the machine family extended custom memory pricing. The price for extended memory is different from the price for memory below the default threshold.

Extended memory prices differ depending on the region. Instances running with extended memory are subject to the same 1-minute minimum charge as any other instance. Extended memory also qualifies for sustained use discounts.

Add extended memory to a machine type

Depending on the machine series, each machine type has a specific amount of memory by default. For some workloads, this might not be enough memory. For an extra cost, you can get more memory per vCPU beyond the default limit. This is referred to as extended memory.

With extended memory, you can specify an amount of memory for the custom machine type with no limitation per vCPU. Instead of using the default memory size based on the number of vCPUs specified, you can specify an amount of extended memory, up to the limit of the machine series.

Machine seriesvCPU limitMemory limit
N480640 GB
N280864 GB
N2D96768 GB
N196624 GB

If you require more memory, use one of the memory-optimized machine types.

Determine if you need extended memory

Certain workloads need more than the default amount of memory per vCPU to deliver optimum results. Workloads that are based on in-memory high- performance analytics databases, including relational and NoSQL databases such as MS SQL Server, MongoDB, and MemcacheD/Redis, are in this category. The vCPU-based licensing of operating systems and software stacks also makes selecting an optimal VM memory configuration more challenging with predefined machine types. By using extended memory, you can add as much memory as your VMs need for the best price- performance ratio.

Express memory in GB or MB

For Google Cloud tools and documentation, machine type memory is calculated in gigabytes (GB), where 1 GB is 230 bytes. This unit of measurement is also known as a gibibyte (GiB) . When converting memory from GB to MB, 1 GB = 1024 MB.

In the API, you must always provide memory in megabytes. If you use the Google Cloud CLI, you can provide the total memory for a VM in gigabytes or megabytes. However, the gcloud CLI expects the memory value to be an integer, so you cannot provide a float value. For example, to express 5.75 GB, convert 5.75 GB into MB instead. In this case, 5.75 GB is 5888 MB.

Create a VM with a custom machine type

Before you create a custom VM instance, make sure you read the custom specifications for creating this machine type.

Console

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

    Go to Create an instance

  2. In the Machine configuration pane, do the following:

    1. Select the Region and Zone where you want to host the instance.
    2. Select General-purpose or GPUs.
      1. In the Series list, select a machine series.
        • For General-purpose, choose N4, N2, N2D, E2, or N1.
        • For GPUs, choose G2.
      2. In the Machine type section, select Custom.
      3. To specify the number of vCPUs and the amount of memory for the instance, drag the sliders or enter the values in the text boxes. The console displays an estimated cost for the instance as you change the number of vCPUs and memory.
  3. Optional: Specify other configuration options. For more information, see Configuration options during instance creation.

  4. To create and start the instance, click Create.

gcloud

Create a custom machine type using the gcloud compute instances create command with the --machine-type option.

 gcloud compute instances create INSTANCE_NAME \ --machine-type=MACHINE_TYPE-custom-NUMBER_OF_VCPUS-AMOUNT_OF_MEMORY_MB

Replace the following:

  • INSTANCE_NAME: instance name
  • MACHINE_TYPE: machine type, like N2
  • NUMBER_OF_VCPUS: number of vCPUs
  • AMOUNT_OF_MEMORY_MB: amount of memory in MB or GB

    To create a G2 VM with 4 vCPUs and 19 GB of memory in zone us-central1-a, run the following command:

    gcloud compute instances create example-instance \ --zone=us-central1-a --machine-type=g2-custom-4-19456 

    For G2 VMs, memory must be a multiple of 1024 MB and within the supported memory range. For more information about creating G2 VMs, see Create a VM that has attached GPUs.

Here's an example using an N2 machine type with 48 vCPUs and 310 GB of memory in zone us-central1-a:

gcloud compute instances create example-instance \ --zone=us-central1-a --machine-type=n2-custom-48-317440 

For supported machine series, such as N2, you can configure extended memory, which lets you specify a higher amount of memory than the maximum ratio of memory to vCPUs. To configure extended memory, increase the value of AMOUNT_OF_MEMORY_MB and append -ext to the machine type name, for example --machine-type=n2-custom-48-720000-ext.

Alternatively, you can specify a custom machine type by using the custom options: --custom-cpu, --custom-memory, --custom-vm-type, and --custom-extensions.

 gcloud compute instances create INSTANCE_NAME \ --custom-cpu=NUMBER_OF_VCPUS \ --custom-memory=NUMBER_OF_MB \ --custom-vm-type=MACHINE_TYPE \ --custom-extension 

Replace the following:

  • INSTANCE_NAME: name of instance
  • NUMBER_OF_VCPUS: number of vCPUs
  • NUMBER_OF_MB: amount of memory in MB or GB
  • MACHINE_TYPE: machine type, like N2

The following example is an N2 custom machine type with 48 vCPUs and 310 GB of memory using the Google Cloud CLI options.

 gcloud compute instances create example-instance \ --custom-cpu=48 --custom-memory=317440 --custom-extension --custom-vm-type=n2 

When using the --custom-memory option, specify the total amount of memory in GB or in MB. The property value must be an integer, so if you want to specify increments of 0.25 GB for memory, use the value 250 MB instead.

For E2 shared-core custom machine types, use the same gcloud compute instances create command and include the shared-core machine size: micro, small, or medium. The vCPU and memory are limited.

 gcloud compute instances create INSTANCE_NAME\ --machine-type=MACHINE_TYPE-AMOUNT_OF_MEMORY_MB

Replace the following:

  • INSTANCE_NAME: name of instance
  • MACHINE_TYPE: E2 small
  • AMOUNT_OF_MEMORY_MB: amount of memory in MB or GB

The following example is an E2 shared-core small custom machine type with 0.5 vCPU and 2.25 GB of memory.

 gcloud compute instances create example-instance \ --machine-type=e2-custom-small-2304 

Terraform

To generate the Terraform code, you can use the Equivalent code component in the Google Cloud console.
  1. In the Google Cloud console, go to the VM instances page.

    Go to VM Instances

  2. Click Create instance.
  3. Specify the parameters you want.
  4. At the top or bottom of the page, click Equivalent code, and then click the Terraform tab to view the Terraform code.

Go

Before trying this sample, follow the Go setup instructions in the Compute Engine quickstart using client libraries. For more information, see the Compute Engine Go API reference documentation.

To authenticate to Compute Engine, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

import("context""fmt""io"compute"cloud.google.com/go/compute/apiv1"computepb"cloud.google.com/go/compute/apiv1/computepb""google.golang.org/protobuf/proto")funccustomMachineTypeURI(zone,cpuSeriesstring,coreCount,memoryint)(string,error){const(n1="custom"n2="n2-custom"n2d="n2d-custom"e2="e2-custom"e2Micro="e2-custom-micro"e2Small="e2-custom-small"e2Medium="e2-custom-medium")typetypeLimitstruct{allowedCores[]intminMemPerCoreintmaxMemPerCoreintallowExtraMemoryboolextraMemoryLimitint}makeRange:=func(start,end,stepint)[]int{ifstep<=0||end < start{return[]int{}}s:=make([]int,0,1+(end-start)/step)forstart<=end{s=append(s,start)start+=step}returns}containsString:=func(s[]string,strstring)bool{for_,v:=ranges{ifv==str{returntrue}}returnfalse}containsInt:=func(nums[]int,nint)bool{for_,v:=rangenums{ifv==n{returntrue}}returnfalse}var(cpuSeriesE2Limit=typeLimit{allowedCores:makeRange(2,33,2),minMemPerCore:512,maxMemPerCore:8192,}cpuSeriesE2MicroLimit=typeLimit{minMemPerCore:1024,maxMemPerCore:2048}cpuSeriesE2SmallLimit=typeLimit{minMemPerCore:2048,maxMemPerCore:4096}cpuSeriesE2MeidumLimit=typeLimit{minMemPerCore:4096,maxMemPerCore:8192}cpuSeriesN2Limit=typeLimit{allowedCores:append(makeRange(2,33,2),makeRange(36,129,4)...),minMemPerCore:512,maxMemPerCore:8192,allowExtraMemory:true,extraMemoryLimit:624 << 10,}cpuSeriesN2DLimit=typeLimit{allowedCores:[]int{2,4,8,16,32,48,64,80,96},minMemPerCore:512,maxMemPerCore:8192,allowExtraMemory:true,extraMemoryLimit:768 << 10,}cpuSeriesN1Limit=typeLimit{allowedCores:append([]int{1},makeRange(2,97,2)...),minMemPerCore:922,maxMemPerCore:6656,allowExtraMemory:true,extraMemoryLimit:624 << 10,})typeLimitsMap:=map[string]typeLimit{n1:cpuSeriesN1Limit,n2:cpuSeriesN2Limit,n2d:cpuSeriesN2DLimit,e2:cpuSeriesE2Limit,e2Micro:cpuSeriesE2MicroLimit,e2Small:cpuSeriesE2SmallLimit,e2Medium:cpuSeriesE2MeidumLimit,}if!containsString([]string{e2,n1,n2,n2d},cpuSeries){return"",fmt.Errorf("incorrect cpu type: %v",cpuSeries)}tl:=typeLimitsMap[cpuSeries]// Check whether the requested parameters are allowed.// Find more information about limitations of custom machine types at:// https://cloud.google.com/compute/docs/general-purpose-machines#custom_machine_types// Check the number of coresiflen(tl.allowedCores) > 0 && !containsInt(tl.allowedCores,coreCount){return"",fmt.Errorf("invalid number of cores requested. Allowed number of cores for %v is: %v",cpuSeries,tl.allowedCores,)}// Memory must be a multiple of 256 MBifmemory%256!=0{return"",fmt.Errorf("requested memory must be a multiple of 256 MB")}// Check if the requested memory isn't too littleifmemory < coreCount*tl.minMemPerCore{return"",fmt.Errorf("requested memory is too low. Minimal memory for %v is %v MB per core",cpuSeries,tl.minMemPerCore,)}// Check if the requested memory isn't too muchifmemory > coreCount*tl.maxMemPerCore && !tl.allowExtraMemory{return"",fmt.Errorf("requested memory is too large.. Maximum memory allowed for %v is %v MB per core",cpuSeries,tl.maxMemPerCore,)}ifmemory > tl.extraMemoryLimit && tl.allowExtraMemory{return"",fmt.Errorf("requested memory is too large.. Maximum memory allowed for %v is %v MB",cpuSeries,tl.extraMemoryLimit,)}// Return the custom machine type in form of a string acceptable by Compute Engine API.ifcontainsString([]string{e2Small,e2Micro,e2Medium},cpuSeries){returnfmt.Sprintf("zones/%v/machineTypes/%v-%v",zone,cpuSeries,memory),nil}ifmemory > coreCount*tl.maxMemPerCore{returnfmt.Sprintf("zones/%v/machineTypes/%v-%v-%v-ext",zone,cpuSeries,coreCount,memory,),nil}returnfmt.Sprintf("zones/%v/machineTypes/%v-%v-%v",zone,cpuSeries,coreCount,memory),nil}// createInstanceWithCustomMachineTypeWithHelper creates a new VM instance with a custom machine type.funccreateInstanceWithCustomMachineTypeWithHelper(wio.Writer,projectID,zone,instanceName,cpuSeriesstring,coreCount,memoryint,)error{// projectID := "your_project_id"// zone := "europe-central2-b"// instanceName := "your_instance_name"// cpuSeries := "e2-custom-micro" // the type of CPU you want to use"// coreCount := 2 // number of CPU cores you want to use.// memory := 256 // the amount of memory for the VM instance, in megabytes.machineType,err:=customMachineTypeURI(zone,cpuSeries,coreCount,memory)iferr!=nil{returnfmt.Errorf("unable to create custom machine type string: %w",err)}ctx:=context.Background()instancesClient,err:=compute.NewInstancesRESTClient(ctx)iferr!=nil{returnfmt.Errorf("NewInstancesRESTClient: %w",err)}deferinstancesClient.Close()req:=&computepb.InsertInstanceRequest{Project:projectID,Zone:zone,InstanceResource:&computepb.Instance{Name:proto.String(instanceName),Disks:[]*computepb.AttachedDisk{{InitializeParams:&computepb.AttachedDiskInitializeParams{DiskSizeGb:proto.Int64(10),SourceImage:proto.String("projects/debian-cloud/global/images/family/debian-12",),},AutoDelete:proto.Bool(true),Boot:proto.Bool(true),},},MachineType:proto.String(machineType),NetworkInterfaces:[]*computepb.NetworkInterface{{Name:proto.String("global/networks/default"),},},},}op,err:=instancesClient.Insert(ctx,req)iferr!=nil{returnfmt.Errorf("unable to create instance: %w",err)}iferr=op.Wait(ctx);err!=nil{returnfmt.Errorf("unable to wait for the operation: %w",err)}fmt.Fprintf(w,"Instance created\n")returnnil}

Java

Before trying this sample, follow the Java setup instructions in the Compute Engine quickstart using client libraries. For more information, see the Compute Engine Java API reference documentation.

To authenticate to Compute Engine, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

importcom.google.cloud.compute.v1.AttachedDisk;importcom.google.cloud.compute.v1.AttachedDiskInitializeParams;importcom.google.cloud.compute.v1.InsertInstanceRequest;importcom.google.cloud.compute.v1.Instance;importcom.google.cloud.compute.v1.InstancesClient;importcom.google.cloud.compute.v1.NetworkInterface;importcom.google.cloud.compute.v1.Operation;importcom.google.common.collect.ImmutableMap;importjava.io.IOException;importjava.util.Arrays;importjava.util.Collections;importjava.util.Map;importjava.util.Objects;importjava.util.concurrent.ConcurrentHashMap;importjava.util.concurrent.ExecutionException;importjava.util.concurrent.TimeUnit;importjava.util.concurrent.TimeoutException;importjava.util.stream.IntStream;publicclassCreateWithHelper{// This class defines the configurable parameters for a custom VM.staticfinalclassTypeLimits{int[]allowedCores;intminMemPerCore;intmaxMemPerCore;intextraMemoryLimit;booleanallowExtraMemory;TypeLimits(int[]allowedCores,intminMemPerCore,intmaxMemPerCore,booleanallowExtraMemory,intextraMemoryLimit){this.allowedCores=allowedCores;this.minMemPerCore=minMemPerCore;this.maxMemPerCore=maxMemPerCore;this.allowExtraMemory=allowExtraMemory;this.extraMemoryLimit=extraMemoryLimit;}}publicenumCpuSeries{N1("custom"),N2("n2-custom"),N2D("n2d-custom"),E2("e2-custom"),E2_MICRO("e2-custom-micro"),E2_SMALL("e2-custom-small"),E2_MEDIUM("e2-custom-medium");privatestaticfinalMap<String,CpuSeries>ENUM_MAP;static{ENUM_MAP=init();}// Build an immutable map of String name to enum pairs.publicstaticMap<String,CpuSeries>init(){Map<String,CpuSeries>map=newConcurrentHashMap<>();for(CpuSeriesinstance:CpuSeries.values()){map.put(instance.getCpuSeries(),instance);}returnCollections.unmodifiableMap(map);}privatefinalStringcpuSeries;CpuSeries(StringcpuSeries){this.cpuSeries=cpuSeries;}publicstaticCpuSeriesget(Stringname){returnENUM_MAP.get(name);}publicStringgetCpuSeries(){returnthis.cpuSeries;}}// This enum correlates a machine type with its limits.// The limits for various CPU types are described in:// https://cloud.google.com/compute/docs/general-purpose-machinesenumLimits{CPUSeries_E2(newTypeLimits(getNumsInRangeWithStep(2,33,2),512,8192,false,0)),CPUSeries_E2MICRO(newTypeLimits(newint[]{},1024,2048,false,0)),CPUSeries_E2SMALL(newTypeLimits(newint[]{},2048,4096,false,0)),CPUSeries_E2MEDIUM(newTypeLimits(newint[]{},4096,8192,false,0)),CPUSeries_N2(newTypeLimits(concat(getNumsInRangeWithStep(2,33,2),getNumsInRangeWithStep(36,129,4)),512,8192,true,gbToMb(624))),CPUSeries_N2D(newTypeLimits(newint[]{2,4,8,16,32,48,64,80,96},512,8192,true,gbToMb(768))),CPUSeries_N1(newTypeLimits(concat(newint[]{1},getNumsInRangeWithStep(2,97,2)),922,6656,true,gbToMb(624)));privatefinalTypeLimitstypeLimits;Limits(TypeLimitstypeLimits){this.typeLimits=typeLimits;}publicTypeLimitsgetTypeLimits(){returntypeLimits;}}staticImmutableMap<String,Limits>typeLimitsMap=ImmutableMap.<String,Limits>builder().put("N1",Limits.CPUSeries_N1).put("N2",Limits.CPUSeries_N2).put("N2D",Limits.CPUSeries_N2D).put("E2",Limits.CPUSeries_E2).put("E2_MICRO",Limits.CPUSeries_E2MICRO).put("E2_SMALL",Limits.CPUSeries_E2SMALL).put("E2_MEDIUM",Limits.CPUSeries_E2SMALL).build();// Returns the array of integers within the given range, incremented by the specified step.// start (inclusive): starting number of the range// stop (inclusive): ending number of the range// step : increment valuestaticint[]getNumsInRangeWithStep(intstart,intstop,intstep){returnIntStream.range(start,stop).filter(x->(x-start)%step==0).toArray();}staticintgbToMb(intvalue){returnvalue << 10;}staticint[]concat(int[]a,int[]b){int[]result=newint[a.length+b.length];System.arraycopy(a,0,result,0,a.length);System.arraycopy(b,0,result,a.length,b.length);returnresult;}publicstaticvoidmain(String[]args)throwsIOException,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.StringprojectId="your-google-cloud-project-id";// Name of the zone to create the instance in. For example: "us-west3-b".Stringzone="google-cloud-zone";// Name of the new virtual machine (VM) instance.StringinstanceName="instance-name";StringcpuSeries="N1";// Number of CPU cores you want to use.intcoreCount=2;// The amount of memory for the VM instance, in megabytes.intmemory=256;createInstanceWithCustomMachineTypeWithHelper(projectId,zone,instanceName,cpuSeries,coreCount,memory);}// Create a VM instance with a custom machine type.publicstaticvoidcreateInstanceWithCustomMachineTypeWithHelper(Stringproject,Stringzone,StringinstanceName,StringcpuSeries,intcoreCount,intmemory)throwsIOException,ExecutionException,InterruptedException,TimeoutException{// Construct the URI string identifying the machine type.StringmachineTypeUri=customMachineTypeUri(zone,cpuSeries,coreCount,memory);// Initialize client that will be used to send requests. This client only needs to be created// once, and can be reused for multiple requests. After completing all of your requests, call// the `instancesClient.close()` method on the client to safely// clean up any remaining background resources.try(InstancesClientinstancesClient=InstancesClient.create()){AttachedDiskattachedDisk=AttachedDisk.newBuilder().setInitializeParams(// Describe the size and source image of the boot disk to attach to the instance.// The list of public images available in Compute Engine can be found here:// https://cloud.google.com/compute/docs/images#list_of_public_images_available_onAttachedDiskInitializeParams.newBuilder().setSourceImage(String.format("projects/%s/global/images/family/%s","debian-cloud","debian-11")).setDiskSizeGb(10).build())// Remember to set auto_delete to True if you want the disk to be deleted when you delete// your VM instance..setAutoDelete(true).setBoot(true).build();// Create the Instance object with the relevant information.Instanceinstance=Instance.newBuilder().setName(instanceName).addDisks(attachedDisk).setMachineType(machineTypeUri).addNetworkInterfaces(NetworkInterface.newBuilder().setName("global/networks/default").build()).build();// Create the insert instance request object.InsertInstanceRequestinsertInstanceRequest=InsertInstanceRequest.newBuilder().setProject(project).setZone(zone).setInstanceResource(instance).build();// Invoke the API with the request object and wait for the operation to complete.Operationresponse=instancesClient.insertAsync(insertInstanceRequest).get(3,TimeUnit.MINUTES);// Check for errors.if(response.hasError()){thrownewError("Instance creation failed!!"+response);}System.out.printf("Instance created : %s",instanceName);System.out.println("Operation Status: "+response.getStatus());}}publicstaticStringcustomMachineTypeUri(Stringzone,StringcpuSeries,intcoreCount,intmemory){if(!Arrays.asList(CpuSeries.E2.cpuSeries,CpuSeries.N1.cpuSeries,CpuSeries.N2.cpuSeries,CpuSeries.N2D.cpuSeries).contains(cpuSeries)){thrownewError(String.format("Incorrect cpu type: %s",cpuSeries));}TypeLimitstypeLimit=Objects.requireNonNull(typeLimitsMap.get(CpuSeries.get(cpuSeries).name())).typeLimits;// Perform the following checks to verify if the requested parameters are allowed.// Find more information about limitations of custom machine types at:// https://cloud.google.com/compute/docs/general-purpose-machines#custom_machine_types// 1. Check the number of cores and if the coreCount is present in allowedCores.if(typeLimit.allowedCores.length > 0 && Arrays.stream(typeLimit.allowedCores).noneMatch(x->x==coreCount)){thrownewError(String.format("Invalid number of cores requested. "+"Number of cores requested for CPU %s should be one of: %s",cpuSeries,Arrays.toString(typeLimit.allowedCores)));}// 2. Memory must be a multiple of 256 MBif(memory%256!=0){thrownewError("Requested memory must be a multiple of 256 MB");}// 3. Check if the requested memory isn't too littleif(memory < coreCount*typeLimit.minMemPerCore){thrownewError(String.format("Requested memory is too low. Minimum memory for %s is %s MB per core",cpuSeries,typeLimit.minMemPerCore));}// 4. Check if the requested memory isn't too muchif(memory > coreCount*typeLimit.maxMemPerCore && !typeLimit.allowExtraMemory){thrownewError(String.format("Requested memory is too large.. Maximum memory allowed for %s is %s MB per core",cpuSeries,typeLimit.extraMemoryLimit));}// 5. Check if the requested memory isn't too largeif(memory > typeLimit.extraMemoryLimit && typeLimit.allowExtraMemory){thrownewError(String.format("Requested memory is too large.. Maximum memory allowed for %s is %s MB",cpuSeries,typeLimit.extraMemoryLimit));}// Check if the CPU Series is E2 and return the custom machine type in the form of a string// acceptable by Compute Engine API.if(Arrays.asList(CpuSeries.E2_SMALL.cpuSeries,CpuSeries.E2_MICRO.cpuSeries,CpuSeries.E2_MEDIUM.cpuSeries).contains(cpuSeries)){returnString.format("zones/%s/machineTypes/%s-%s",zone,cpuSeries,memory);}// Check if extended memory was requested and return the extended custom machine type// in the form of a string acceptable by Compute Engine API.if(memory > coreCount*typeLimit.maxMemPerCore){returnString.format("zones/%s/machineTypes/%s-%s-%s-ext",zone,cpuSeries,coreCount,memory);}// Return the custom machine type in the form of a standard string// acceptable by Compute Engine API.returnString.format("zones/%s/machineTypes/%s-%s-%s",zone,cpuSeries,coreCount,memory);}}

Node.js

Before trying this sample, follow the Node.js setup instructions in the Compute Engine quickstart using client libraries. For more information, see the Compute Engine Node.js API reference documentation.

To authenticate to Compute Engine, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

/** * TODO(developer): Uncomment and replace these variables before running the sample. */// const projectId = 'YOUR_PROJECT_ID';// const zone = 'europe-central2-b';// const instanceName = 'YOUR_INSTANCE_NAME';// const cpuSeries = 'N1';// const coreCount = 2// const memory = 256constcompute=require('@google-cloud/compute');functionrange(from,to,step){return[...Array(Math.floor((to-from)/step)+1)].map((_,i)=>from+i*step);}classCustomMachineType{constructor(zone,cpuSeries,coreCount,memory){this.zone=zone;this.cpuSeries=cpuSeries;this.coreCount=coreCount;this.memory=memory;this.N1='custom';this.N2='n2-custom';this.N2D='n2d-custom';this.E2='e2-custom';this.E2Micro='e2-custom-micro';this.E2Small='e2-custom-small';this.E2Medium='e2-custom-medium';this.CpuSeriesE2Limit={allowedCores:range(2,33,2),minMemPerCore:512,maxMemPerCore:8192,allowExtraMemory:false,extraMemoryLimit:0,};this.CpuSeriesE2MicroLimit={allowedCores:[],minMemPerCore:1024,maxMemPerCore:2048,allowExtraMemory:false,extraMemoryLimit:0,};this.CpuSeriesE2SmallLimit={allowedCores:[],minMemPerCore:2048,maxMemPerCore:4096,allowExtraMemory:false,extraMemoryLimit:0,};this.CpuSeriesE2MediumLimit={allowedCores:[],minMemPerCore:4096,maxMemPerCore:8192,allowExtraMemory:false,extraMemoryLimit:0,};this.CpuSeriesN2Limit={allowedCores:[...range(2,33,2),...range(36,129,4)],minMemPerCore:512,maxMemPerCore:8192,allowExtraMemory:true,extraMemoryLimit:624 << 10,};this.CpuSeriesN2DLimit={allowedCores:[2,4,8,16,32,48,64,80,96],minMemPerCore:512,maxMemPerCore:8192,allowExtraMemory:true,extraMemoryLimit:768 << 10,};this.CpuSeriesN1Limit={allowedCores:[1,range(2,97,2)],minMemPerCore:922,maxMemPerCore:6656,allowExtraMemory:true,extraMemoryLimit:624 << 10,};this.TYPE_LIMITS={[this.N1]:this.CpuSeriesN1Limit,[this.N2]:this.CpuSeriesN2Limit,[this.N2D]:this.CpuSeriesN2DLimit,[this.E2]:this.CpuSeriesE2Limit,[this.E2Micro]:this.CpuSeriesE2MicroLimit,[this.E2Small]:this.CpuSeriesE2SmallLimit,[this.E2Medium]:this.CpuSeriesE2MediumLimit,};if(![this.E2,this.N1,this.N2,this.N2D].includes(cpuSeries)){thrownewError(`Incorrect CPU type: ${this.cpuSeries}`);}this.typeLimit=this.TYPE_LIMITS[this.cpuSeries];// Check whether the requested parameters are allowed.// Find more information about limitations of custom machine types at:// https://cloud.google.com/compute/docs/general-purpose-machines#custom_machine_types// Check the number of coresif(this.typeLimit.allowedCores.length > 0&& !this.typeLimit.allowedCores.includes(coreCount)){thrownewError(`Invalid number of cores requested. Allowed number of cores for ${this.cpuSeries} is: ${this.typeLimit.allowedCores}`);}// Memory must be a multiple of 256 MBif(this.memory%256!==0){thrownewError('Requested memory must be a multiple of 256 MB');}// Check if the requested memory isn't too littleif(this.memory < this.coreCount*this.typeLimit.minMemPerCore){thrownewError(`Requested memory is too low. Minimal memory for ${this.cpuSeries} is ${this.typeLimit.minMemPerCore} MB per core`);}// Check if the requested memory isn't too muchif(this.memory > this.coreCount*this.typeLimit.maxMemPerCore&& !this.typeLimit.allowExtraMemory){thrownewError(`Requested memory is too large.. Maximum memory allowed for ${this.cpuSeries} is ${this.typeLimit.maxMemPerCore} MB per core`);}if(this.memory > this.typeLimit.extraMemoryLimit&& this.typeLimit.allowExtraMemory){thrownewError(`Requested memory is too large.. Maximum memory allowed for ${this.cpuSeries} is ${this.typeLimit.extraMemoryLimit} MB`);}}// Returns the custom machine type in form of a string acceptable by Compute Engine API.getMachineTypeURI(){if([this.E2Small,this.E2Micro,this.E2Medium].includes(this.cpuSeries)){return`zones/${this.zone}/machineTypes/${this.cpuSeries}-${this.memory}`;}if(this.memory > this.coreCount*this.typeLimit.maxMemPerCore){return`zones/${this.zone}/machineTypes/${this.cpuSeries}-${this.coreCount}-${this.memory}-ext`;}return`zones/${zone}/machineTypes/${this.cpuSeries}-${this.coreCount}-${this.memory}`;}}asyncfunctioncreateInstanceWithCustomMachineTypeWithHelper(){constinstancesClient=newcompute.InstancesClient();constmachineType=newCustomMachineType(zone,cpuSeries,coreCount,memory).getMachineTypeURI();const[response]=awaitinstancesClient.insert({instanceResource:{name:instanceName,disks:[{initializeParams:{diskSizeGb:'64',sourceImage:'projects/debian-cloud/global/images/family/debian-11/',},autoDelete:true,boot:true,},],machineType,networkInterfaces:[{name:'global/networks/default',},],},project:projectId,zone,});letoperation=response.latestResponse;constoperationsClient=newcompute.ZoneOperationsClient();// Wait for the create operation to complete.while(operation.status!=='DONE'){[operation]=awaitoperationsClient.wait({operation:operation.name,project:projectId,zone:operation.zone.split('/').pop(),});}console.log('Instance created.');}createInstanceWithCustomMachineTypeWithHelper();

Python

Before trying this sample, follow the Python setup instructions in the Compute Engine quickstart using client libraries. For more information, see the Compute Engine Python API reference documentation.

To authenticate to Compute Engine, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

from__future__importannotationsfromcollectionsimportnamedtuplefromenumimportEnumfromenumimportuniqueimportreimportsysfromtypingimportAnyimportwarningsfromgoogle.api_core.extended_operationimportExtendedOperationfromgoogle.cloudimportcompute_v1defgb_to_mb(value:int)-> int:returnvalue << 10classCustomMachineType:""" Allows to create custom machine types to be used with the VM instances. """@uniqueclassCPUSeries(Enum):N1="custom"N2="n2-custom"N2D="n2d-custom"E2="e2-custom"E2_MICRO="e2-custom-micro"E2_SMALL="e2-custom-small"E2_MEDIUM="e2-custom-medium"TypeLimits=namedtuple("TypeLimits",["allowed_cores","min_mem_per_core","max_mem_per_core","allow_extra_memory","extra_memory_limit",],)# The limits for various CPU types are described on:# https://cloud.google.com/compute/docs/general-purpose-machinesLIMITS={CPUSeries.E2:TypeLimits(frozenset(range(2,33,2)),512,8192,False,0),CPUSeries.E2_MICRO:TypeLimits(frozenset(),1024,2048,False,0),CPUSeries.E2_SMALL:TypeLimits(frozenset(),2048,4096,False,0),CPUSeries.E2_MEDIUM:TypeLimits(frozenset(),4096,8192,False,0),CPUSeries.N2:TypeLimits(frozenset(range(2,33,2)).union(set(range(36,129,4))),512,8192,True,gb_to_mb(624),),CPUSeries.N2D:TypeLimits(frozenset({2,4,8,16,32,48,64,80,96}),512,8192,True,gb_to_mb(768)),CPUSeries.N1:TypeLimits(frozenset({1}.union(range(2,97,2))),922,6656,True,gb_to_mb(624)),}def__init__(self,zone:str,cpu_series:CPUSeries,memory_mb:int,core_count:int=0):self.zone=zoneself.cpu_series=cpu_seriesself.limits=self.LIMITS[self.cpu_series]# Shared machine types (e2-small, e2-medium and e2-micro) always have# 2 vCPUs: https://cloud.google.com/compute/docs/general-purpose-machines#e2_limitationsself.core_count=2ifself.is_shared()elsecore_countself.memory_mb=memory_mbself._checked=Falseself._check_parameters()self.extra_memory_used=self._check_extra_memory()defis_shared(self):returnself.cpu_seriesin(CustomMachineType.CPUSeries.E2_SMALL,CustomMachineType.CPUSeries.E2_MICRO,CustomMachineType.CPUSeries.E2_MEDIUM,)def_check_extra_memory(self)-> bool:ifself._checked:returnself.memory_mb > self.core_count*self.limits.max_mem_per_coreelse:raiseRuntimeError("You need to call _check_parameters() before calling _check_extra_memory()")def_check_parameters(self):""" Check whether the requested parameters are allowed. Find more information about limitations of custom machine types at: https://cloud.google.com/compute/docs/general-purpose-machines#custom_machine_types """# Check the number of coresif(self.limits.allowed_coresandself.core_countnotinself.limits.allowed_cores):raiseRuntimeError(f"Invalid number of cores requested. Allowed number of cores for {self.cpu_series.name} is: {sorted(self.limits.allowed_cores)}")# Memory must be a multiple of 256 MBifself.memory_mb%256!=0:raiseRuntimeError("Requested memory must be a multiple of 256 MB.")# Check if the requested memory isn't too littleifself.memory_mb < self.core_count*self.limits.min_mem_per_core:raiseRuntimeError(f"Requested memory is too low. Minimal memory for {self.cpu_series.name} is {self.limits.min_mem_per_core} MB per core.")# Check if the requested memory isn't too muchifself.memory_mb > self.core_count*self.limits.max_mem_per_core:ifself.limits.allow_extra_memory:ifself.memory_mb > self.limits.extra_memory_limit:raiseRuntimeError(f"Requested memory is too large.. Maximum memory allowed for {self.cpu_series.name} is {self.limits.extra_memory_limit} MB.")else:raiseRuntimeError(f"Requested memory is too large.. Maximum memory allowed for {self.cpu_series.name} is {self.limits.max_mem_per_core} MB per core.")self._checked=Truedef__str__(self)-> str:""" Return the custom machine type in form of a string acceptable by Compute Engine API. """ifself.cpu_seriesin{self.CPUSeries.E2_SMALL,self.CPUSeries.E2_MICRO,self.CPUSeries.E2_MEDIUM,}:returnf"zones/{self.zone}/machineTypes/{self.cpu_series.value}-{self.memory_mb}"ifself.extra_memory_used:returnf"zones/{self.zone}/machineTypes/{self.cpu_series.value}-{self.core_count}-{self.memory_mb}-ext"returnf"zones/{self.zone}/machineTypes/{self.cpu_series.value}-{self.core_count}-{self.memory_mb}"defshort_type_str(self)-> str:""" Return machine type in a format without the zone. For example, n2-custom-0-10240. This format is used to create instance templates. """returnstr(self).rsplit("/",maxsplit=1)[1]@classmethoddeffrom_str(cls,machine_type:str):""" Construct a new object from a string. The string needs to be a valid custom machine type like: - https://www.googleapis.com/compute/v1/projects/diregapic-mestiv/zones/us-central1-b/machineTypes/e2-custom-4-8192 - zones/us-central1-b/machineTypes/e2-custom-4-8192 - e2-custom-4-8192 (in this case, the zone parameter will not be set) """zone=Noneifmachine_type.startswith("http"):machine_type=machine_type[machine_type.find("zones/"):]ifmachine_type.startswith("zones/"):_,zone,_,machine_type=machine_type.split("/")extra_mem=machine_type.endswith("-ext")ifmachine_type.startswith("custom"):cpu=cls.CPUSeries.N1_,cores,memory=machine_type.rsplit("-",maxsplit=2)else:ifextra_mem:cpu_series,_,cores,memory,_=machine_type.split("-")else:cpu_series,_,cores,memory=machine_type.split("-")ifcpu_series=="n2":cpu=cls.CPUSeries.N2elifcpu_series=="n2d":cpu=cls.CPUSeries.N2Delifcpu_series=="e2":cpu=cls.CPUSeries.E2ifcores=="micro":cpu=cls.CPUSeries.E2_MICROcores=2elifcores=="small":cpu=cls.CPUSeries.E2_SMALLcores=2elifcores=="medium":cpu=cls.CPUSeries.E2_MEDIUMcores=2else:raiseRuntimeError("Unknown CPU series.")cores=int(cores)memory=int(memory)returncls(zone,cpu,memory,cores)defget_image_from_family(project:str,family:str)-> compute_v1.Image:""" Retrieve the newest image that is part of a given family in a project. Args: project: project ID or project number of the Cloud project you want to get image from. family: name of the image family you want to get image from. Returns: An Image object. """image_client=compute_v1.ImagesClient()# List of public operating system (OS) images: https://cloud.google.com/compute/docs/images/os-detailsnewest_image=image_client.get_from_family(project=project,family=family)returnnewest_imagedefdisk_from_image(disk_type:str,disk_size_gb:int,boot:bool,source_image:str,auto_delete:bool=True,)-> compute_v1.AttachedDisk:""" Create an AttachedDisk object to be used in VM instance creation. Uses an image as the source for the new disk. Args: disk_type: the type of disk you want to create. This value uses the following format: "zones/{zone}/diskTypes/(pd-standard|pd-ssd|pd-balanced|pd-extreme)". For example: "zones/us-west3-b/diskTypes/pd-ssd" disk_size_gb: size of the new disk in gigabytes boot: boolean flag indicating whether this disk should be used as a boot disk of an instance source_image: source image to use when creating this disk. You must have read access to this disk. This can be one of the publicly available images or an image from one of your projects. This value uses the following format: "projects/{project_name}/global/images/{image_name}" auto_delete: boolean flag indicating whether this disk should be deleted with the VM that uses it Returns: AttachedDisk object configured to be created using the specified image. """boot_disk=compute_v1.AttachedDisk()initialize_params=compute_v1.AttachedDiskInitializeParams()initialize_params.source_image=source_imageinitialize_params.disk_size_gb=disk_size_gbinitialize_params.disk_type=disk_typeboot_disk.initialize_params=initialize_params# Remember to set auto_delete to True if you want the disk to be deleted when you delete# your VM instance.boot_disk.auto_delete=auto_deleteboot_disk.boot=bootreturnboot_diskdefwait_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)ifoperation.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)raiseoperation.exception()orRuntimeError(operation.error_message)ifoperation.warnings:print(f"Warnings during {verbose_name}:\n",file=sys.stderr,flush=True)forwarninginoperation.warnings:print(f" - {warning.code}: {warning.message}",file=sys.stderr,flush=True)returnresultdefcreate_instance(project_id:str,zone:str,instance_name:str,disks:list[compute_v1.AttachedDisk],machine_type:str="n1-standard-1",network_link:str="global/networks/default",subnetwork_link:str=None,internal_ip:str=None,external_access:bool=False,external_ipv4:str=None,accelerators:list[compute_v1.AcceleratorConfig]=None,preemptible:bool=False,spot:bool=False,instance_termination_action:str="STOP",custom_hostname:str=None,delete_protection:bool=False,)-> compute_v1.Instance:""" Send an instance creation request to the Compute Engine API and wait for it to complete. Args: project_id: project ID or project number of the Cloud project you want to use. zone: name of the zone to create the instance in. For example: "us-west3-b" instance_name: name of the new virtual machine (VM) instance. disks: a list of compute_v1.AttachedDisk objects describing the disks you want to attach to your new instance. machine_type: machine type of the VM being created. This value uses the following format: "zones/{zone}/machineTypes/{type_name}". For example: "zones/europe-west3-c/machineTypes/f1-micro" network_link: name of the network you want the new instance to use. For example: "global/networks/default" represents the network named "default", which is created automatically for each project. subnetwork_link: name of the subnetwork you want the new instance to use. This value uses the following format: "regions/{region}/subnetworks/{subnetwork_name}" internal_ip: internal IP address you want to assign to the new instance. By default, a free address from the pool of available internal IP addresses of used subnet will be used. external_access: boolean flag indicating if the instance should have an external IPv4 address assigned. external_ipv4: external IPv4 address to be assigned to this instance. If you specify an external IP address, it must live in the same region as the zone of the instance. This setting requires `external_access` to be set to True to work. accelerators: a list of AcceleratorConfig objects describing the accelerators that will be attached to the new instance. preemptible: boolean value indicating if the new instance should be preemptible or not. Preemptible VMs have been deprecated and you should now use Spot VMs. spot: boolean value indicating if the new instance should be a Spot VM or not. instance_termination_action: What action should be taken once a Spot VM is terminated. Possible values: "STOP", "DELETE" custom_hostname: Custom hostname of the new VM instance. Custom hostnames must conform to RFC 1035 requirements for valid hostnames. delete_protection: boolean value indicating if the new virtual machine should be protected against deletion or not. Returns: Instance object. """instance_client=compute_v1.InstancesClient()# Use the network interface provided in the network_link argument.network_interface=compute_v1.NetworkInterface()network_interface.network=network_linkifsubnetwork_link:network_interface.subnetwork=subnetwork_linkifinternal_ip:network_interface.network_i_p=internal_ipifexternal_access:access=compute_v1.AccessConfig()access.type_=compute_v1.AccessConfig.Type.ONE_TO_ONE_NAT.nameaccess.name="External NAT"access.network_tier=access.NetworkTier.PREMIUM.nameifexternal_ipv4:access.nat_i_p=external_ipv4network_interface.access_configs=[access]# Collect information into the Instance object.instance=compute_v1.Instance()instance.network_interfaces=[network_interface]instance.name=instance_nameinstance.disks=disksifre.match(r"^zones/[a-z\d\-]+/machineTypes/[a-z\d\-]+$",machine_type):instance.machine_type=machine_typeelse:instance.machine_type=f"zones/{zone}/machineTypes/{machine_type}"instance.scheduling=compute_v1.Scheduling()ifaccelerators:instance.guest_accelerators=acceleratorsinstance.scheduling.on_host_maintenance=(compute_v1.Scheduling.OnHostMaintenance.TERMINATE.name)ifpreemptible:# Set the preemptible settingwarnings.warn("Preemptible VMs are being replaced by Spot VMs.",DeprecationWarning)instance.scheduling=compute_v1.Scheduling()instance.scheduling.preemptible=Trueifspot:# Set the Spot VM settinginstance.scheduling.provisioning_model=(compute_v1.Scheduling.ProvisioningModel.SPOT.name)instance.scheduling.instance_termination_action=instance_termination_actionifcustom_hostnameisnotNone:# Set the custom hostname for the instanceinstance.hostname=custom_hostnameifdelete_protection:# Set the delete protection bitinstance.deletion_protection=True# Prepare the request to insert an instance.request=compute_v1.InsertInstanceRequest()request.zone=zonerequest.project=project_idrequest.instance_resource=instance# Wait for the create operation to complete.print(f"Creating the {instance_name} instance in {zone}...")operation=instance_client.insert(request=request)wait_for_extended_operation(operation,"instance creation")print(f"Instance {instance_name} created.")returninstance_client.get(project=project_id,zone=zone,instance=instance_name)defcreate_custom_instance(project_id:str,zone:str,instance_name:str,cpu_series:CustomMachineType.CPUSeries,core_count:int,memory:int,)-> compute_v1.Instance:""" Create a new VM instance with a custom machine type. Args: project_id: project ID or project number of the Cloud project you want to use. zone: name of the zone to create the instance in. For example: "us-west3-b" instance_name: name of the new virtual machine (VM) instance. cpu_series: the type of CPU you want to use. Select one value from the CustomMachineType.CPUSeries enum. For example: CustomMachineType.CPUSeries.N2 core_count: number of CPU cores you want to use. memory: the amount of memory for the VM instance, in megabytes. Return: Instance object. """assertcpu_seriesin(CustomMachineType.CPUSeries.E2,CustomMachineType.CPUSeries.N1,CustomMachineType.CPUSeries.N2,CustomMachineType.CPUSeries.N2D,)custom_type=CustomMachineType(zone,cpu_series,memory,core_count)newest_debian=get_image_from_family(project="debian-cloud",family="debian-12")disk_type=f"zones/{zone}/diskTypes/pd-standard"disks=[disk_from_image(disk_type,10,True,newest_debian.self_link)]returncreate_instance(project_id,zone,instance_name,disks,str(custom_type))

REST

Using the instances.insert method, construct an instance creation request and specify the custom machineType values. Provide the memory in MB.

  • For N1 machine series, start with custom.
  • For E2-shared core, the NUMBER_OF_CPUs is micro, small, or medium.
  • To enable extended memory, include -ext at the end of the machine type.

POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instances

Example request body

 { "name": "exampleinstance", "machineType": "zones/us-central1-a/machineTypes/n2-custom-16-107520", "disks": [ { "boot": true, "diskSizeGb": 10, "initializeParams": { "sourceImage": "projects/debian-cloud/global/images/family/debian-11" } } ], "networkInterfaces": [ { "network": "global/networks/default" } ] } ``` 

You can similarly create preemptible instances and groups of instances with custom machine types.

Add extended memory during instance creation

To create a VM instance with extended memory, do the following:

Console

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

    Go to Create an instance

  2. In the Machine configuration pane, do the following:

    1. In the Zone list, select the zone where you want to host this VM. The Series list is filtered to include only the machine type families available in the selected zone.
    2. Select General-purpose.
      1. In the Series list, select N4, N2, N2D, N1 to create a custom machine type.
      2. To specify the number of vCPUs drag the Cores slider or enter the value in the field.
      3. To add extended memory, select Extend memory. To specify the amount of memory for the machine type, drag the Memory slider or enter the value in the field. The console displays an estimated cost for the VM as you edit the amount of vCPUs and memory.
  3. Optional: Specify other configuration options. For more information, see Configuration options during instance creation.

  4. To create and start the VM, click Create.

gcloud

Create a custom machine using the gcloud compute instances create command with the --machine-type option.

 gcloud compute instances create INSTANCE_NAME\ --machine-type=MACHINE_TYPE-custom-NUMBER_OF_VCPUS-AMOUNT_OF_MEMORY_MB

Replace the following:

  • INSTANCE_NAME: name of instance
  • MACHINE_TYPE: machine type, like N2
  • NUMBER_OF_VCPUS: number of vCPUs
  • AMOUNT_OF_MEMORY_MB: amount of memory in MB or GB

Here's an example using an N2 machine type with 48 vCPUs and 310 GB of memory in zone us-central1-a:

 gcloud compute instances create example-instance \ --zone=us-central1-a --machine-type=n2-custom-48-317440 

Go

Before trying this sample, follow the Go setup instructions in the Compute Engine quickstart using client libraries. For more information, see the Compute Engine Go API reference documentation.

To authenticate to Compute Engine, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

import("context""fmt""io"compute"cloud.google.com/go/compute/apiv1"computepb"cloud.google.com/go/compute/apiv1/computepb""google.golang.org/protobuf/proto")// createInstanceWithExtraMemWithoutHelper сreates new VM instances with extra memory// without using a CustomMachineType struct.funccreateInstanceWithExtraMemWithoutHelper(wio.Writer,projectID,zone,instanceName,cpuSeriesstring,coreCount,memoryint,)error{// projectID := "your_project_id"// zone := "europe-central2-b"// instanceName := "your_instance_name"// cpuSeries := "N1"// coreCount := 2 // number of CPU cores you want to use.// memory := 256 // the amount of memory for the VM instance, in megabytes.// The coreCount and memory values are not validated anywhere and can be rejected by the API.ctx:=context.Background()instancesClient,err:=compute.NewInstancesRESTClient(ctx)iferr!=nil{returnfmt.Errorf("NewInstancesRESTClient: %w",err)}deferinstancesClient.Close()mt:=fmt.Sprintf("zones/%s/machineTypes/%v-%v-%v-ext",zone,cpuSeries,coreCount,memory)inst:=&computepb.Instance{Name:proto.String(instanceName),Disks:[]*computepb.AttachedDisk{{InitializeParams:&computepb.AttachedDiskInitializeParams{DiskSizeGb:proto.Int64(10),SourceImage:proto.String("projects/debian-cloud/global/images/family/debian-12",),},AutoDelete:proto.Bool(true),Boot:proto.Bool(true),},},MachineType:proto.String(mt),NetworkInterfaces:[]*computepb.NetworkInterface{{Name:proto.String("global/networks/default"),},},}req:=&computepb.InsertInstanceRequest{Project:projectID,Zone:zone,InstanceResource:inst,}op,err:=instancesClient.Insert(ctx,req)iferr!=nil{returnfmt.Errorf("unable to create instance: %w",err)}iferr=op.Wait(ctx);err!=nil{returnfmt.Errorf("unable to wait for the operation: %w",err)}fmt.Fprintf(w,"Instance created\n")returnnil}

Java

Before trying this sample, follow the Java setup instructions in the Compute Engine quickstart using client libraries. For more information, see the Compute Engine Java API reference documentation.

To authenticate to Compute Engine, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

importcom.google.cloud.compute.v1.AttachedDisk;importcom.google.cloud.compute.v1.AttachedDiskInitializeParams;importcom.google.cloud.compute.v1.InsertInstanceRequest;importcom.google.cloud.compute.v1.Instance;importcom.google.cloud.compute.v1.InstancesClient;importcom.google.cloud.compute.v1.NetworkInterface;importcom.google.cloud.compute.v1.Operation;importjava.io.IOException;importjava.util.concurrent.ExecutionException;importjava.util.concurrent.TimeUnit;importjava.util.concurrent.TimeoutException;publicclassExtraMemoryWithoutHelper{publicstaticvoidmain(String[]args)throwsIOException,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.StringprojectId="your-google-cloud-project-id";// Name of the zone to create the instance in. For example: "us-west3-b".Stringzone="google-cloud-zone";// Name of the new virtual machine (VM) instance.StringinstanceName="instance-name";StringcpuSeries="N1";// Number of CPU cores you want to use.intcoreCount=2;// The amount of memory for the VM instance, in megabytes.intmemory=256;createInstanceWithExtraMemoryWithoutHelper(projectId,zone,instanceName,cpuSeries,coreCount,memory);}// Create VM instances with extra memory without using a CustomMachineType class and// return the created Instance.publicstaticvoidcreateInstanceWithExtraMemoryWithoutHelper(Stringproject,Stringzone,StringinstanceName,StringcpuSeries,intcoreCount,intmemory)throwsIOException,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. After completing all of your requests, call// the `instancesClient.close()` method on the client to safely// clean up any remaining background resources.try(InstancesClientinstancesClient=InstancesClient.create()){// The coreCount and memory values are not validated anywhere and can be rejected by the API.StringmachineType=String.format("zones/%s/machineTypes/%s-%s-%s-ext",zone,cpuSeries,coreCount,memory);AttachedDiskattachedDisk=AttachedDisk.newBuilder().setInitializeParams(// Describe the size and source image of the boot disk to attach to the instance.// The list of public images available in Compute Engine can be found here:// https://cloud.google.com/compute/docs/images#list_of_public_images_available_onAttachedDiskInitializeParams.newBuilder().setSourceImage(String.format("projects/%s/global/images/family/%s","debian-cloud","debian-11")).setDiskSizeGb(10).build())// Remember to set auto_delete to True if you want the disk to be deleted when you delete// your VM instance..setAutoDelete(true).setBoot(true).build();// Create the Instance object with the relevant information.Instanceinstance=Instance.newBuilder().setName(instanceName).addDisks(attachedDisk).setMachineType(machineType).addNetworkInterfaces(NetworkInterface.newBuilder().setName("global/networks/default").build()).build();// Create the insert instance request object.InsertInstanceRequestinsertInstanceRequest=InsertInstanceRequest.newBuilder().setProject(project).setZone(zone).setInstanceResource(instance).build();// Invoke the API with the request object and wait for the operation to complete.Operationresponse=instancesClient.insertAsync(insertInstanceRequest).get(3,TimeUnit.MINUTES);// Check for errors.if(response.hasError()){System.out.println("Instance creation failed!!"+response);return;}System.out.printf("Instance created : %s",instanceName);System.out.println("Operation Status: "+response.getStatus());}}}

Node.js

Before trying this sample, follow the Node.js setup instructions in the Compute Engine quickstart using client libraries. For more information, see the Compute Engine Node.js API reference documentation.

To authenticate to Compute Engine, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

/** * TODO(developer): Uncomment and replace these variables before running the sample. */// const projectId = 'YOUR_PROJECT_ID';// const zone = 'europe-central2-b';// const instanceName = 'YOUR_INSTANCE_NAME';// const cpuSeries = 'N1';// const coreCount = 2// const memory = 256// The coreCount and memory values are not validated anywhere and can be rejected by the API.constcompute=require('@google-cloud/compute');asyncfunctioncreateInstanceWithExtraMemWithoutHelper(){constinstancesClient=newcompute.InstancesClient();constmachineType=`zones/${zone}/machineTypes/${cpuSeries}-${coreCount}-${memory}-ext`;const[response]=awaitinstancesClient.insert({instanceResource:{name:instanceName,disks:[{initializeParams:{diskSizeGb:'64',sourceImage:'projects/debian-cloud/global/images/family/debian-11/',},autoDelete:true,boot:true,},],machineType,networkInterfaces:[{name:'global/networks/default',},],},project:projectId,zone,});letoperation=response.latestResponse;constoperationsClient=newcompute.ZoneOperationsClient();// Wait for the create operation to complete.while(operation.status!=='DONE'){[operation]=awaitoperationsClient.wait({operation:operation.name,project:projectId,zone:operation.zone.split('/').pop(),});}console.log('Instance created.');}createInstanceWithExtraMemWithoutHelper();

Python

Before trying this sample, follow the Python setup instructions in the Compute Engine quickstart using client libraries. For more information, see the Compute Engine Python API reference documentation.

To authenticate to Compute Engine, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

from__future__importannotationsimportreimportsysfromtypingimportAnyimportwarningsfromgoogle.api_core.extended_operationimportExtendedOperationfromgoogle.cloudimportcompute_v1defget_image_from_family(project:str,family:str)-> compute_v1.Image:""" Retrieve the newest image that is part of a given family in a project. Args: project: project ID or project number of the Cloud project you want to get image from. family: name of the image family you want to get image from. Returns: An Image object. """image_client=compute_v1.ImagesClient()# List of public operating system (OS) images: https://cloud.google.com/compute/docs/images/os-detailsnewest_image=image_client.get_from_family(project=project,family=family)returnnewest_imagedefdisk_from_image(disk_type:str,disk_size_gb:int,boot:bool,source_image:str,auto_delete:bool=True,)-> compute_v1.AttachedDisk:""" Create an AttachedDisk object to be used in VM instance creation. Uses an image as the source for the new disk. Args: disk_type: the type of disk you want to create. This value uses the following format: "zones/{zone}/diskTypes/(pd-standard|pd-ssd|pd-balanced|pd-extreme)". For example: "zones/us-west3-b/diskTypes/pd-ssd" disk_size_gb: size of the new disk in gigabytes boot: boolean flag indicating whether this disk should be used as a boot disk of an instance source_image: source image to use when creating this disk. You must have read access to this disk. This can be one of the publicly available images or an image from one of your projects. This value uses the following format: "projects/{project_name}/global/images/{image_name}" auto_delete: boolean flag indicating whether this disk should be deleted with the VM that uses it Returns: AttachedDisk object configured to be created using the specified image. """boot_disk=compute_v1.AttachedDisk()initialize_params=compute_v1.AttachedDiskInitializeParams()initialize_params.source_image=source_imageinitialize_params.disk_size_gb=disk_size_gbinitialize_params.disk_type=disk_typeboot_disk.initialize_params=initialize_params# Remember to set auto_delete to True if you want the disk to be deleted when you delete# your VM instance.boot_disk.auto_delete=auto_deleteboot_disk.boot=bootreturnboot_diskdefwait_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)ifoperation.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)raiseoperation.exception()orRuntimeError(operation.error_message)ifoperation.warnings:print(f"Warnings during {verbose_name}:\n",file=sys.stderr,flush=True)forwarninginoperation.warnings:print(f" - {warning.code}: {warning.message}",file=sys.stderr,flush=True)returnresultdefcreate_instance(project_id:str,zone:str,instance_name:str,disks:list[compute_v1.AttachedDisk],machine_type:str="n1-standard-1",network_link:str="global/networks/default",subnetwork_link:str=None,internal_ip:str=None,external_access:bool=False,external_ipv4:str=None,accelerators:list[compute_v1.AcceleratorConfig]=None,preemptible:bool=False,spot:bool=False,instance_termination_action:str="STOP",custom_hostname:str=None,delete_protection:bool=False,)-> compute_v1.Instance:""" Send an instance creation request to the Compute Engine API and wait for it to complete. Args: project_id: project ID or project number of the Cloud project you want to use. zone: name of the zone to create the instance in. For example: "us-west3-b" instance_name: name of the new virtual machine (VM) instance. disks: a list of compute_v1.AttachedDisk objects describing the disks you want to attach to your new instance. machine_type: machine type of the VM being created. This value uses the following format: "zones/{zone}/machineTypes/{type_name}". For example: "zones/europe-west3-c/machineTypes/f1-micro" network_link: name of the network you want the new instance to use. For example: "global/networks/default" represents the network named "default", which is created automatically for each project. subnetwork_link: name of the subnetwork you want the new instance to use. This value uses the following format: "regions/{region}/subnetworks/{subnetwork_name}" internal_ip: internal IP address you want to assign to the new instance. By default, a free address from the pool of available internal IP addresses of used subnet will be used. external_access: boolean flag indicating if the instance should have an external IPv4 address assigned. external_ipv4: external IPv4 address to be assigned to this instance. If you specify an external IP address, it must live in the same region as the zone of the instance. This setting requires `external_access` to be set to True to work. accelerators: a list of AcceleratorConfig objects describing the accelerators that will be attached to the new instance. preemptible: boolean value indicating if the new instance should be preemptible or not. Preemptible VMs have been deprecated and you should now use Spot VMs. spot: boolean value indicating if the new instance should be a Spot VM or not. instance_termination_action: What action should be taken once a Spot VM is terminated. Possible values: "STOP", "DELETE" custom_hostname: Custom hostname of the new VM instance. Custom hostnames must conform to RFC 1035 requirements for valid hostnames. delete_protection: boolean value indicating if the new virtual machine should be protected against deletion or not. Returns: Instance object. """instance_client=compute_v1.InstancesClient()# Use the network interface provided in the network_link argument.network_interface=compute_v1.NetworkInterface()network_interface.network=network_linkifsubnetwork_link:network_interface.subnetwork=subnetwork_linkifinternal_ip:network_interface.network_i_p=internal_ipifexternal_access:access=compute_v1.AccessConfig()access.type_=compute_v1.AccessConfig.Type.ONE_TO_ONE_NAT.nameaccess.name="External NAT"access.network_tier=access.NetworkTier.PREMIUM.nameifexternal_ipv4:access.nat_i_p=external_ipv4network_interface.access_configs=[access]# Collect information into the Instance object.instance=compute_v1.Instance()instance.network_interfaces=[network_interface]instance.name=instance_nameinstance.disks=disksifre.match(r"^zones/[a-z\d\-]+/machineTypes/[a-z\d\-]+$",machine_type):instance.machine_type=machine_typeelse:instance.machine_type=f"zones/{zone}/machineTypes/{machine_type}"instance.scheduling=compute_v1.Scheduling()ifaccelerators:instance.guest_accelerators=acceleratorsinstance.scheduling.on_host_maintenance=(compute_v1.Scheduling.OnHostMaintenance.TERMINATE.name)ifpreemptible:# Set the preemptible settingwarnings.warn("Preemptible VMs are being replaced by Spot VMs.",DeprecationWarning)instance.scheduling=compute_v1.Scheduling()instance.scheduling.preemptible=Trueifspot:# Set the Spot VM settinginstance.scheduling.provisioning_model=(compute_v1.Scheduling.ProvisioningModel.SPOT.name)instance.scheduling.instance_termination_action=instance_termination_actionifcustom_hostnameisnotNone:# Set the custom hostname for the instanceinstance.hostname=custom_hostnameifdelete_protection:# Set the delete protection bitinstance.deletion_protection=True# Prepare the request to insert an instance.request=compute_v1.InsertInstanceRequest()request.zone=zonerequest.project=project_idrequest.instance_resource=instance# Wait for the create operation to complete.print(f"Creating the {instance_name} instance in {zone}...")operation=instance_client.insert(request=request)wait_for_extended_operation(operation,"instance creation")print(f"Instance {instance_name} created.")returninstance_client.get(project=project_id,zone=zone,instance=instance_name)defcreate_custom_instances_extra_mem(project_id:str,zone:str,instance_name:str,core_count:int,memory:int)-> list[compute_v1.Instance]:""" Create 3 new VM instances with extra memory without using a CustomMachineType helper class. Args: project_id: project ID or project number of the Cloud project you want to use. zone: name of the zone to create the instance in. For example: "us-west3-b" instance_name: name of the new virtual machine (VM) instance. core_count: number of CPU cores you want to use. memory: the amount of memory for the VM instance, in megabytes. Returns: List of Instance objects. """newest_debian=get_image_from_family(project="debian-cloud",family="debian-12")disk_type=f"zones/{zone}/diskTypes/pd-standard"disks=[disk_from_image(disk_type,10,True,newest_debian.self_link)]# The core_count and memory values are not validated anywhere and can be rejected by the API.instances=[create_instance(project_id,zone,f"{instance_name}_n1_extra_mem",disks,f"zones/{zone}/machineTypes/custom-{core_count}-{memory}-ext",),create_instance(project_id,zone,f"{instance_name}_n2_extra_mem",disks,f"zones/{zone}/machineTypes/n2-custom-{core_count}-{memory}-ext",),create_instance(project_id,zone,f"{instance_name}_n2d_extra_mem",disks,f"zones/{zone}/machineTypes/n2d-custom-{core_count}-{memory}-ext",),]returninstances

REST

Using the instances.insert method, construct your instance creation request as you would to create an instance with custom memory. Include -ext, which indicates extended memory, when specifying the machineType values:

  • For all custom machine types append -ext to extend the memory up to the limit as described by that particular machine family.
 zones/ZONE/machineTypes//MACHINE_TYPE-NUMBER_OF_CPUS-AMOUNT_OF_MEMORY-ext 

Replace the following:

  • ZONE: Machine location
  • MACHINE_TYPE: Machine type, like N2 or E2 small
  • NUMBER_OF_CPUS: amount of vCPUs
  • AMOUNT_OF_MEMORY_MB: amount of memory in MB

For example, the following specifies an N2 machine type with 2 vCPUs and 20 GB of memory, note that you must convert the memory to MB:

zones/ZONE/machineTypes/n2-custom-2-20480-ext 

Add extended memory to an existing VM instance

To add more memory to an existing instance that uses a custom machine type, you must first stop the instance. After the instance stops, complete the following steps to add more memory to the VM.

Console

  1. In the Google Cloud console, go to the VM instances page.

    Go to VM instances

  2. Choose the stopped VM you want to modify from the VM list.

  3. Click Edit at the top of the page.

  4. In Machine configuration, select General-purpose.

  5. In the Machine type section, select Custom.

  6. Select the number of vCPUs you want.

  7. To add extended memory, select Extend memory and then specify the amount of memory you want.

  8. Save your changes.

gcloud

Use the gcloud compute instances stop command to stop the VM. Then use the gcloud compute instances set-machine-type command with the --custom-memory and --custom-extensions options to edit the machine's resources.

Save the changes and restart the VM.

Go

Before trying this sample, follow the Go setup instructions in the Compute Engine quickstart using client libraries. For more information, see the Compute Engine Go API reference documentation.

To authenticate to Compute Engine, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

import("context""fmt""io""strings"compute"cloud.google.com/go/compute/apiv1"computepb"cloud.google.com/go/compute/apiv1/computepb""google.golang.org/protobuf/proto")// modifyInstanceWithExtendedMemory sends an instance creation request// to the Compute Engine API and waits for it to complete.funcmodifyInstanceWithExtendedMemory(wio.Writer,projectID,zone,instanceNamestring,newMemoryint,)error{// projectID := "your_project_id"// zone := "europe-central2-b"// instanceName := "your_instance_name"// newMemory := 256 // the amount of memory for the VM instance, in megabytes.ctx:=context.Background()instancesClient,err:=compute.NewInstancesRESTClient(ctx)iferr!=nil{returnfmt.Errorf("NewInstancesRESTClient: %w",err)}deferinstancesClient.Close()reqInstance:=&computepb.GetInstanceRequest{Project:projectID,Zone:zone,Instance:instanceName,}instance,err:=instancesClient.Get(ctx,reqInstance)iferr!=nil{returnfmt.Errorf("unable to get instance: %w",err)}containsString:=func(s[]string,strstring)bool{for_,v:=ranges{ifv==str{returntrue}}returnfalse}if!(strings.Contains(instance.GetMachineType(),"machineTypes/n1-")||strings.Contains(instance.GetMachineType(),"machineTypes/n2-")||strings.Contains(instance.GetMachineType(),"machineTypes/n2d-")){returnfmt.Errorf("extra memory is available only for N1, N2 and N2D CPUs")}// Make sure that the machine is turned offif!containsString([]string{"TERMINATED","STOPPED"},instance.GetStatus()){reqStop:=&computepb.StopInstanceRequest{Project:projectID,Zone:zone,Instance:instanceName,}op,err:=instancesClient.Stop(ctx,reqStop)iferr!=nil{returnfmt.Errorf("unable to stop instance: %w",err)}iferr=op.Wait(ctx);err!=nil{returnfmt.Errorf("unable to wait for the operation: %w",err)}}// Modify the machine definition, remember that extended memory// is available only for N1, N2 and N2D CPUsmachineType:=instance.GetMachineType()start:=machineType[:strings.LastIndex(machineType,"-")]updateReq:=&computepb.SetMachineTypeInstanceRequest{Project:projectID,Zone:zone,Instance:instanceName,InstancesSetMachineTypeRequestResource:&computepb.InstancesSetMachineTypeRequest{MachineType:proto.String(fmt.Sprintf("%s-%v-ext",start,newMemory)),},}op,err:=instancesClient.SetMachineType(ctx,updateReq)iferr!=nil{returnfmt.Errorf("unable to update instance: %w",err)}iferr=op.Wait(ctx);err!=nil{returnfmt.Errorf("unable to wait for the operation: %w",err)}fmt.Fprintf(w,"Instance updated\n")returnnil}

Java

Before trying this sample, follow the Java setup instructions in the Compute Engine quickstart using client libraries. For more information, see the Compute Engine Java API reference documentation.

To authenticate to Compute Engine, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

importcom.google.api.gax.longrunning.OperationFuture;importcom.google.cloud.compute.v1.GetInstanceRequest;importcom.google.cloud.compute.v1.Instance;importcom.google.cloud.compute.v1.Instance.Status;importcom.google.cloud.compute.v1.InstancesClient;importcom.google.cloud.compute.v1.InstancesSetMachineTypeRequest;importcom.google.cloud.compute.v1.Operation;importcom.google.cloud.compute.v1.SetMachineTypeInstanceRequest;importcom.google.cloud.compute.v1.StopInstanceRequest;importjava.io.IOException;importjava.util.concurrent.ExecutionException;importjava.util.concurrent.TimeUnit;importjava.util.concurrent.TimeoutException;publicclassUpdateMemory{publicstaticvoidmain(String[]args)throwsIOException,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.StringprojectId="your-google-cloud-project-id";// Name of the zone to create the instance in. For example: "us-west3-b".Stringzone="google-cloud-zone";// Name of the new virtual machine (VM) instance.StringinstanceName="instance-name";// The amount of memory for the VM instance, in megabytes.intnewMemory=256;modifyInstanceWithExtendedMemory(projectId,zone,instanceName,newMemory);}// Modify an existing VM to use extended memory and return the modified Instance.publicstaticvoidmodifyInstanceWithExtendedMemory(Stringproject,Stringzone,StringinstanceName,intnewMemory)throwsIOException,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. After completing all of your requests, call// the `instancesClient.close()` method on the client to safely// clean up any remaining background resources.try(InstancesClientinstancesClient=InstancesClient.create()){// Create the get instance request object.GetInstanceRequestgetInstanceRequest=GetInstanceRequest.newBuilder().setProject(project).setZone(zone).setInstance(instanceName).build();Instanceinstance=instancesClient.get(getInstanceRequest);// Check the machine type.if(!(instance.getMachineType().contains("machineTypes/n1-")||instance.getMachineType().contains("machineTypes/n2-")||instance.getMachineType().contains("machineTypes/n2d-"))){System.out.println("extra memory is available only for N1, N2 and N2D CPUs");return;}// Make sure that the machine is turned off.if(!(instance.getStatus().equals(Status.TERMINATED.toString())||instance.getStatus().equals(Status.STOPPED.toString()))){StopInstanceRequeststopInstanceRequest=StopInstanceRequest.newBuilder().setProject(project).setZone(zone).setInstance(instanceName).build();OperationFuture<Operation,Operation>operation=instancesClient.stopAsync(stopInstanceRequest);Operationresponse=operation.get(3,TimeUnit.MINUTES);if(response.hasError()){System.out.printf("Unable to stop instance %s",response.getError());return;}}// Modify the machine definition. Note that extended memory// is available only for N1, N2 and N2D CPUs.StringmachineType=instance.getMachineType();Stringstart=machineType.substring(0,machineType.lastIndexOf("-"));// Create the machine type instance request object.SetMachineTypeInstanceRequestsetMachineTypeInstanceRequest=SetMachineTypeInstanceRequest.newBuilder().setProject(project).setZone(zone).setInstance(instanceName).setInstancesSetMachineTypeRequestResource(InstancesSetMachineTypeRequest.newBuilder().setMachineType(String.format("%s-%d-ext",start,newMemory)).build()).build();// Invoke the API with the request object and wait for the operation to complete.Operationresponse=instancesClient.setMachineTypeAsync(setMachineTypeInstanceRequest).get(3,TimeUnit.MINUTES);// Check for errors.if(response.hasError()){System.out.printf("Unable to update instance %s",response.getError());return;}System.out.println("Instance updated!");}}}

Node.js

Before trying this sample, follow the Node.js setup instructions in the Compute Engine quickstart using client libraries. For more information, see the Compute Engine Node.js API reference documentation.

To authenticate to Compute Engine, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

/** * TODO(developer): Uncomment and replace these variables before running the sample. */// const projectId = 'YOUR_PROJECT_ID';// const zone = 'europe-central2-b';// const instanceName = 'YOUR_INSTANCE_NAME';// const newMemory = 256;constcompute=require('@google-cloud/compute');asyncfunctionmodifyInstanceWithExtendedMemory(){constinstancesClient=newcompute.InstancesClient();const[instance]=awaitinstancesClient.get({project:projectId,zone,instance:instanceName,});if(!['machineTypes/n1-','machineTypes/n2-','machineTypes/n2d-'].some(type=>instance.machineType.includes(type))){thrownewError('extra memory is available only for N1, N2 and N2D CPUs');}// Make sure that the machine is turned offif(!['TERMINATED','STOPPED'].some(status=>instance.status===status)){const[response]=awaitinstancesClient.stop({project:projectId,zone,instance:instanceName,});letoperation=response.latestResponse;constoperationsClient=newcompute.ZoneOperationsClient();// Wait for the stop operation to complete.while(operation.status!=='DONE'){[operation]=awaitoperationsClient.wait({operation:operation.name,project:projectId,zone:operation.zone.split('/').pop(),});}}// Modify the machine definition, remember that extended memory// is available only for N1, N2 and N2D CPUsconststart=instance.machineType.substring(0,instance.machineType.lastIndexOf('-'));const[response]=awaitinstancesClient.setMachineType({project:projectId,zone,instance:instanceName,instancesSetMachineTypeRequestResource:{machineType:`${start}-${newMemory}-ext`,},});letoperation=response.latestResponse;constoperationsClient=newcompute.ZoneOperationsClient();// Wait for the update operation to complete.while(operation.status!=='DONE'){[operation]=awaitoperationsClient.wait({operation:operation.name,project:projectId,zone:operation.zone.split('/').pop(),});}console.log('Instance updated.');}modifyInstanceWithExtendedMemory();

Python

Before trying this sample, follow the Python setup instructions in the Compute Engine quickstart using client libraries. For more information, see the Compute Engine Python API reference documentation.

To authenticate to Compute Engine, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

from__future__importannotationsimportsysimporttimefromtypingimportAnyfromgoogle.api_core.extended_operationimportExtendedOperationfromgoogle.cloudimportcompute_v1defwait_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)ifoperation.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)raiseoperation.exception()orRuntimeError(operation.error_message)ifoperation.warnings:print(f"Warnings during {verbose_name}:\n",file=sys.stderr,flush=True)forwarninginoperation.warnings:print(f" - {warning.code}: {warning.message}",file=sys.stderr,flush=True)returnresultdefadd_extended_memory_to_instance(project_id:str,zone:str,instance_name:str,new_memory:int):""" Modify an existing VM to use extended memory. Args: project_id: project ID or project number of the Cloud project you want to use. zone: name of the zone to create the instance in. For example: "us-west3-b" instance_name: name of the new virtual machine (VM) instance. new_memory: the amount of memory for the VM instance, in megabytes. Returns: Instance object. """instance_client=compute_v1.InstancesClient()instance=instance_client.get(project=project_id,zone=zone,instance=instance_name)ifnot("n1-"ininstance.machine_typeor"n2-"ininstance.machine_typeor"n2d-"ininstance.machine_type):raiseRuntimeError("Extra memory is available only for N1, N2 and N2D CPUs.")# Make sure that the machine is turned offifinstance.statusnotin(instance.Status.TERMINATED.name,instance.Status.STOPPED.name,):operation=instance_client.stop(project=project_id,zone=zone,instance=instance_name)wait_for_extended_operation(operation,"instance stopping")start=time.time()whileinstance.statusnotin(instance.Status.TERMINATED.name,instance.Status.STOPPED.name,):# Waiting for the instance to be turned off.instance=instance_client.get(project=project_id,zone=zone,instance=instance_name)time.sleep(2)iftime.time()-start >=300:# 5 minutesraiseTimeoutError()# Modify the machine definition, remember that extended memory is available only for N1, N2 and N2D CPUsstart,end=instance.machine_type.rsplit("-",maxsplit=1)instance.machine_type=start+f"-{new_memory}-ext"# TODO: If you prefer to use the CustomMachineType helper class, uncomment this code and comment the 2 lines above# Using CustomMachineType helper# cmt = CustomMachineType.from_str(instance.machine_type)# cmt.memory_mb = new_memory# cmt.extra_memory_used = True# instance.machine_type = str(cmt)operation=instance_client.update(project=project_id,zone=zone,instance=instance_name,instance_resource=instance,)wait_for_extended_operation(operation,"instance update")returninstance_client.get(project=project_id,zone=zone,instance=instance_name)

REST

Use the instances.stop method then use the instances.setMachineType method to edit the machine type resources.

Once you stop the instance, edit the machineTypes option with your changes.

{

"name": "INSTANCE_NAME", "machineType": "zones/ZONE/machineTypes/MACHINE_TYPE",

... }

Replace the following:

+INSTANCE_NAME: The name of the VM +ZONE: The zone where your VMs reside +MACHINE_TYPE: The name of the VM 

What's next