Grant Timmerman
Aug 7 · 3 min read

⚡+☁️+👟!

In this post, we’ll learn how to deploy a Node 12 application using the Functions Framework and Cloud Run.

Cloud Run + Node

Let’s say this.

  • You learn that the Node Foundation releases cutting-edge Node 12.
  • It includes cool new features like more performant async/await, faster HTTP parsing, and private fields. It’s still in development but you want to use it for your services.
  • But Google Cloud Functions doesn’t support Node 12 yet.😢

So whatchagunnado?

  • Blog about it?

…Maybe

  • Or use Cloud Run 💨👟

Let’s learn how to do that!

Deploying a Node 12 Function to Cloud Run

Node is a JavaScript runtime built on Chrome’s V8 engine.

Cloud Run (http://cloud.run) is a managed, serverless computing platform that abstracts away infrastructure management, so you can focus on building great applications.

Together, you can build scalable services atop Google Cloud in minutes.

First, Install Node 12

TJ has an incredibly simple Node module called n for managing Node versions (i.e. switching between 10 and 12). Install the global CLI called n:

sudo npm i n -g

Then switch your Node version to Node 12:

sudo n 12

Verify your Node version

node -v # v12.7.0

Create a Node 12 Function

Create a new package.json:

Hard to make it simpler than that!

Create a class in greeter.js that uses Node 12 private fields:

#private field

Create an index.js file:

Private fields are inaccessible!

Test the Functions Framework locally

You’ll want to test your service locally to ensure you code works.

So we got things working locally. But how do we deploy this to Google Cloud? Read on!

Next, we’ll use Cloud Build + Cloud Run

Create a Dockerfile

OK. I’ll be honest. If you asked me 6 months ago what Docker was, I’d mumble something about boxes, container ships, and a blue whale. But…

Docker is really nothing to be afraid of. It is an open platform for developing and running applications. To run an application, you’ll need to create some instructions for Docker to set up your environment.

Create a file called Dockerfile (no file extension) as such:

A basic Dockerfile for Node applications

We’ll tell Docker, use Node 12, copy our package.json we defined above, install, copy our files, then start our server!

Deploy to Cloud Run

To deploy to Google’s Cloud Run, follow these 3 steps:

1️⃣ Setup gcloud

# Install gcloud beta services
gcloud components install beta
# Set env var "GCP_PROJECT" to our project name
GCP_PROJECT=$(gcloud config list --format 'value(core.project)' 2>/dev/null)
# Set our Cloud Run region (so we aren't prompted)
gcloud config set run/region us-central1

2️⃣ Upload your code and build the container

# Build and upload your image in Google Container Registry
gcloud builds submit --tag gcr.io/$GCP_PROJECT/helloworld
Notice each command in the Dockerfile gets executed (~30s)

3️⃣ Deploy to Cloud Run

# Deploy your container to Cloud Run
gcloud beta run deploy --image gcr.io/$GCP_PROJECT/helloworld --platform managed
Deploy our container to Cloud Run (~17s)

You’ll get a URL that looks like this:

https://helloworld-q7vieseilq-uc.a.run.app

Congrats! You deployed the Functions Framework to Cloud Run.

Next Steps

Thanks for reading! Check out these related posts:

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.

Grant Timmerman

Written by

Google • Explorer

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