Cloud Functions for Firebase is a serverless framework that lets you automatically run backend code in response to events triggered by Firebase features and HTTPS requests. Your JavaScript or TypeScript code is stored in Google's cloud and runs in a managed environment. There's no need to manage and scale your own servers.
Learn about Firebase Cloud Function V2. How they are different from V1, the technology behind it (Cloud Run) and how to use them.
Code samples & performance testing.
2. whoami - I Am !Groot
Aviv Laufer
CTO as a Service - https://42-golems.io/
@avivl
42-golems.io
3. A Brief History of Firebase
● 2014 Acquired by Google, Firebase Hosting & Authentication.
● 2016 Analytics, Messaging.
● Firebase, a real-time document database.
● Cloud Firestore scalable NoSQL cloud database.
● Cloud Functions
● Remote Config
● And much more https://firebase.google.com/products-build
42-golems.io
4. “Cloud Functions for Firebase is a serverless framework that lets you
automatically run backend code in response to events triggered by Firebase
features and HTTPS requests. Your JavaScript or TypeScript code is stored in
Google's cloud and runs in a managed environment. There's no need to
manage and scale your own servers.“
Firebase Functions the Shpiel
42-golems.io
5. The Shpiel continues...
After you write and deploy a function, Google's servers begin to manage the function
immediately. You can fire the function directly with an HTTP request, or, in the case of
background functions, Google's servers will listen for events and run the function when it
is triggered.
● Cloud Firestore triggers
● Realtime Database triggers
● Remote Config triggers
● Firebase Authentication triggers
● Google Analytics triggers
● Cloud Storage triggers
● Pub/Sub triggers
42-golems.io
6. Welcome to V2
Powered by Cloud Run and Eventarc, Cloud Functions for Firebase (2nd gen) has
the potential to give you more powerful infrastructure, advanced control over
performance and scalability, and more control of the functions runtime.
42-golems.io
7. Cloud Run
● Fully managed
● Excellent for stateless containers.
● Serverless
● Built on top of Knative.
● Comes in 2 flavorus.
Shameless Plug
Taking Google Cloud Run for a spin
Hacking Google Cloud Run
42-golems.io
8. Eventarc
Eventarc lets you build event-driven architectures without having to implement, customize, or maintain the underlying
infrastructure. Eventarc offers a standardized solution to manage the flow of state changes, called events, between
decoupled microservices. When triggered, Eventarc routes these events through Pub/Sub subscriptions to various
destinations (in this document, see Event destinations) while managing delivery, security, authorization, observability, and
error-handling for you.
42-golems.io
9. What's new in Cloud Functions for Firebase (2nd gen)
● Function instances can now execute more than one request at a time.
● Secure your callable and HTTP functions with a new `cors` setting.
42-golems.io
10. ● HTTP functions can now have a 1 hour timeout (up from 9 minutes
previously).
● Function instances now default to the default compute service account rather
than the app engine service account.
● New trigger types include Firebase Alerts, triggers custom event triggers for
Firebase Extensions, and task queue functions. .
What's new cont...
42-golems.io
14. Call functions directly
● Call functions from your app
● Call functions via HTTP requests
● Enqueue functions with Cloud Tasks
● Run functions on schedule
42-golems.io
16. Custom event triggers/extensions NEW!
Firebase Extensions:
● Firebase Extensions help you deploy functionality to your app quickly with pre-packaged solutions.
● Once installed, a Firebase Extension performs a specific task or set of tasks in response to HTTPS
requests, Cloud Scheduler events, or to triggering events from other Firebase products, like Cloud
Firestore or Firebase Cloud Messaging.
● Available on the extensions Marketplace
42-golems.io
17. Extensions
● Stream Collections to BigQuery
● Resize Images
● Delete User Data
● Trigger Email
● Send Invoices using Stripe
● Send Messages with Twilio
● And many more....
42-golems.io
18. When you upload a file to your specified Cloud Storage bucket, this extension:
Detects if the file is an image. If it is, then:
● Creates a resized image with your specified dimensions.
● Names the resized image using the same name as the original uploaded
image, but suffixed with your specified width and height.
● Stores the resized image in the same Storage bucket as the original uploaded
image.
Resize Images
42-golems.io
20. Authentication blocking triggers
Blocking functions let you execute custom code that modifies the result of a user registering or
signing in to your app. For example, you can prevent a user from authenticating if they don't meet
certain criteria, or update a user's information before returning it to your client app.
You can register blocking functions for two events:
● beforeUserCreated: Triggers before a new user is saved to the Firebase Authentication
database, and before a token is returned to your client app.
● beforeUserSignedIn: Triggers after a user's credentials are verified, but before Firebase
Authentication returns an ID token to your client app. If your app uses multi-factor
authentication, the function triggers after the user verifies their second factor. Note that
creating a new user also triggers beforeUserSignedIn, in addition to beforeUserCreated.
42-golems.io
23. And the winner is....
● The test was configured to run up to 20 VUs for 6 minutes
● Using K6
42-golems.io
24. Life behind Firebase - Cloud Functions (2nd gen)
● Longer request processing - Up to 60 mins for HTTP functions.
● Larger instances - Up to 16GB of RAM and 4 vCPUs
● Concurrency - Up to 1000 concurrent requests with a single function.
● Minimum instances - Provide for pre-warmed instances to cut your cold starts and make sure the
bootstrap time of your application does not impact application performance.
● Traffic splitting - Support multiple revisions of your functions, splitting traffic between different
revisions and rolling your function back to a prior version.
● Event Sources - Native support for Eventarc, which brings over 90+ event sources from direct
sources and Cloud Audit logs (e.g., BigQuery, Cloud SQL, Cloud Storage, Firebase...)
42-golems.io