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 in the general-purpose machine family for the N and E machine series only. Custom machine types are not available for the C and Tau machine series. You can create a custom VM on the N4, N2, N2D, E2, or N1 machine types.
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.
Before you begin
- Read the machine families documentation.
-
If you haven't already, set up authentication.
Authentication is
the process by which your identity is verified for access to Google Cloud services and APIs.
To run code or samples from a local development environment, you can authenticate to
Compute Engine as follows.
Select the tab for how you plan to use the samples on this page:
Console
When you use the Google Cloud console to access Google Cloud services and APIs, you don't need to set up authentication.
gcloud
-
Install the Google Cloud CLI, then initialize it by running the following command:
gcloud init
- Set a default region and zone.
Terraform
To use the Terraform samples on this page in a local development environment, install and initialize the gcloud CLI, and then set up Application Default Credentials with your user credentials.
- Install the Google Cloud CLI.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
Create local authentication credentials for your user account:
gcloud auth application-default login
For more information, see Set up authentication for a local development environment.
Go
To use the Go samples on this page in a local development environment, install and initialize the gcloud CLI, and then set up Application Default Credentials with your user credentials.
- Install the Google Cloud CLI.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
Create local authentication credentials for your user account:
gcloud auth application-default login
For more information, see Set up authentication for a local development environment.
Java
To use the Java samples on this page in a local development environment, install and initialize the gcloud CLI, and then set up Application Default Credentials with your user credentials.
- Install the Google Cloud CLI.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
Create local authentication credentials for your user account:
gcloud auth application-default login
For more information, see Set up authentication for a local development environment.
Node.js
To use the Node.js samples on this page in a local development environment, install and initialize the gcloud CLI, and then set up Application Default Credentials with your user credentials.
- Install the Google Cloud CLI.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
Create local authentication credentials for your user account:
gcloud auth application-default login
For more information, see Set up authentication for a local development environment.
PHP
To use the PHP samples on this page in a local development environment, install and initialize the gcloud CLI, and then set up Application Default Credentials with your user credentials.
- Install the Google Cloud CLI.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
Create local authentication credentials for your user account:
gcloud auth application-default login
For more information, see Set up authentication for a local development environment.
Python
To use the Python samples on this page in a local development environment, install and initialize the gcloud CLI, and then set up Application Default Credentials with your user credentials.
- Install the Google Cloud CLI.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
Create local authentication credentials for your user account:
gcloud auth application-default login
For more information, see Set up authentication for a local development environment.
REST
To use the REST API samples on this page in a local development environment, you use the credentials you provide to the gcloud CLI.
Install the Google Cloud CLI, then initialize it by running the following command:
gcloud init
For more information, see Authenticate for using REST in the Google Cloud authentication documentation.
-
Required roles
To get the permissions that you need to 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.
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 only available on custom machine types. Predefined machine types are not supported.
- 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 committed use discounts.
- N4 and N2D machine types are available only in select regions and zones.
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 series | vCPU limit | Memory limit |
---|---|---|
N4 | 80 | 640 GB |
N2 | 128 | 640 GB |
N2D | 224 | 768 GB |
E2 | 32 | 128 GB |
N1 | 96 | 624 GB |
If you require more memory, use one of the memory-optimized machine types, which lets you create VMs with a total memory of up to 12 TB per VM.
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
In the Google Cloud console, go to the Create an instance page.
Select the Region and Zone where you want to host the VM.
In the Machine configuration section, select General-purpose.
- In the Series list, select a machine series to create a custom machine; N4, N2, N2D, E2, and N1 offer custom machine types.
- In the Machine type section, select Custom.
- 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 VM as you change the number of vCPUs and memory.
Continue to create the VM.
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-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
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
Alternatively, you can specify a custom machine type by using the custom
options: --custom-cpu
, --custom-memory
, --custom-vm-type
,
and --custom-extensions
. To configure extended memory,
which lets you specify a higher amount of memory than the maximum ratio of
memory to vCPUs, increase the value of AMOUNT_OF_MEMORY_MB
and
append -ext
to the machine type name.
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 must be an integer, so if you want to
specify increments of 0.25 GB for memory, convert that value to 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.- In the Google Cloud console, go to the VM instances page.
- Click Create instance.
- Specify the parameters you want.
- 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.
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.
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.
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.
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
, ormedium
. - 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
In the Google Cloud console, go to the Create an instance page.
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.
In the Machine configuration section, select General-purpose.
- In the Series list, select N4, N2, N2D, E2, or N1 to create a custom machine type.
- To specify the number of vCPUs drag the Cores slider or enter the value in the field. For E2 shared-core, check the box.
- 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.
Continue creating the VM.
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-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
For custom E2 shared-core machine types, use the --machine-type
option,
and indicate the machine type: micro
, small
, or medium
and enter
the amount of memory as MB or GB.
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.
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.
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.
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.
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. - E2 shared-core, specify
micro
,small
, ormedium
.
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, you must first stop the instance. After the instance stops, complete the following steps to add more memory to the VM.Console
In the Google Cloud console, go to the VM instances page.
Choose the stopped VM you want to modify from the VM list.
Click Edit at the top of the page.
In Machine configuration, select General-purpose.
In the Machine type section, select Custom.
Select the number of vCPUs you want.
To add extended memory, select Extend memory and then specify the amount of memory you want.
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.
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.
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.
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.
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
- Learn more about machine families.
- After creating a VM, step through a tutorial to start using a VM instance.
- Learn how to connect to your VM instance.