This page provides the minimum information about the creation and the delation of VMs on Google Cloud Platform (GCP). A detailed version of the documentation is provided here.
Before starting
To be able to execute the instructions presented in this tutorial, you first need to:
- Create/have a account on Google Cloud Platform: https://console.cloud.google.com/
- Install the gcloud command line tool. Instructions to install gcloud on your system are available at https://cloud.google.com/sdk/docs/quickstarts.
We describe in this tutorial how to use the command line interface (gcloud) gcloud to create a VM on GCP. This tool is very useful as it allows automatizing many tasks that are often executed when working with GCP. The documentation about gcloud can be found here: https://cloud.google.com/sdk/gcloud/.
- A more detailed version of our documentation on VM creation is available here.
- Managing VMs through the GCP graphical interface is described here.
Creating a VM in GCP
This command creates a VM instance called my-first-instance
, that is of type f1-micro
(the smallest instance type: 1 CPU, 0.6 GB of memory), and running ubuntu-1804-lts
as operating system:
gcloud compute instances create my-first-instance --image-project=ubuntu-os-cloud --image-family=ubuntu-1804-lts --machine-type=f1-micro
To obtain the list of running instances in your project, run:
gcloud compute instances list
The new instance is started in the default region associated with your project. To obtain information about the default configuration of your project, use:
gcloud config configurations list
or simply:
gcloud config list
To learn about commands to obtain information about the available machine types and operating system images, have a look here.
Deleting a VM in GCP
To delete an instance, simply run:
gcloud compute instances delete [INSTANCE_NAME]
Connecting through SSH to VM instances
The first time you are using GCP, some configuration steps are required to be able to connect to the VMs you create using ssh
. A detailed version of the following explanations is available here.
We explain the main steps using gcloud
below. But associating a SSH key to a project can also be done through the web interface, as described here.
To enable SSH connections to VMs created in your project, run:
gcloud compute project-info add-metadata --metadata enable-oslogin=TRUE
To associate a public SSH key with your account, run:
gcloud compute os-login ssh-keys add --key-file [PATH_TO_KEY]/[PUB_KEY_FILE] --ttl 0
From this point, it is possible to use the associate private key to connect to the VMs you create using the following command:
ssh -i [PATH_TO_PRIVATE_KEY] [USERNAME]@[EXTERNAL_IP_ADDRESS]
Note that:
- The option
-i [PATH_TO_PRIVATE_KEY]
is only required is you have chosen to create a ssh key pair dedicated to connect to GCP, and that this key pair is not stored in the default$HOME\.ssh\
directory. - The public IP address (called EXTERNAL_IP) of the created instance is displayed by the create command. it can also be obtained using the command
gcloud compute instances list
. - Your
USERNAME
is your google login where.
and@
are replaced with_
(for example,thomas_ropars_gmail_com
), possibly truncated to 32 characters.
Important: Don’t forget to delete all VM instances before stopping working with GCP !!
The VMs you create (and this comment is more generally applicable to any resource that you allocate in the cloud) are not deleted until you explicitly ask for it to be deleted. There are not even deleted when you disconnect from GCP. As long as a VM is not deleted, you continue paying for it.