Migrating to Firebase Functions v2: Production Firestore Event Triggers

Google Cloud Functions Gen 2 (Cloud Run + Eventarc) Migration Guide: Uncover the production architecture changes when migrating Firestore triggers from 1st Gen to 2nd Gen Cloud Functions powered by Google Cloud Run and Eventarc — including concurrency tuning (up to 1,000 concurrent requests per instance), cold-start reduction, idempotent event handling, and IAM Eventarc permissions. Authored by Google Cloud Certified GCP Professional Cloud Architect Aldian Fazrihady.

2026-05-25 23:40:27.533521+00:00

Firebase Cloud Functions v2 represents a massive leap forward for the serverless Firebase ecosystem. Built entirely on top of Google Cloud Run and Eventarc, v2 introduces game-changing capabilities such as concurrent request handling (up to 1,000 requests per container instance), regional endpoints, custom secrets management, and robust event-driven triggers.

However, migrating from Cloud Functions v1 to v2 is not a simple package update. The API surface has been completely redesigned.

This article details the real-world lessons, syntax shifts, and architectural patterns of migrating Firestore Event Triggers from v1 to v2, using actual code patterns implemented in the Tetangga App codebase.


🏗️ The Architectural Evolution: v1 vs v2

Before diving into the code, it is important to understand the underlying infrastructure shift:

🔄 Syntax Breakdown: Moving from v1 to v2

Let’s look at the absolute changes when writing a Firestore Document Creation listener.

The Legacy v1 Code (Deprecated)

import * as functions from "firebase-functions";

export const onTopicCreatedV1 = functions.firestore
  .document("topics/{topicId}")
  .onCreate(async (snap, context) => {
    const topic = snap.data();
    const topicId = context.params.topicId; // v1 uses 'context' for wildcards
    console.log(New topic created: ${topicId});
  });

The Modern v2 Code

import { onDocumentCreated } from "firebase-functions/v2/firestore";

export const onTopicCreatedV2 = onDocumentCreated(
  { 
    document: "topics/{topicId}", 
    database: "production-db", // v2 allows specifying multi-databases!
    region: "asia-southeast2",  // Configure region directly inside the options object
  },
  async (event) => {
    const snap = event.data;    // v2 uses a single 'event' object
    if (!snap) return;
    
    const topic = snap.data();
    const topicId = event.params.topicId; // Wildcard parameters are now inside 'event.params'
    console.log(New topic created: ${topicId});
  }
);

🔑 Key Real-World Lessons from the Tetangga Codebase

During the migration of the Tetangga neighborhood portal triggers (such as onTopicCreatedV2 and onTopicReplyCreatedV2), several critical lessons were learned.

1. The Single Parameter Paradigm Shift

In v1, your callbacks always took two arguments: (snapshot, context). In v2, your callbacks receive a single unified event object of type FirestoreEvent.

This leads to the following key mappings:

2. Multi-Database Support is Now Native

If your project utilizes Firebase's multi-database Firestore feature, v1 made targeting non-default databases highly complex. In v2, you can specify the target database directly inside the options configuration block:

export const onTopicCreatedV2 = onDocumentCreated(
  { document: 'topics/{topicId}', database: 'aldianapps' },
  async (event) => {
    // This listener only targets the 'aldianapps' Firestore database!
  }
);

3. Exposing Secrets Safely

If your triggers communicate with external APIs (like sending FCM notifications, or sending Twilio WhatsApp alerts like in Tetangga), you must not expose secret tokens in your code. v2 allows you to bind Google Cloud Secrets Manager secrets directly to your triggers:

import { onDocumentCreated } from "firebase-functions/v2/firestore";

export const onNotificationTrigger = onDocumentCreated(
  { 
    document: "notifications/{id}",
    secrets: ["TWILIO_AUTH_TOKEN", "FCM_SERVER_KEY"] // Auto-bind secrets safely
  },
  async (event) => {
    const token = process.env.TWILIO_AUTH_TOKEN; // Accessible securely in runtime
    // Send alert safely...
  }
);

4. Resolving the AppRegistryNotReady Initialization Order

When importing v2 triggers alongside Twilio, sendgrid, or custom database configurations, ensure you initialize your Firebase Admin SDK first, before exporting any triggers or importing services that depend on firestore instances:

import * as admin from "firebase-admin";
import { getFirestore } from "firebase-admin/firestore";

// Initialize Firebase Admin first
const app = admin.initializeApp();
const db = getFirestore(app);

// NOW import / export trigger scripts
import { onDocumentCreated } from "firebase-functions/v2/firestore";

🚀 Step 5: Summary of Migration Gains

By refactoring your Firestore database triggers to Firebase Functions v2, you achieve:

  1. Dramatic Cost Reductions: Up to 10x less container instantiation overhead via Cloud Run request concurrency.
  2. Improved Maintainability: Strict TypeScript types for the event payload and wildcard parameters.
  3. Native Multi-Database Listeners: Effortless scaling across Firestore shards.
  4. State-of-the-Art Secrets Security: Zero-leak environment configurations bound directly to Cloud Secrets Manager.

Idempotent CloudEvents & Infinite-Loop Guards in Firebase Functions v2

Prevent recursive Firestore onDocumentUpdated trigger storms by comparing field diffs and tracking processed event IDs before writing back to the same document.

Firebase Functions v2 (Cloud Run & Eventarc) Production FAQ

What changes when migrating Firestore triggers from Firebase Functions v1 to v2?

Firebase Functions v2 runs on Google Cloud Run with Eventarc delivery instead of single-concurrency Gen 1 containers, allowing up to 1,000 concurrent requests per container instance, configurable CPU/memory tiers, and explicit Cloud Storage / Pub/Sub service agent IAM bindings.

How do you prevent infinite trigger loops in Firestore onDocumentUpdated functions?

Compare canonical before/after document snapshots at the very top of the handler, skip writes when target fields are already up to date, and skip redundant updates when your own service last mutated the document metadata marker.

Why do Firestore Eventarc triggers require idempotency guards in v2?

Eventarc delivers events with at-least-once semantics during network retries and container scaling, so checking a processed event.id marker inside a Firestore transaction prevents duplicate notifications, billing events, or downstream webhooks.


About the Author — Hire Aldian Fazrihady (Cloud Engineer, GCP Professional Cloud Architect, GCP Professional Data Engineer, Backend, AI & Full Stack Engineer)

Aldian Fazrihady is a Top-Rated Senior Cloud Engineer, GCP Professional Cloud Architect (Google Cloud Certified), GCP Professional Data Engineer (Google Cloud Certified), Backend Engineer (Python, Django, FastAPI, Go, PostgreSQL), AI Application Engineer (LLMs, RAG, MCP, Google ADK, LangGraph), and Full Stack Engineer (React, Next.js, TypeScript) with 25+ years of production software engineering experience.

Hire Aldian Fazrihady on Upwork | Book 1-Day Programming Service ($100 USD / Rp 1.000.000) | View Live AI Portfolio | Send a Direct Message

Related Engineering Articles

Engineering Role Pillar Guides & Category Hubs