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.
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
Virtual Machine with Node.js Server
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.
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.
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".
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.
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 @firstname.lastname@example.org.