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. With custom machine types, you can create VM instances with as little as 1 vCPU and up to 96 vCPUs, or any even number of vCPUs in between. You can also configure up to 8 GB of memory per vCPU.
Custom machine types are available in the general-purpose machine family. When you create a custom VM, you're deploying a custom VM from the E2, N2, N2D, or N1 machine family.
Custom VMs are ideal in the following scenarios:
- Workloads that aren't a good fit for the predefined virtual machine 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.
-
Set up authentication.
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 from 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 Google 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 from 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 Google 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 from 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 Google 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 from 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 Google 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 from 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 Google 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 from 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 Google 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
-
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 standard machine types. See VM instance pricing for the pricing information of custom machine types from each machine family.
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.
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 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 VM instance, make sure you read and understand the custom specifications for creating this machine type.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, click N1 in First Generation for N1 custom machine types or E2, N2, or N2D for Second Generation 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
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 you want.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 compute instances create
command
and include one of the following options:
- The
--custom-cpu
,--custom-memory
, and--custom-vm-type
flags. - The
--machine-type=n2d-custom-NUMBER_OF_CPUS-NUMBER_OF_MB
flag. - The
--machine-type=e2-custom-NUMBER_OF_CPUS-NUMBER_OF_MB
flag.
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
--machine-type=e2-custom-SHARED_CORE_MACHINE_SIZE-NUMBER_OF_MB
flag.
For example:
gcloud compute instances create example-instance \
--machine-type=e2-custom-medium-NUMBER_OF_MB
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 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 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 you want.NUMBER_OF_MB
: The amount of memory, in MB.
For example:
gcloud compute instances create example-instance --machine-type=e2-custom-6-3072
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.