top of page

Deploying multiple functions to Google Cloud Function in Node/Express application. When, Why, How to

Updated: Jun 13, 2023


Backend Service in a Function Flowchart using Node.js, Git and Google Cloud Functions (GCF)
Backend Service in a Function Flowchart

I have been intrigued by Serverless ever since I heard that first in 2015. The idea of just deploying your code without hustling with Server configurations was exciting but also mind boggling as to how the service providers will do that for me. Black-boxed that for the first time, and trusted the Service Providers ever after.

But back then, I was following the documentation ideally and writing functions to deploy those individually, resulting in 1:1 mapping of the endpoints to Serverless functions and thus if I had 15 endpoints, I ended up creating 15 Cloud functions.

Although that is ideal, but there may be a point when you want to adopt Serverless, but not have multiple cloud functions for multiple endpoints (e.g. when launching MVP/expecting low traffic volumes/benchmarking performances/do not want to invest in management of cloud services etc).

Here is how one can do that.

const express = require("express");
const functions = require('@google-cloud/functions-framework');
const routerV1 = require("express").Router();

const app = express();
/*
  app.use statements
  app.use(express.json());
  app.use(cors());
  other app configuration
*/

/*
 This is my preferred way of defining routes. 
 But you can use any of the syntax supported by express.
*/
const routerV1 = express.Router();

// Define middlwares and controllers as needed by your app.
const userRoutes = routerV1.post("/", middlewares, controllers);

routerV1.use('/user', userRoutes);
// All other routes if you have can be defined.

app.use("/v1", routerV1);


// These 2 lines are most important.
// Instead of root, it can be any name you want to use to register your function name with.
// This name has nothing to do with how your express endpoints are going to be called.
functions.http('root', app);


This allows deploying your express app all together as one function, and then the endpoints can be called all pointing at the same function.

Another aspect is Continuous Deployment. In order to configure CD for Google Cloud Functions, one needs to follow the following steps:-

Step 1. Prerequisites to Continuous Deployment (CD) is Connecting your Source Repository to Google Cloud. That can be done by going to https://source.cloud.google.com/your_project_key (alternatively search for Source Repositories service in Google Cloud Console UI, and select your project). Just select the git vendor, connect to your account, grant access to the repo you wish and you are ready!

Step 2. Go to Google Cloud Console UI. Create a new Function and Select Generation 1 function (at the time of writing this blog CD is supported in Generation 1 only). Select whatever memory and Configuration you wish.

Step 3. In the source code tab, add the connected repo name, (selected in step 1), branch name (the one from which you want to deploy your function), and Voila, you are done!

Google Cloud will immediately fetch the latest commit you push to the selected branch of that repo and build it to this function.

Also if you do not want to change how you work locally, one can still configure google cloud functions framework conditionally as follows:-

const http = require('http');
const express = require("express");
const functions = require('@google-cloud/functions-framework');
const routerV1 = require("express").Router();

const app = express();
/*
  app.use statements
  app.use(express.json());
  app.use(cors());
  other app configuration
*/

/*
 This is my preferred way of defining routes. 
 But you can use any of the syntax supported by express.
*/
const routerV1 = express.Router();

// Define middlwares and controllers as needed by your app.
const userRoutes = routerV1.post("/", middlewares, controllers);

routerV1.use('/user', userRoutes);
// All other routes if you have can be defined.

app.use("/v1", routerV1); 


if (process.env.NODE_ENV === 'local') { // Or whatever way you identify LOCAL
  http.createServer(app).listen(process.env.PORT, () => {
    console.log(`Server started. Listening on port ${process.env.PORT}`);
  });
} else {
  functions.http('root', app);
}

Hope this helps and feel free to reach out for any questions or clarity.

Share this quick hack of deploying the entire express app as a Google Cloud Function without additional Configuration or work.

Happy Coding!

PS: This blog assumes understanding of what is Serverless, why would someone need it, and fundamental understanding of Express.js.

7 views0 comments

コメント


bottom of page