If your VM does not have a local SSD and is not part of a managed instance group (MIG), you can change the machine type of your VM after stopping it

If your existing machine type is not a good fit for the workloads you run on your VM, change the machine type of that VM. You can change the machine type of a VM to adjust the number of vCPUs and memory as your workload changes. For example, you can start a VM with a smaller machine during setup, development, and testing and change the VM to use a larger machine type when you are ready for production workloads

For VMs that do not have a local SSD and are not part of a MIG, you can change the machine type without affecting the following resources:
- The VM's SSH keys
- The VM configurations, such as VM metadata
- The VM's persistent disk data, including installed applications and application data
If you need to change the machine type of VMs within a MIG, read about updating MIGs

If your VM has a local SSD, you cannot stop the VM to change its machine type unless you force it to stop, which causes you to lose all of the data on the local SSD. For details, see Adding local SSDs

## Before you begin
- If you want to use the command-line examples in this guide, do the following:
- Install or update to the latest version of the Google Cloud CLI

- Set a default region and zone

- If you want to use the API examples in this guide, set up API access

- Understand how to stop a VM

- Understand machine types

- If your VM uses an ephemeral external IP address, the IP address might change when you modify the VM's machine type. To preserve the IP address, promote it to a static external IP address

- If you want to use the newest generation CPUs in the machine family, see Migrating to third generation machine series. If you want to change the machine type to an Arm machine type, see Migrate your workload from an existing VM to a new VM

## Billing implications
Each machine type is billed at a different rate, so make sure you understand
the pricing implications of changing machine
types. For example, an
e2-standard-2 machine type costs more than an
e2-micro machine type

Changing a machine type might also affect the sustained use discounts for that VM. Sustained use discounts are calculated separately for different categories in the same region. If you change machine types so that the new machine type is in a different category, the subsequent running time of the VM counts toward the sustained use discount of the new category

For example, assume you have a VM with
n2-standard-2 machine type
running for half a month. You then decide to change the machine type to
m1-ultramem-40. Once you make that change, Compute Engine starts
counting the running time of the VM towards the sustained
use discount of the memory-optimized vCPU and memory category

On your bill, you would see a sustained use discount applied to the
n2-standard-2 machine type from before you made the machine type change, and
a separate sustained use discount for
m1-ultramem-40, if your VM remains
running on
m1-ultramem-40 for at least a 25% of the rest of the month

## Best practices
Here are some best practices to help you successfully change the VM machine type

Make regular backups of your persistent disk data using snapshots. Consider taking a snapshot of your persistent disk data before you change the machine type. If you want to make sure the new machine type is able to support the data on the existing VM, you can take a persistent disk snapshot and use it to start a second VM with the new machine type to confirm that the VM starts up successfully. Add additional persistent disks to the. If you have additional persistent disks attached to your VM, make sure they are added to the
/etc/fstabfile
/etc/fstabfile so that they are automatically mounted when the VM reboots

Create a reservation before changing the machine type. To avoid errors related to resource availability, create Compute Engine reservations for the new machine types when they are availableto reserve them within a zone. Reservations help ensure that the resources are available when you need them

To create a reservation, complete the following steps:
Create a reservation (or identify existing reservations) with identical properties as the planned VMs. The reservation's VM count needs to be equal to or greater than the number of VMs that you want to change. Optionally, to prevent other VMs from consuming this reservation, use the
specificReservationRequiredoption

Verify that the planned VMs will be able to consume the reservation:
- Verify that the desired VMs have the correct reservation affinity

- Immediately before changing the VMs, ensure the reservation has enough capacity

## Change a machine type
You can only change the machine type of a stopped VM. A VM is
considered stopped only when the VM is in the
TERMINATED state. You
cannot change the machine type of a running VM

If you are upgrading your machine type to the latest generation, review Migrate to third generation machine series before proceeding

 Permissions required for this task
To perform this task, you must have the following permissions:
compute.instances.setMachineTypeon the VM
 Console
In the Google Cloud console, go to the
VM instancespage

