Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Google Cloud Functions support multiple functions in a single deployment package?

Moreover, what's an efficient way to deploy a GCF solution given the requirement for multiple related functions? (And given that each deployment step is fairly slow - 10's of seconds)

This sounds like a straightforward question, but I can't find a straight answer with exactly what to do. I've read a few related answers on SO, but they aren't very clear.

The Google documentation here mentions exporting "one or more functions": https://cloud.google.com/functions/docs/writing/

But when it comes time to deploy, the CLI command explicitly only deploys one function as far as I can tell: gcloud beta functions deploy OneFunctionNameHere.

What am I missing? If I have several related functions in one index.js, surely I don't need several slow gcloud beta functions deploy commands? (Or maybe I do so that I get independent reporting for each one???)

I'm guessing one of these might be the answer, but I'm really not sure:

  1. Push/deploy once from local source into a GCP bucket, then deploy from that bucket for the other functions? (Faster?)
  2. Extend the access URL like ".../MyFunction/SomeSubFunction" and use request.params to route to the right response-generating code. (There are npm packages for that..)
  3. Run multiple deploys in parallel so that 'time to deploy' isn't a big issue (bandwidth allowing...)

Thanks for any wisdom for a better solution.

like image 701
spechter Avatar asked Oct 23 '17 14:10

spechter


People also ask

Can a cloud function have multiple triggers?

You specify triggers as part of function deployment. You cannot bind the same function to more than one trigger at a time, but you can have the same event cause multiple functions to execute by deploying multiple functions with the same trigger settings.

How many requests can handle a single cloud function?

By default each Cloud Run container instance can receive up to 80 requests at the same time; you can increase this to a maximum of 1000.

Is Firebase Functions the same as Cloud Functions?

Firebase gives mobile developers access to a complete range of fully managed mobile-centric services including analytics, authentication and Realtime Database. Cloud Functions rounds out the offering by providing a way to extend and connect the behavior of Firebase features through the addition of server-side code.

What is the difference between Google Cloud run and Google Cloud Functions?

Cloud Functions lets you deploy snippets of code (functions) written in a limited set of programming languages, while Cloud Run lets you deploy container images using the programming language of your choice.


2 Answers

I do not think it is possible to deploy multiple functions using one command for google cloud - as mentioned by various posts. To make things easy, I ended up using the following command:

sed -n 's/exports\.\([a-zA-Z0-9\-_#]*\).*/\1/p' index.js | xargs -I {} gcloud beta functions deploy {} --trigger-http

Be careful when using this, specifically, make sure that the output of sed -n 's/exports\.\([a-zA-Z0-9\-_#]*\).*/\1/p' index.js are only the name of the functions you want to export.

like image 165
Samik R Avatar answered Sep 27 '22 22:09

Samik R


You can have multiple Firebase Cloud Functions in one deployment or single index.js file.

index.js

 const functions = require('firebase-functions')
 const admin = require('firebase-admin')
 admin.initializeApp(functions.config().firebase)

 exports.sendNotification = functions.https.onRequest((req, res) => {
     res.end()
 })

 exports.saveToken = functions.https.onRequest((req, res) => {
     res.end()
 })

Call the functions with a POST request

https://us-central1-projectname.cloudfunctions.net/saveToken

and

https://us-central1-projectname.cloudfunctions.net/sendNotification

To deploy the functions run

 npm run deploy

package.json

{
  "name": "functions",
  "version": "1.0.0",
  "main": "index.js",
  "scripts": {   
    "deploy": "firebase deploy --only functions"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "firebase-admin": "^5.2.1",
    "firebase-functions": "^0.6.3"
  }
}

In the cloud function console you see them as separate functions.

enter image description here

like image 20
Yoruba Avatar answered Sep 27 '22 20:09

Yoruba