Creating a Google Cloud compute instance from the terminal

I have been working with Amazon Web Services for a while, but recently I started moving the applications from the project I am currently working on to Google Cloud. I was not thinking of Google Cloud as my production platform at first, but since I am using Firebase as the database for the mobile application, I thought I could take some advantage by moving everything there.

Believing that by moving from Amazon Web Services as a good decision for the product, I started my endeavor in learning more about how to do it properly. Although I am willing to share many things about it, I have decided to start with this simple example and tell you how to create a Google Cloud machine using the terminal.

Well, there are a few things you need in advance:

  1. A configured Google Cloud account
  2. The Google Cloud SDK installed
  3. The commands to create our machine

First two items

First, you need to sign in to the Google Cloud here. If you already have a Google account, this should happen smoothly. Whether it is your first time, you are entitled to have $300 to spend over a year. Second, you should download the SDK for your preferred OS and install it. 

The commands to create our machine

Now that everything is set, we can start creating our machine in the cloud. Open your terminal and start by logging into the Google Cloud with the command:

gcloud auth login

Entering the command will take you to the Google Account login page. There are ways of logging in without opening the browser, but it requires extra steps that I will not cover here. To be able to create the compute instance first it is necessary to create and set up a project where the compute instance will live under. Just in case you already have more than one project, you can skip the first command.

gcloud projects create unique-project-name
gcloud config set project unique-project-name

After executing the above commands, we have our project almost set. Though, in order to list the compute instances or create one, it is necessary to link your billing account to the project in the Google Cloud console. You can find it on the left side. Once the billing is linked, the creation can be carried on. First lets list all the compute instances you might have:

gcloud compute instances list

Whether you just started your account or project moments ago, you might have none. Well, now is the part where all this post is leading to, the compute instance creation. A few things are important to consider: first, the region (cloud physical location) where you want to create it. I will choose the europe-west4 since I am living in The Netherlands. You can choose the one that fits you best. Execute the following command to get a list of the available regions:

gcloud compute regions list

The second important thing to consider is the size of your machine. It can vary from micro with shared CPU and low RAM to large machines with many virtual CPUs and lots of RAM. You must decide based on your product needs. I will choose a micro compute instance. You can list the available machine types with the command filtering by zone within the region we choose above:

gcloud compute machine-types list --filter "zone:(europe-west4-a)"

The third important thing to consider is the image you want to start the instance with. You can choose between different flavors of Linux and also Windows. In this case, I will choose the Debian 9 distribution. Execute the command below to list the available images:

gcloud compute images list

Okay, now we have chosen the zone within the region, the size of the instance and the image, we can finally create our machine. There are many options besides the three I am using like disk, CPU and network configurations, but this is subject for another post. Now, to create our instance, the following command should be executed:

gcloud compute instances create unique-instance-name \
  --zone europe-west4-a \
  --machine-type f1-micro \ 
  --image-project debian-cloud \ 
  --image-family debian-9

Alright, after a little while, we can see that our machine was created successfully. It will return some information like internal and external IP. Once the machine gets started, you can access it through SSH by using the command:

gcloud compute ssh unique-instance-name

Great! I am glad you are still here. You have created your first or more Google Cloud machine instances using almost only your terminal. Now, whether you decide to move from any other cloud service really depends on your product needs. It will not happen in all cases, but for the project, I am working on it fits perfectly.

See you!