Google Cloud is a cloud platform from Google. It offers several services for computing, storage and application development that run on Google’s hardware. In this post we will learn together how to create and launch a NodeJS application with Google Compute Engine, but before we begin please make sure you already have account on google cloud platform.

If you are planning to learn about how to use google cloud features or you just want to try it, at the moment google offer trial version that give USD 300 of credit for the next 12 months (credit card required), you can read the detail here.

Once your account already activated then you can follow these step:

Login Into Google Cloud Console

Open google cloud homepage with your favourite browser then click console button at the top right corner.

login to console

login to console

Create Project

After logged in you will be redirected to console dashboard and see the following screen:

console dashboard

console dashboard

In google cloud every instance belongs to a project, to create a new project just click on create button. Type your project name and pay attention to which organisation your project will be registered.

create new project

create new project

If this is your first time login to Google Cloud console and you already subscribe to Google Cloud free tier program then you will have a default project created automatically. You can use this project instead of creating a new one.

Launch VM

We are going to launch a virtual machine (also known as instance) to running our NodeJS application, this machine is fully controllable by us so we can choose every properties that we want like number of cpu, memory, disk size, etc. At project’s dashboard expand the left menu then choose Compute Engine > VM Instances.

project dashboard

project dashboard

vm instance menu

vm instance menu

It may take minutes to get Google Compute Engine is ready, when it was ready click on create button.

create instance

create instance

On the next screen you will see a form to configure your machine properties, these are the explanation of each field:

  • Name. Fill it with your machine’s name, in this example I’ll give it name ‘rumbia-example-instance’.
  • Zone. Select to which zone your instance will be available, the zone determine what computing resources are available and where your data is store. For now I’ll select ‘us-central1-c’ which is physically located in United States. For further reading about zone please click here.
  • Machine Type. This option is to choose what type of machine you want to have, you can select a pre-defined machine type or build your custom machine. Every machine type’s has different cost regarding resource that attached to it, you can see the cost estimation on the right to help you choose wisely.
  • Boot Disk. Boot disk determine which operating system or application image that your machine will running on. If you select ‘OS Images’ boot disk Google Cloud offer two kind of disk: standard and SSD. In this example I’ll use ‘Debian GNU/Linux 8 (jessie)’ OS image.
  • Identity and API Access. Select service account you want to use to call Google Cloud API’s and set level permission for it.
  • Firewall. Set which incoming traffic is allowed to your machine, you can allow either HTTP traffic or HTTPS traffic or two of them.
  • If you need more configuration for your machine click on Management, disks, networking, SSH keys link at the bottom to expand the form.
setup vm

setup instance

Once your instance created you will see it has IP public assigned automatically, this IP given by Google Cloud so we can access our machine from the internet.

create vm

vm created

test ping

test connection

If you haven’t sign up for free trial yet then you may see a notification “Sign up for the free trial to start using Compute Engine”, just click on the sign up for a free trial button and follow the instruction.

SSH Connect

Now our instance is up already with Debian jessie as it operating system, let’s try to connect to it through ssh. There are three ways to connect: directly from the browser (cloud ssh), from terminal using gcloud command-line tool and from terminal by ssh key-pair that you generate. I’d prefer the last method because it’s more comfortable to me, the steps are:

Generate a new ssh key-pair

Open terminal and running the following code (Linux and Mac):

replace [USERNAME] with your google username.

generate ssh key-pair

generate ssh key-pair

Run the following in terminal to restrict access to ssh private key:

Add ssh key-pair to project metadata

Expand the left menu then choose Compute Engine > Metadata. After the page already opened, click tab SSH Keys then click edit so you can modify the public ssh keys in your project.

metadata menu

metadata menu

Copy the contents of public key to the list of ssh keys

Get content of your public key by running the following command from terminal:

Copy the output from the cat command and paste it as a new item in the list of SSH keys, to make the text area enabled you must click on Add item button first, don’t forget to click save button after that.

copy-paste public key content

copy-paste public key content

Use ssh command to connect to instance

Run the following command to connect to instance using your generated private key.

for example:

ssh connect

ssh connect