10

As there isn't any direct option to change machine type and i have to create a new instance. What are the steps to do so that the configuration/software that I had installed remain same ?

Sunil Garg
  • 14,608
  • 25
  • 132
  • 189
  • 2
    Possible duplicate of [How to change a machine type on Google Compute Engine?](http://stackoverflow.com/questions/27373257/how-to-change-a-machine-type-on-google-compute-engine) – Rohit Gupta Nov 13 '15 at 00:36

3 Answers3

10

Updated answer

I'm not sure when this launched, but it is now possible to change the machine type, without deleting instance and re-creating it from scratch, per the docs:

You can change the machine type of a stopped instance if it is not part of a managed instance group.

Here's how you can do this with gcloud:

$ gcloud compute instances set-machine-type INSTANCE_NAME \
      --machine-type NEW_MACHINE_TYPE

Also, note the caveat about moving to smaller instance types:

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.


Original answer (outdated)

You can't change the instance type of a VM on-the-fly. To upgrade or downgrade the VM type, you should do the following:

  1. VERY IMPORTANT: make sure to not delete VM's boot disk while shutting down the VM; see this answer for details

  2. shut down the VM cleanly while taking into account the information from step #1 if you're doing this via Google Developers Console or via gcloud on the CLI by using the --keep-disks option or by having already set those disks to not auto-delete as described in this answer:

     gcloud compute instances delete VM \
          --keep-disks=all \
          --project $PROJECT
          --zone $ZONE
    

Note that --keep-disks accepts any of the following options: boot, data, or all. In your case, you want at least boot but if you've attached other disks, you want to specify all. See the docs for more info.

  1. create a new VM and choose a larger/smaller instance type: again, this can be done via Google Developers Console or via gcloud on the CLI and instead of creating a new boot disk, select the boot disk from the original VM, e.g.,

     gcloud compute instances create $VM \
          --disk name=${DISK_NAME},boot=yes \
          --machine-type ${MACHINE_TYPE} \
          --project $PROJECT
          --zone $ZONE
    

See the docs for more info.

Misha Brukman
  • 12,938
  • 4
  • 61
  • 78
10

1) Delete the instance that you want to upgrade by keeping its boot disk.

  gcloud compute instances delete <instance-name> --keep-disks boot

2) Now create image from this boot disk

  gcloud compute images create <any-image-name> --source-disk <instance-name>

3) Now Check Images list

 gcloud compute images list

4) Now Create new instance from developer console or using gcloud compute

and select your image as boot disk.

5) Done.

Here is the link.

Sunil Garg
  • 14,608
  • 25
  • 132
  • 189
  • Is this equivalent to replicating the entire disk (including custom PHP scripts and local SQL data)? (My goal is to make a new instance with expanded API privileges. That can only be done on a new instance. But I can only create an image from a detached disk.) – Leif Jones Aug 06 '16 at 14:47
9

As of today, this ability can be seen on Google Compute Engine. You will need to stop the instance and then edit the instance.. which will give you a drop-down menu for the Machine Types

https://cloud.google.com/sdk/gcloud/reference/alpha/compute/instances/set-machine-type?hl=en

Raghav
  • 513
  • 3
  • 10
  • ...has emerged from beta and can be done programmatically after stopping this instance: [`gcloud compute instances set-machine-type`](http://stackoverflow.com/a/38950577/462302) – aculich Aug 15 '16 at 07:46