Select the tab for how you plan to use the samples on this page:
When you use the Google Cloud console to access Google Cloud services and APIs, you don't need to set up authentication.
After installing the Google Cloud CLI, initialize it by running the following command:
gcloudinit
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
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.
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
To initialize the gcloud CLI, run the following command:
gcloudinit
If you're using a local shell, then create local authentication credentials for your user account:
gcloudauthapplication-defaultlogin
You don't need to do this if you're using Cloud Shell.
If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.
For more information, see Set up authentication for a local development environment.
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.
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
To initialize the gcloud CLI, run the following command:
gcloudinit
If you're using a local shell, then create local authentication credentials for your user account:
gcloudauthapplication-defaultlogin
You don't need to do this if you're using Cloud Shell.
If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.
For more information, see Set up authentication for a local development environment.
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.
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
To initialize the gcloud CLI, run the following command:
gcloudinit
If you're using a local shell, then create local authentication credentials for your user account:
gcloudauthapplication-defaultlogin
You don't need to do this if you're using Cloud Shell.
If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.
For more information, see Set up authentication for a local development environment.
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.
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
To initialize the gcloud CLI, run the following command:
gcloudinit
If you're using a local shell, then create local authentication credentials for your user account:
gcloudauthapplication-defaultlogin
You don't need to do this if you're using Cloud Shell.
If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.
For more information, see Set up authentication for a local development environment.
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.
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
To initialize the gcloud CLI, run the following command:
gcloudinit
If you're using a local shell, then create local authentication credentials for your user account:
gcloudauthapplication-defaultlogin
You don't need to do this if you're using Cloud Shell.
If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.
For more information, see Set up authentication for a local development environment.
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.
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
To initialize the gcloud CLI, run the following command:
gcloudinit
If you're using a local shell, then create local authentication credentials for your user account:
gcloudauthapplication-defaultlogin
You don't need to do this if you're using Cloud Shell.
If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.
For more information, see Set up authentication for a local development environment.
To use the REST API samples on this page in a local development environment, you use the credentials you provide to the gcloud CLI.
After installing the Google Cloud CLI, initialize it by running the following command:
gcloudinit
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
For more information, see Authenticate for using REST in the Google Cloud authentication documentation.
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:
The following permissions are required to create a VM with a custom machine type:
compute.instances.setMachineType
on the VM compute.instances.create
on the projectcompute.images.useReadOnly
on the imagecompute.snapshots.useReadOnly
on the snapshotcompute.instanceTemplates.useReadOnly
on the instance templatecompute.networks.use
on the projectcompute.addresses.use
on the projectcompute.networks.useExternalIp
on the projectcompute.subnetworks.use
on the project or on the chosen subnetcompute.subnetworks.useExternalIp
on the project or on the chosen subnetcompute.instances.setMetadata
on the projectcompute.instances.setTags
on the VMcompute.instances.setLabels
on the VMcompute.instances.setServiceAccount
on the VMcompute.disks.create
on the projectcompute.disks.use
on the diskcompute.disks.useReadOnly
on the diskYou might also be able to get these permissions with custom roles or other predefined roles.
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.
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.
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 | 80 | 864 GB |
N2D | 96 | 768 GB |
N1 | 96 | 624 GB |
If you require more memory, use one of the memory-optimized machine types.
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.
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.
In the Google Cloud console, go to the Create an instance page.
In the Machine configuration pane, do the following:
Optional: Specify other configuration options. For more information, see Configuration options during instance creation.
To create and start the instance, click Create.
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:
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:
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:
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
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.
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.
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.
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.
Using the instances.insert
method, construct an instance creation request and specify the custom machineType
values. Provide the memory in MB.
micro
, small
, or medium
.-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.
In the Google Cloud console, go to the Create an instance page.
In the Machine configuration pane, do the following:
Optional: Specify other configuration options. For more information, see Configuration options during instance creation.
To create and start the VM, click Create.
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:
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
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.
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.
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.
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.
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:
-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:
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
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.
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.
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.
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.
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.
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.
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
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-04-25 UTC.