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:
Thanks for any wisdom for a better solution.
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.
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With