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.

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,
  • Create a compute client,
  • Start the VM.

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

Virtual Machine with Node.js Server

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.

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.

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 Platform - Community

A collection of technical articles published or curated by Google Cloud Platform 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 Platform - Community

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

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade