Creating a VM Instance with a custom machine type

Compute Engine offers predefined machine types that you can use when you create a virtual machine (VM) instance. A predefined machine type has a preset number of vCPUs and amount of memory and are charged at a set price as described on the All pricing page.

If predefined machine types 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 only for general-purpose machine types. When you create a custom machine type, you're effectively deploying a custom machine type from the E2, N2, N2D, or N1 machine type family.

Custom machine types are ideal for the following scenarios:

  • Workloads that aren't a good fit for the predefined machine types that are available to you.
  • Workloads that require more processing power or more memory but don't need all upgrades that are provided by the next machine type level.

Before you begin

Specifications

General

  • The total memory for a custom machine type must be a multiple of 256 MB. For example, 6.9 GB is not supported, but 6.75 GB and 7 GB are supported.

  • VMs with custom machine types have the same persistent disk capacity limitations as instances with predefined machine types.

  • Custom machine types are subject to the same persistent disk limits as general-purpose machine types. Currently, the maximum total persistent disk size for each machine type per instance is 257 TB and the maximum number of persistent disks is 128.

E2 custom machine types

  • E2 machine types support predefined platforms with Intel or AMD EPYC Rome processors. You can create E2 custom machine types with vCPUs in multiples of 2, up to 32 vCPUs. The minimum acceptable number of vCPUs is 2.

  • By default, the memory per vCPU that you can select for a custom machine type is determined by the machine type family. For E2 machine types, select 0.5 GB to 8 GB per vCPU, inclusive.

Examples of invalid machine types

  • 1 vCPU, 1024 MB of memory. Invalid because the vCPU count is too small. E2 custom machine types require a minimum of 2 vCPUs.
  • 32 vCPUs, 1 GB of total memory. Invalid because the ratio of memory to vCPUs is below 0.5 GB per vCPU.

Examples of valid machine types

  • 32 vCPUs, 16 GB of total memory. Valid because the total number of vCPUs is even and the total memory is an acceptable ratio of memory to vCPU.
  • 2 vCPUs, 8 GB of total memory. Valid because it has 2 vCPUs, which is the minimum value, and the total memory is a multiple of 256 MB. The amount of memory per vCPU is also within the acceptable range of 0.5 GB to 8 GB per vCPU.

N2 custom machine types

  • For N2 machine types, which support only the Cascade Lake platform, you can create custom machine types with 2 to 80 vCPUs. You can create N2 custom machine types with vCPUs in multiples of 2, up to 30 vCPUs. For machine types with at least 32 vCPUs, you must select a vCPU count that is a multiple of 4. So, for example, 32, 36, and 40 vCPUs are all valid, but 38 is invalid. The minimum number of acceptable vCPUs is 2.

  • By default, the memory per vCPU that you can select for a custom machine type is determined by the machine type family. For N2 machine types, select between 0.5 GB and 8.0 GB per vCPU, inclusive. Select larger amounts of memory by enabling extended memory.

  • N2 machines are available only in select zones. To learn more, see regions and zones.

Examples of invalid machine types

  • 2 vCPUs, 0.8 GB of total memory. Invalid because the total memory is less than the minimum 0.5 GB for an N2 machine type.
  • 34 vCPUs, 34 GB of total memory. Invalid because the total number of vCPUs is not divisible by 4.
  • 1 vCPU, 1024 MB of memory. Invalid because the vCPU count is too small. N2 custom machine types require a minimum of 2 vCPUs.

Examples of valid machine types

  • 36 vCPUs, 18 GB of total memory. Valid because the total number of vCPUs is even and the total memory is a multiple of 256 MB. The amount of memory per vCPU is 0.5 GB, which satisfies the minimum requirement. Because the number of vCPUs is larger than 32 vCPUs, the number of vCPUs must be divisible by 4.
  • 2 vCPUs, 7 GB of total memory. Valid because it has 2 vCPUs, which is the minimum value, and the total memory is a multiple of 256 MB. The amount of memory per vCPU is also within the acceptable range of 0.5 GB to 8 GB per vCPU.

N2D custom machine types

  • The maximum number of vCPUs allowed for a custom machine type is determined by the machine type family you choose. For N2D machine types, which support the AMD EPYC Rome platform, you can deploy custom machine types with 2 to 96 vCPUs.

  • You can create N2D custom machines with 2, 4, 8, or 16 vCPUs. After 16, you can increment the number of vCPUs by 16, up to 96 vCPUs. The minimum acceptable number of vCPUs is 2.

  • By default, the memory per vCPU that you can select for a custom machine type is determined by the machine type family. For N2D machine types, select between 0.5 GB and 8.0 GB per vCPU, inclusive. Select larger amounts of memory by enabling extended memory.

  • N2D machines are available only in select zones. To learn more, see regions and zones.

