Manage Google Compute Engine with Node.js

An Intro to the Node.js Client Library for Compute Engine

Google Compute Engine delivers virtual machines (VMs). Compute Engine supports scaling from single instances to global, load-balanced cloud computing.

On Compute Engine VMs, you can install any operating system or software and you have full control over the firewall. A Compute Engine VM is like your local workstation, except that it lives in the cloud. You can easily scale to more or fewer VMs at any moment as needed.

The easiest way to get started with Compute Engine is through the Cloud Console. You can configure and run virtual machines in the UI.

Image for post
Image for post
Configure Compute Engine instances in the Cloud Console.

But what if you want to automate the process of configuring and running VMs? That’s where the Compute Engine API comes in handy. It is a REST API for managing virtual machines on Google Cloud Platform. The@google-cloud/compute Client Library for Node reduces the amount of code you need to write and makes your application more robust. It’s easy to manage your VMs programmatically with Node.

The following snippet creates a new virtual machine with Ubuntu using the @google-cloud/compute library. Before you run it, enable the Google Compute Engine API. Set up authentication with a service account so you can access the API from your local workstation.

This minimal snippet does the following:

  • Load the client library,

If you check in the Cloud Console, you should now see a new virtual machine named ubuntu-instance.

Image for post
Image for post

Virtual Machine with Node.js Server

Image for post
Image for post
You can manage any VM with Node — including VMs that run Node.

The next example is more complex. It creates a virtual machine with HTTP access and installs Node and a Node Hello World app on startup. Copy the following script to a file named createVM.js and run it with npm install @google-cloud/compute && node createVM.js.

Copy to a file named createVM.js and run it with "npm install @google-cloud/compute && node createVM.js”.

If everything worked correctly, you will see a Hello World homepage when you navigate to the IP that the sample script logged to the terminal.

Image for post
Image for post

Troubleshooting

It takes about a minute to install Node, git, clone the sample application, and start the server. If the startup script didn’t work, you can use the Cloud Console to log in to the VM. Once connected, have a look at the log files in /var/log/syslog to debug the problem. Keep in mind that the startup script is run as root with a different home directory than your default user.

Image for post
Image for post
Cloud Console

The Cloud Console also shows the gcloud command so you can ssh into the VM from your terminal: gcloud compute --project "PROJECT_ID" ssh --zone "us-central1-a" "vm-with-node-server".

Try It!

There is more sample code on GitHub. Clone the repository and create and delete VMs with Node as described in the Readme. The API reference explains many more methods. Which do you find useful to manage your Compute Engine VMs? Don’t forget to delete your VMs when you’re done so they don’t incur charges.

Note on pricing: At the time of writing, a micro Compute Engine is part of the GCP free tier. Running the standard Compute Engine from the example above costs less than $0.05 per hour, or $25 per month. See pricing for details.

You need Node 8 or newer for the sample code. The examples were written for @google-cloud/compute version 0.10.0. You can install that specific version with npm install @google-cloud/compute@0.10.0.

Google Cloud - Community

Google Cloud community articles and blogs

Franziska Hinkelmann

Written by

Senior Engineer at Google. Node.js Monkey Patcher.

Google Cloud - Community

A collection of technical articles and blogs published or curated by Google Cloud Developer Advocates. The views expressed are those of the authors and don't necessarily reflect those of Google.

Franziska Hinkelmann

Written by

Senior Engineer at Google. Node.js Monkey Patcher.

Google Cloud - Community

A collection of technical articles and blogs published or curated by Google Cloud Developer Advocates. The views expressed are those of the authors and don't necessarily reflect those of Google.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store