spechter
spechter

Reputation: 2368

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.

Upvotes: 14

Views: 8709

Answers (2)

Yoruba
Yoruba

Reputation: 2780

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

Upvotes: 4

Samik R
Samik R

Reputation: 1638

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.

Upvotes: 4

Related Questions