Examples of invalid machine types

  • 2 vCPUs, 0.4 GB of total memory. Invalid because the total memory is less than the minimum 0.5 GB for an N2D machine type.
  • 34 vCPUs, 34 GB of total memory. Invalid because the total number of vCPUs is not divisible by 16.
  • 1 vCPU, 1024 MB of memory. Invalid because the vCPU count is too small. N2D custom machine types require a minimum of 2 vCPUs.

Examples of valid machine types

  • 32 vCPUs, 16 GB of total memory. Valid because the total number of vCPUs is even and the total memory is a multiple of 256 MB. The amount of memory per vCPU is 0.5 GB, which satisfies the minimum requirement. Because the number of vCPUs is larger than 8 vCPUs, the number of vCPUs must be divisible by 16.
  • 2 vCPUs, 7 GB of total memory. Valid because it has 2 vCPUs, which is the minimum value, and the total memory is a multiple of 256 MB. The amount of memory per vCPU is also within the acceptable range of 0.5 GB to 8 GB per vCPU.

N1 custom machine types

  • The maximum number of vCPUs allowed for a custom machine type is determined by the machine type family you choose. For N1 machine types:

    • If you deploy in a zone that supports the Skylake CPU platform, you can create custom machine types with up to 96 vCPUs.
    • If you deploy in zones that support the Broadwell, Haswell, or Ivy Bridge CPU platforms, you can deploy custom machine types with up to 64 vCPUs.

    To see which platforms are available in each zone, see regions and zones.

  • You can create N1 machine types with 2 or more vCPUs. The number of vCPUs must be a multiple of 2, up to 96 vCPUs for the Skylake CPU platform or up to 64 vCPUs for Broadwell, Haswell, or Ivy Bridge CPU platforms.

  • By default, the memory per vCPU that you can select for a custom machine type is determined by the machine type family. For N1 machine types, select between 0.9 GB and 6.5 GB per vCPU, inclusive. Select larger amounts of memory by enabling extended memory.

Examples of invalid machine types

  • 1 vCPU, 0.2 GB of total memory. Invalid because the total memory is less than the minimum 0.9 GB for an N1 machine type.
  • 1 vCPU, 0.9 GB of total memory. Invalid because the total memory must be a multiple of 256 MB. For 1 vCPU, use a minimum memory of 1024 MB.

Examples of valid machine types

  • 32 vCPUs, 29 GB of total memory. Valid because the total number of vCPUs is even and the total memory is a multiple of 256 MB. The amount of memory per vCPU is 0.9 GB, which satisfies the minimum requirement.
  • 1 vCPU, 1 GB of total memory. Valid because it has one vCPU, which is the minimum value, and the total memory is a multiple of 256 MB. The amount of memory per vCPU is also within the acceptable range of 0.9 GB to 6.5 GB per vCPU.

Pricing

Google charges for custom machine types based on the number of vCPUs and memory hours that the instance uses. This is different from how predefined machine types are charged. Review the pricing page before you create a custom machine type. For example, to review pricing for custom E2 machine types, see E2 custom vCPUs and memory.

VM instances running a custom machine type 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 machine types.

Expressing 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 gcloud command-line tool, you can provide the total memory for a VM instance in gigabytes or megabytes. However, the gcloud tool 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 instance with a custom machine type

Before you create a VM instance, make sure you read and understand the Specifications for creating a custom machine type.

Console

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

    Go to Create an instance

  2. Under Zone, select the zone where you want to host this instance. The Series list is filtered to include only the machine type families available in the selected zone.

  3. Under Machine configuration, select General-purpose.

  4. From the Series list, select N1 under First Generation for N1 custom machine types or E2, N2, or N2D for Second Generation custom machine types.

  5. From the Machine type list, select Custom.

  6. To specify the number of vCPUs and the amount of memory for the VM 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.

  7. Save your changes and continue to create the VM.

gcloud

The gcloud command for custom machine types differs slightly depending on the machine type family.

