Streamlining Node API Deployment on Cloud Run
Simplifying Cloud Infrastructure Setup
With the rise of no-ops platforms, setting up complex cloud infrastructure for small to medium-sized projects has become increasingly easy. Cloud Run, built on Knative, is a fully managed serverless platform that allows you to deploy containerized applications with autoscaling configured out of the box.
Getting Started with Cloud Run
To follow along with this tutorial, you’ll need:
- Basic knowledge of Node.js
- Basic knowledge of MongoDB (nice to have)
- Basic knowledge of Git
- Yarn or NPM installed (we’ll be using yarn)
- Git installed
- Google Cloud SDK installed
- Postman installed
- A MongoDB atlas account
- A Google account
- A development machine configured to run Node.js
Initializing the Project Directory
Run the following commands in your terminal to set up the project directory:
// Initialize the project directory
Building the API Image
Cloud Run allows you to run applications built with almost any language, thanks to application packaging and containerization. For this tutorial, we’ll use Google Cloud Builder to package and containerize our API.
Defining Image Specifications
Open the Dockerfile in the root of your project directory and add the following code:
// Dockerfile code snippet
This code pulls in a lightweight version of Node.js, copies dependency manifests, installs dependencies, copies the codebase, compiles code from ES6 to ES5 with babel, and starts the API server.
Building the Container Image
Run the following command to build the container image:
// Build the container image
Deploying to Cloud Run
Head over to the Google Cloud Console to create your Cloud Run service. Click the CREATE SERVICE button and specify your service name, container image from GCR, and make it publicly accessible.
Setting up CI/CD with Cloud Build
To automate deployments, we’ll create a cloudbuild.yaml file and configure a continuous deployment (CD) build trigger.
Creating the cloudbuild.yaml File
Run the following command to create the file:
$ touch cloudbuild.yaml
Add the following code to define deployment rules:
// cloudbuild.yaml code snippet
This YAML snippet shows how Cloud Build builds, packages, and deploys our API.
Configuring the Build Trigger
Head over to the triggers page in the Google Cloud console and connect your repository. Then, run the following code in your terminal:
// Configure the build trigger
Testing the Build Trigger
Update the index method in src/controllers/base-controller.js and commit the changes to your source repository. If all goes well, you should see the updated API in action.
Handling Environment Variables
Environment variables differentiate our local development environment from production. We’ll explore two ways to deploy our API with environment variables: storing them in a Google storage bucket and manual definition on the Cloud Run console.
Storing Environment Variables in a Storage Bucket
Create a storage bucket called secrets-locker and copy your env variable into the bucket using gsutil. Then, add an extra build step to your cloudbuild.yaml file to copy the production environment variable into your container image.
Defining Environment Variables on the Cloud Run Console
Alternatively, you can define environment variables manually on the Cloud Run console.
Conclusion
In this tutorial, we’ve learned how to deploy a Node API on Google Cloud Run with and without environment variables. We’ve also learned how to configure continuous delivery for our Cloud Run service.