In the
Namecolumn, click the name of the VM for which you want to change machine type

From the
VM instance detailspage, complete the following steps:
- If the VM is running, click
Stopto stop the VM

- To edit the VM, click
Edit

In the
Machine configurationsection, select the machine type that you want to use, or create a custom machine type

To save your changes, click
Save

- If the VM is running, click
 gcloud
Stop the VM by using the
gcloud compute instances stopcommand:
gcloud compute instances stop
VM_NAME
Replace
with the VM containing the machine type to change

VM_NAME
Change the machine type by using the
gcloud compute instances set-machine-typecommand:
gcloud compute instances set-machine-type
VM_NAME\ --machine-type NEW_MACHINE_TYPE
Replace
with the new machine type for the VM. The machine type can be one of the following:
NEW_MACHINE_TYPE
A predefined machine type
For example:
e2-standard-2
For example, you can specify
--machine-type e2-custom-4-2048to set a custom machine type that has 4 vCPUs and 2 GB of memory

-
 API
Stop the VM by using the
instances.stopmethod:
POST httpscompute.googleapis.com/compute/v1/projects/
PROJECT_ID/zones/ ZONE/instances/ VM_NAME/stop
Replace the following:
: the project ID
PROJECT_ID
: the zone containing the VM
ZONE
: the VM containing the machine type to change
VM_NAME
-
Change the machine type by using the
instances.setMachineTypemethod:
POST httpscompute.googleapis.com/compute/v1/projects/
PROJECT_ID/zones/ ZONE/instances/ VM_NAME/setMachineType
In the body of the request, provide the updated
machineType:
{ machineType: "zones/
MACHINE_TYPE_ZONE/machineTypes/ NEW_MACHINE_TYPE" }
Replace the following:
: the zone containing the machine type
MACHINE_TYPE_ZONE
: the new machine type for the VM
NEW_MACHINE_TYPE
The machine type can be one of the following:
A predefined machine type
For example:
e2-standard-2
For example, you can specify
--machine-type e2-custom-4-2048to set a custom machine type that has 4 vCPUs and 2 GB of memory

-
-
## Move to a smaller machine type
If you move from a machine type with more resources to a machine type with fewer
resources, such as moving from a
e2-standard-8 machine type to a
e2-standard-2, you could run into hardware resource issues or performance
limitations because smaller machine types are less powerful than larger machine
types. Make sure that your new machine type is able to support any applications
or services that are currently running on the VM, or that you update your
services and applications to run on the smaller machine types

Review the
*rightsizing recommendations* before changing the machine type. For
information about Compute Engine sizing recommendations,
see
Applying machine type recommendations for VM instances

## Migrate to a third generation machine series
When the requirements are met, you can use a new generation VM in a machine
series by changing the machine type—for example, from
m1-ultramem-160
to
m3-ultramem-128

Newer generation machine types might not support the same features and interfaces as your current machine type. Before starting the migration process, review the machine type information for the newer generation–for example, M3

The new machine type might not be available in all the same regions and zones as your current VM instance. Review the available regions and zones for the newer generation machine series, and adjust your disaster recovery plans as needed

To change the machine type for your VM instance to a third generation machine type, you can use either of the following approaches

 Create and migrate to a new VM instance
In this procedure, you create a new VM instance, then move your workload to the new VM

For details on how to complete this procedure, see Migrate your workload from an existing VM to a new VM

 Change the machine type to a new machine type
In this procedure, you verify that your current VM instance can be updated to use the new machine type, and then change the machine type

Verify your current VM instance uses a supported operating system version. If the version is not supported, then follow Migrate your workload from an existing VM to a new VM

Verify your applications and programs can support a different network or storage interface, such as gVNIC or NVMe

- To update your instance to use gVNIC, see Using Google Virtual NIC

- To prepare to use the NVMe interface and drivers, see Choosing an interface

Save the image to the boot disk

Follow the procedure described in Change a machine type

## What's next
- Learn about machine type recommendations

- Learn about managed instance groups

- Learn about patterns for scalable and resilient apps.