For N1 machine types, use the gcloud compute instances create command and include one of the following options:

  • The --custom-cpu and --custom-memory flags.
  • The --machine-type=custom-[NUMBER_OF_CPUS]-[NUMBER_OF_MB] flag.

For example, the following command would create an instance running an N1 machine type with 4 vCPUs and 5 GB of total memory:

gcloud compute instances create example-instance \
    --custom-cpu=4 --custom-memory=5

For N2 machine types, use the gcloud compute instances create command and include one of the following options:

  • The --custom-cpu, --custom-memory, and --custom-vm-type flags.
  • The --machine-type=n2-custom-number-of-cpus-number-of-mb flag.

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

To specify an incremental value, such as 2.5 GB of memory, convert the value to MB and supply the value followed by the MB suffix. The following example creates an instance running an N2 machine type:

gcloud compute instances create example-instance \
    --custom-cpu=6 --custom-memory=3072MB --custom-vm-type=n2

Alternatively, you can specify a custom machine type using the following format:

gcloud compute instances create example-instance \
    --machine-type=n2-custom-number-of-cpus-number-of-mb

Replace the following:

  • number-of-cpus: the number of CPUs.
  • number-of-mb: the amount of memory, in MB.

For example:

gcloud compute instances create example-instance --machine-type=n2-custom-6-3072

For N2D or E2 machine types, use the gcloud beta compute instances create command and include one of the following options:

  • The --custom-cpu, --custom-memory, and --custom-vm-type flags.
  • The --machine-type=e2-custom-number-of-cpus-number-of-mb flag.
  • The --machine-type=n2d-custom-number-of-cpus-number-of-mb flag.

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

To specify an incremental value, such as 2.5 GB of memory, convert the value to MB and supply the value followed by the MB suffix. The following example creates an instance running an E2 machine type:

gcloud beta compute instances create example-instance \
    --custom-cpu=6 --custom-memory=3072MB --custom-vm-type=e2

Alternatively, you can specify a custom machine type by using the following format:

gcloud beta compute instances create example-instance \
    --machine-type=e2-custom-number-of-cpus-number-of-mb

Replace the following:

  • number-of-cpus: the number of CPUs.
  • number-of-mb: the amount of memory, in MB.

For example:

gcloud compute instances create example-instance --machine-type=e2-custom-6-3072

API

In the API, create your instance creation request as you would normally, but when specifying a machineType value, use one of the following formats. Replace number-of-cpus with the number of CPUs and amount-of-memory-mb with the amount of memory you want. Provide memory in MB units.

  • For N1 machine types, use:

    zones/zone/machineTypes/custom-number-of-cpus-amount-of-memory-mb
    
  • For N2 machine types, use:

    zones/zone/machineTypes/n2-custom-number-of-cpus-amount-of-memory-mb
    
  • For N2D machine types, use:

    zones/zone/machineTypes/n2d-custom-number-of-cpus-amount-of-memory-mb
    
  • For E2 machine types, use:

    zones/zone/machineTypes/e2-custom-number-of-cpus-amount-of-memory-mb
    

For example, the following URL would create an instance with 4 vCPUs and 5 GB (5120 MB) of memory:

zones/us-central1-f/machineTypes/custom-4-5120

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

Add extended memory to a machine type

Depending on the machine type, each machine type can have up to a specific amount of memory by default. For example, if you create a VM with an N1 custom machine type, the VM can have up to 6.5 GB of memory per vCPU. For N2 custom machine types, this number goes up to 8 GB of memory per vCPU.

For some workloads, this might not be enough memory. At 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 add memory to a machine type with no limitations per vCPU. You can add extended memory up to certain limits based on the machine type:

  • For N1 machine types, you can add up to a total of 624 GB of memory per VM.
  • For N2 machine types, you can add up to a total of 640 GB of memory per VM.
  • For N2D machine types, you can add up to a total of 768 GB of memory per VM.
  • Extended memory is not available for E2 machine types.

If you require more memory, you must use one of the mega-memory machine types, which let you create instances with a total of 1.4 TB per VM instance.

Determining whether 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.

Pricing

When using a custom machine type, any memory up to and including the default amount per vCPU is charged at the N1 custom machine type pricing or the N2 custom machine type pricing. For more memory than the default, Compute Engine charges for extended memory based on the N1 extended custom memory pricing or the N2 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.

For more information, see sustained use discounts for custom machine types and extended custom memory pricing.

