
Managing the VM
There are several operations that you will want to do as you start working with a VM on Google Compute, such as starting instances, stopping instances, resizing and modifying disks, and taking snapshots. We go over the most important ones:
- Start and shut down the VM:
$ gcloud compute instances start sparrow --project packt-gcp
$ gcloud compute instances stop sparrow --project packt-gcp
- Check the VM status:
$ gcloud compute instances list --project packt-gcp
The instance we started with is an f1-micro with not-enough CPU, RAM, or disk space for a real-world data science project. We want to change the underlying machine and augment its disk space. But, before that, we should take a snapshot of our current machine as a backup. If anything goes wrong, we'll be able to restore the instance from the snapshot:
- Taking a snapshot of a VM:
$ gcloud compute disks snapshot [DISK_NAME]
- In our case, let's call our disk sparrow-backup as we run:
- Changing the machine type, you first need to stop your instance with $ gcloud compute instances stop sparrow --project packt-gcp. Once that's done, changing the machine type is doable with the generic command:
$ gcloud compute instances set-machine-type INSTANCE --machine-type MACHINE-TYPE
- In our case, if we want to change the type to n1-standard-1 (3.75 GB memory and 1 vCPU), we should run this:
- While we're at it, we would also like to resize the underlying disk from 10 GB to 100 GB:
- Another important setting is to make sure that the disk will not be deleted when the instance is deleted:
This is an important parameter that can also be set in the compute engine console by unselecting Delete boot disk when instance is deleted when creating or editing an instance:

- Instance configuration: The entire instance configuration is available via $ gcloud compute instances describe sparrow.
- Creating the right VM from scratch: in this all these parameters are available when you create a VM from scratch. Running the following command will create a new n1-standard-1 instance named hummingbird in the europe-west1-c zone, when running on Ubuntu 17.04, with a 100 GB disk also named hummingbird. Note that this instance is pre-emptible (--preemptible) and the disk will persist once the instance is deleted (--no-boot-disk-auto-delete):
We can verify that we now have two instances in our project:
To keep our resources under control, we should delete this new instance with:
Note that if you have set up a different zone as default either in the config setup or as an environment variable, you need to specify the zone of the instance before you can delete it; otherwise, a resource not found error message will be generated.