You can change the machine type of a stopped instance if it is not part of a managed instance group.
Change the machine types of your instances if your existing machine type is not a good fit for the workloads you run on that instance. You can change the machine type of an instance to adjust the number of vCPUs and memory as your workload changes. For example, you can start an instance with a smaller machine during setup, development, and testing and change the instance to use a larger machine type when you are ready for production workloads.
For instances that are not part of a managed instance group, you can change the machine type without affecting the instance's SSH keys, instance configurations such as instance metadata, and persistent disk data, which includes installed applications and application data.
If you need to change the machine type of instances within a managed instance group, read Updating managed instance groups.
Before you begin
- If you want to use the command-line examples in this guide:
- Install or update to the latest version of the gcloud command-line tool.
- 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 an instance.
- Understand machine types.
- If your instance uses an ephemeral external IP address, the IP address might change when you modify the instance's machine type. To preserve the IP address, promote it to a static external IP address.
Changing a machine type
You can only change the machine type of a stopped instance. An instance is
considered stopped only when the instance is in the TERMINATED
state. You
cannot change the machine type of a running instance.
Permissions required for this task
To perform this task, you must have the following permissions:
compute.instances.setMachineType
on the instance
Console
In the Google Cloud Console, go to the VM instances page.
In the Name column, click the name of the instance for which you want to change machine type.
From the instance details page, complete the following steps:
- Click the Stop button to stop the instance, if you have not stopped it yet.
After the instance stops, click the Edit button at the top of the page.
Under the Machine configuration section, select the machine type you want to use, or create a custom machine type.
Save your changes.
gcloud
Stop the instance by using the
gcloud compute instances stop
command.Change the machine type by using the
gcloud compute instances set-machine-type
command.gcloud compute instances set-machine-type INSTANCE_NAME \ --machine-type NEW_MACHINE_TYPE
Replace the following:
INSTANCE_NAME
: the instance containing the machine type to change.NEW_MACHINE_TYPE
: the new machine type for the instance. The machine type can be one of the following:Predefined machine type, such as
e2-standard-2
.Custom machine type. For example, a custom machine type with 4 vCPUs and 2 GB of memory can be specified as
--machine-type e2-custom-4-2048
.
API
Stop the instance by using the
instances.stop
method.Change the machine type by using the
instances.setMachineType
method:POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/INSTANCE_ZONE/instances/INSTANCE_NAME/setMachineType { machineType: "zones/MACHINE_TYPE_ZONE/machineTypes/NEW_MACHINE_TYPE" }
Replace the following:
PROJECT_ID
: the name of the project.INSTANCE_ZONE
: the zone containing the instance.INSTANCE_NAME
: the instance containing the machine type to change.MACHINE_TYPE_ZONE
: the zone containing the machine type.NEW_MACHINE_TYPE
: the new machine type for the instance. The machine type can be one of the following:Predefined machine type, such as
e2-standard-2
.Custom machine type. For example, a custom machine type with 4 vCPUs and 2 GB of memory can be specified as
e2-custom-4-2048
.
Moving 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 instance, or that you update your
services and applications to run on the smaller machine types.
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 virtual machine instance counts toward the sustained use discount of the new category.
For example, assume you have an instance 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 virtual machine instance 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 instance remains
running on m1-ultramem-40
for at least a 25% of the rest of the month.
Best practices
It is good practice to 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 virtual machine instance, you can take a persistent disk snapshot and use it to start a second virtual machine instance with the new machine type to confirm that the instance will start up successfully.
If you have a second persistent disk attached to your instance, make sure
it is added to the /etc/fstab
file so that it is automatically mounted
when the instance reboots.