Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Say Hello to Cloud Functions for Firebase 2nd Gen

Say Hello to Cloud Functions for Firebase 2nd Gen

Get to know Cloud Functions for Firebase (2nd gen), the next generation of Functions-as-a-Service that built on Cloud Run and Eventarc to gives you more powerful infrastructure, advanced control over performance and scalability.

Firebase Thailand

August 19, 2023
Tweet

More Decks by Firebase Thailand

Other Decks in Technology

Transcript

  1. “Write and Run backend code in response to events triggered

    by Firebase features and HTTPS requests” Cloud Functions for Firebase
  2. Events exports.make Uppercase = functions.da tabase()... exports.mode rate = functions.da

    tabase()... exports.send Email = functions.da tabase()... Authentication Firestore Realtime Database Storage Extensions App Distribution Crashlytics Performance Monitoring Test Lab Remote Config Analytics Firebase Triggers
  3. Cloud Scheduler const { onSchedule } = require("firebase-functions/v2/scheduler"); exports.cronjob =

    onSchedule("every day 10:00", async (event) => { // Send notification }); Schedule Functions
  4. • Up to 60 mins for HTTP triggered functions •

    Up to 9 mins for event triggered functions Request Timeout
  5. import {beforeUserCreated} from "firebase-functions/v2/identity"; export const blockauth = beforeUserCreated((event) =>

    { }); const user = event.data; // Ensure only google.com emails can log into the changelog app if (!user?.email?.includes('@google.com')) { throw new HttpsError( "permission-denied", "Only @google.com are allowed to register."); } Blocking Functions
  6. Both APIs are in the same SDK const functions =

    require("firebase-functions/v1"); const { onRequest } = require("firebase-functions/v2/https"); exports.f1 = functions.https.onRequest((req, res) => { res.send("Hello from Firebase!"); }); exports.f2 = onRequest((req, res) => { res.send("Hello from Firebase!"); });
  7. Runtime options const functions = require("firebase-functions/v1"); const { onRequest }

    = require("firebase-functions/v2/https"); const cors = require("cors")({ origin: true }); exports.f1 = functions.runWith({ memory: "1GB" }).https.onRequest((req, res) => { cors(req, res, () => { res.send("Hello from Firebase!"); }); }); exports.f2 = onRequest({ memory: "1GB", cors: true }, (req, res) => { res.send("Hello from Firebase!"); });
  8. Runtime options const { onRequest } = require("firebase-functions/v2/https"); const {

    setGlobalOptions } = require("firebase-functions/v2"); setGlobalOptions({ region: "asia-southeast1", memory: "1GB", concurrency: 40 }); export const api = onRequest(apiHandler); export const content = onRequest(contentHandler);
  9. Loading environment variables specified in a .env file to your

    application runtime Securely store API keys, passwords, certificates, and other sensitive data. Environment Variables Cloud Secret Manager Strongly-typed environment configuration with parameters that are validated at deploy time Parameterized Config Environment Configuration New
  10. Parameterized Configuration const { onRequest } = require("firebase-functions/v2/https"); const {

    defineString, defineInt, defineSecret } = require("firebase-functions/params"); const welcomeMessage = defineString("WELCOME_MESSAGE"); const concurrencyConfig = defineInt("CONCURRENCY", { default: 80 }); const apiKey = defineSecret("API_KEY"); exports.f2 = onRequest({secrets: [apiKey], concurrency: concurrencyConfig}, (req, res) => { const sampleApiKey = apiKey.value(); res.send(`${welcomeMessage.value()}! I am a function.`); });
  11. Enhancements in Cloud Functions 2nd Gen ⚡Concurrency (Up to 1000

    concurrent requests per instance) ☁ Larger in memory, and compute intensive (Up to 32GiB RAM with 8 vCPUs) 󰞵 Python support 🚨 New events (Firebase Alerts, Auth Blocking, and Firebase Extensions) 🌐 Secure your callable and HTTP functions with a new CORS ✔ Task Queue functions ⏳ Long-running in HTTP Functions (Up to 1 hour timeout) 🖥 Modern JavaScript (config functions globally, options objects, and modular imports) Recap