Running Node.js on App Engine
Surely anyone who has developed a project to run on App Engine (Google’s PaaS) could have found a situation like this:
- The platform is very interesting.
- The services and libraries provided solve many common problems.
- The configuration required is minimal, Google does the rest.
- The way in which it scales is wonderful. It allows that no request is left unattended no matter the amount of requests.
- And many other great advantages of the product.
But surely he/she could have found or wondered how to solve any of the following:
- Using a language apart of the 4 possible (Python, Java, Go, PHP).
- Installing a library that was not available.
- Using an unsupported framework.
In either case, the solution is called: Managed VMs.
In this article I will present the Managed VMs and then show you an example of how to use Node.js on App Engine.
So, what are the Managed VMs?
Managed VMs are one of the components of the Google Cloud offer that lets you run applications on App Engine using configurable Compute Engine Virtual Machines.
What did we achieve that?
Basically, we retain all the benefits of App Engine and add the flexibility provided by Compute Engine.
We avoid the constraints of the sandbox environments. We can also configure only some modules of our application to use managed VMs and everything works with containers technology.
How to configure an application to use managed VMs?
In the app.yaml file (the file that describes our configuration for App Engine) we must modify the runtime parameter and add the vm parameter as follows:
The vm parameter is simple and indicates that we will use our own instances in App Engine.
The runtime parameter is a bit more complex. We can use a standard runtime (based on an environment provided by Google that can be modified and extended), or a custom runtime, based on an environment fully defined by the user.
The question now is obvious,
How we define an environment?
Here is where the containers I mentioned before appear.
The container technology adopted by Google Cloud is Docker. Docker can build images automatically by reading the instructions from a Dockerfile. A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image.
So, when we use a custom runtime, App Engine will search a Dockerfile where we specify all necessary steps to define our environment. Installing libraries, frameworks, dependencies, everything needed to run our application.
Putting all together. Finally running Node.js on App Engine.
To illustrate the execution of an application with Managed VMs, I propose one that queries the recent tweets using a hashtag and displays them on the screen using Node.js as the server technology.
I will not delve into the application code because it exceeds the objective of the article. It is a basic node application with some dependencies.
What I’ll explain is the app.yaml file content.
In this case, the runtime is custom but provided by Google. Google supplies base images for a few stacks and one of them is Node.js. Check them here: https://cloud.google.com/appengine/docs/managed-vms/custom-runtimes#base_images
Basically, using one of the runtimes provided by google, we avoid the need to write the dockerfile. Saying that our runtime is nodejs, we are using a preassembled dockerfile for node that performs the following tasks:
- Extends an App Engine base image.
- Installs some dependencies.
- Installs node.
- Sets environment variables.
- Runs npm start.
You can check it here:
Or see it here:
With that only change in the app.yaml file we changed the standard environment of App Engine by one with support for Node.js.
Managed VMs allow us to modify the base technology stack of an application running on App Engine without losing the possibility to create new immutable instances when App Engine considers the need. (When it needs to scale for example).
You can see the complete application code in the following repository:
Contribute to nodengine development by creating an account on GitHub.github.com
Any question, feedback or improvement do not doubt to contact me.
The latest Tweets from Leonardo Pittelli (@leopittelli). Software Engineer. Full stack developer. Google Developer…twitter.com