Limitations

  • You can add extended memory only to custom machine types. Predefined machine types are not supported.
  • There is no limit to the memory per vCPU that you can add, but there is a maximum amount of memory you can add to each VM instance. For N1 machine types, you can add up to 624 GB of memory. For N2 machine types, you can add up to 640 GB of memory. For N2D machines types, you can add up to 768 GB of memory.
  • N2 and N2D machine types are available only in select regions and zones.
  • You must specify memory in 256-MB increments.
  • Extended memory does not qualify for committed use discounts.

Add extended memory during instance creation

To create a VM instance with extended memory, follow the instructions below.

Console

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

    Go to Create an instance

  2. Under Zone, select the zone where you want to host this instance. The Series list is filtered to include only the machine type families available in the selected zone.

  3. Under Machine configuration, select General-purpose.

  4. From the Series drop-down list, select N1 under First Generation for N1 custom machine types or N2 or N2D for Second Generation custom machine types.

  5. From the Machine type drop-down list, select Custom.

  6. Specify 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 and continue to create the VM.

gcloud

For N1 machine types, use the gcloud command and the instances create sub-command, followed by the --custom-extensions flag to enable extended memory. Then, specify the total memory that you want with the --custom- memory parameter.

gcloud compute instances create example-instance  \
    --custom-extensions --custom-cpu=2 --custom-memory=15

For N2 machine types, use the gcloud command with the instances create sub-command. Set the --custom-extensions, --custom-cpu, and --custom-memory flags, followed by the --custom-vm-type flag, to specify the N2 machine type:

gcloud compute instances create example-instance \
    --custom-extensions --custom-cpu=2 --custom-memory=16 \
    --custom-vm-type=n2

For N2D machine types, use the gcloud beta command with the instances create sub-command. Set the --custom-extensions, --custom-cpu, and --custom-memory flags, followed by the --custom-vm-type flag, to specify the N2D machine type:

gcloud beta compute instances create example-instance \
    --custom-extensions --custom-cpu=2 --custom-memory=16 \
    --custom-vm-type=n2d

API

In the API, construct your instance creation request as you would normally, but when specifying a machineType value, use one of the following formats:

  • For N1 machine types, use:

    zones/zone/machineTypes/custom-number-of-cpus-amount-of-memory-ext
    
  • For N2 machine types, use:

    zones/zone/machineTypes/n2-custom-number-of-cpus-amount-of-memory-ext
    
  • For N2D machine types, use:

    zones/zone/machineTypes/n2d-custom-number-of-cpus-amount-of-memory-ext
    

Replace the following:

  • number-of-cpus: The number of vCPUs.
  • amount-of-memory: The amount of memory, in MB, you want.

For example, the following specifies an N2 machine type with 2 vCPUs and 20 GB of memory:

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

Add extended memory to an existing VM instance

To add more memory to an existing instance, you must first stop the instance. After the instance stops, follow the instructions below 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 instance you want to modify from the instances list.

  3. Click Edit at the top of the page.

  4. Under Machine configuration, select General-purpose.

  5. From the Machine type list, 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

Using the gcloud command-line tool, use the instances set-machine-type sub-command, followed by the --custom-extensions flag paired with the existing --custom-memory parameter.

For example, to add 2 GB memory to a VM instance named example-instance, which currently has an N1 machine type with 2 vCPU, 13 GB, in the us-central1-b zone, use the following command:

gcloud compute instances set-machine-type example-instance \
   --zone=us-central1-b --custom-cpu=2 \
   --custom-memory=15 --custom-extensions

API

In the API, make a POST request to the setMachineType method with the desired machine type in the request body. Use one of the following formats:

  • For N1 machine types, use:

    zones/zone/machineTypes/custom-number-of-cpus-amount-of-memory-ext
    
  • For N2 machine types, use:

    zones/zone/machineTypes/n2-custom-number-of-cpus-amount-of-memory-ext
    
  • For N2D machine types, use:

    zones/zone/machineTypes/n2d-custom-number-of-cpus-amount-of-memory-ext
    

Replace the following:

  • number-of-cpus: the number of vCPUs.
  • amount-of-memory: the amount of memory, in MB, you want.

For example, the following request uses a machine type with 2 vCPUs and 15 GB of memory:

zones/zone/machineTypes/custom-2-15360-ext

For example:

POST https://compute.googleapis.com/compute/v1/projects/project-id/zones/zone/instances/instance-name/setMachineType

{
    machineType: "zones/us-central1-f/machineTypes/custom-2-15360-ext"
}

What's next