Time to “Hello, World”: running Node.js on VMs, Containers, Apps, and Functions — Part 4

Building on top of application platform

This article is a part of the series: Part 1 (overview), Part 2 (VMs), Part 3 (containers), Part 4 (apps) and Part 5 (functions & summary).

“Hello, World” on App Engine

App Engine is a framework and computing platform for developing and hosting web applications in Google-managed data centers. App Engine offers automatic scaling — as the number of requests increases, it automatically allocates more resources for the application to handle the additional demand.

A quick look at the docs indicates that App Engine provides standard and flexible environment. For the task at hand (running a “Hello World” application in Node.js), I decided to go with the standard environment based on its ability to scale down to 0 instances when there is no traffic which is ideal for free or very low cost experimentation. (Note: Node.js runtime is available on App Engine standard through an Early Access Program at the time of writing.)

Plan of attack:

  1. Create and configure an App Engine project
  2. Deploy and test the application
  3. Assess scaling

1) Create and configure an App Engine project

When I navigated to the App Engine tab for the first time, I was greeted with this nice tutorial:

App Engine project creation wizard

What I wanted, however, was to go through the steps manually, to better understand how App Engine works. So I exited the tutorial, opened Cloud Shell, and copied my existing code (from GKE step):

$ mkdir helloworld-gae
$ cd helloworld-gae/
$ cp ../server-express.js server-express.js
$ vim server-express.js
// Changed “Hello World from GKE!” to “Hello World from GAE!”

App Engine relies on a proprietary file format to configure applications; the app.yaml file contains CPU, memory, network and disk resources, scaling configuration (automatic or manual), and general settings. I copy-pasted the very first sample configuration from the App Engine’s docs page (only ½ GB memory and one instance… ideal for my “Hello World” application):

$ vim app.yaml
runtime: nodejs8
manual_scaling:
   instances: 1
resources:
   cpu: 1
   memory_gb: 0.5
   disk_size_gb: 10

And, since I built my application with Express, I need to add it to the list of dependencies:

$ npm init
$ npm install express --save

2) Deploy and test the app

$ gcloud app deploy
...
Step #0: Application detection failed: Error: node.js checker: Neither "start" in the "scripts" section of "package.json" nor the "server.js" file were found.

Ah… Looks like I need to add the “start” script to fire up the Node server:

$ vim package.json
{
   "name": "helloworld-gae",
   "version": "1.0.0",
   "description": "",
   "main": "server-express.js",
   "scripts": {
      "test": "echo \"Error: no test specified\" && exit 1",
      "start": "node server-express.js"
   },
   "author": "",
   "license": "ISC",
   "dependencies": {
   "express": "^4.16.3"
   }
}

Let’s try again… success!

$ gcloud app deploy
Updating service [default] (this may take several minutes)...
Deployed service [default] to [https://helloworld-mtthw.appspot.com]

And test:

$ gcloud app browse

3) What’s involved with running apps at scale

App Engine provides automatic scaling and manual scaling options. Since I configured App Engine to scale manually (manual_scaling in my application’s app.yaml file), I need to implement the following change to make App Engine automatically manage and auto-scale the instances:

automatic_scaling:
   min_num_instances: 1
   max_num_instances: 15
   cool_down_period_sec: 180
   cpu_utilization:
      target_utilization: 0.6

Most of these are self-explanatory, except the cool_down_period_sec perhaps. This parameter specifies the number of seconds that the autoscaler should wait before it starts collecting information from a new instance (which prevents it from attempting to collect unreliable information while the instance is initializing).

Further review of documentation revealed that App Engine comes fully equipped with features required to run web applications at scale, including memcache, a NoSQL document database, task scheduling, version management and traffic splitting for new version rollouts and A/B testing.

Although there are a lot of other considerations that go into building and running a real production application, App Engine comes with robust documentation including design best practices to help you scale your applications to a really high load.


4) Time check & getting started resources

Image source

Building my first application and playing with App Engine for a few hours helped me understand why it has been around for so long (first released in 2008, App Engine just celebrated its 10 year anniversary). It’s a fully-managed application development platform with a ton of useful out-of-the-box functionality. Figuring out the syntax of GAE’s app.yaml configuration file was literally the trickiest thing in standing up a “Hello World” application. App Engine arguably is one of the easiest ways to get a complete web application up and running.

Useful getting started resources:


Continue on to Part 5 for building with FaaS (Cloud Functions), summary and concluding thoughts.