Using Firebase triggers in Serverless Framework

Mariano Finochietto
PONCE AgTech
Published in
4 min readApr 4, 2019

Having trouble with Firebase triggers in Serverless Framework? You are not alone. This is the article you’ve been waiting for.

Note 1: The code examples below are written for Node.js 6, which is the only stable environment available at the time of writing this article. However, keep in mind that Firebase related triggers are still on beta, so use them at your own risk.

Note 2: Serverless Framework doesn’t support Cloud Functions for Firebase, only Google Cloud Functions. As pointed out in the official docs, it’s more efficient to use the first one to interact with Firebase products.

To follow this guide, you should have basic knowledge in Serverless Framework and Google Cloud Functions.

All Firebase related triggers are classified as Background Functions, so their definition should be the same:

exports.myFunction = (event, callback) => {
// do stuff here
}

In the sections below, you can find a description of each trigger, and how to configure them in the Serverless YAML configuration file.

Cloud Firestore triggers

In the code snippets below, ensure that you replace <your-project-id> for your Firebase project id (in lowercase!), the database (if not, the default one for the project will be used) and the <path-to-document> to the ones you need the trigger to act on (you can use wildcards and parameters).

Create event

Triggered when a document is written to for the first time.

myFunction:
handler: myFunction
events:
- event:
eventType: providers/cloud.firestore/eventTypes/document.create
resource: projects/<project-id>/databases/(default)/documents/<path-to-document>

Update event

Triggered when a document already exists and has any value changed.

myFunction:
handler: myFunction
events:
- event:
eventType: providers/cloud.firestore/eventTypes/document.update
resource: projects/<project-id>/databases/(default)/documents/<path-to-document>

Delete event

Triggered when a document with data is deleted.

myFunction:
handler: myFunction
events:
- event:
eventType: providers/cloud.firestore/eventTypes/document.delete
resource: projects/<project-id>/databases/(default)/documents/<path-to-document>

Write event

Triggered when a document is created, updated or deleted.

myFunction:
handler: myFunction
events:
- event:
eventType: providers/cloud.firestore/eventTypes/document.write
resource: projects/<project-id>/databases/(default)/documents/<path-to-document>

Firebase Realtime Database triggers

These are very similar to the ones for Cloud Firestore. You will need to replace both <project-id> and <path> .

Create event

Triggered when new data is created in the Realtime Database.

myFunction:
handler: myFunction
events:
- event:
eventType: providers/google.firebase.database/eventTypes/ref.write
resource: projects/_/instances/<project-id>/refs/<path>

Update event

Triggered when data is updated in the Realtime Database.

myFunction:
handler: myFunction
events:
- event:
eventType: providers/google.firebase.database/eventTypes/ref.update
resource: projects/_/instances/<project-id>/refs/<path>

Delete event

Triggered when data is deleted from the Realtime Database.

myFunction:
handler: myFunction
events:
- event:
eventType: providers/google.firebase.database/eventTypes/ref.delete
resource: projects/_/instances/<project-id>/refs/<path>

Write event

Triggered on any mutation event: when data is created, updated, or deleted in the Realtime Database.

myFunction:
handler: myFunction
events:
- event:
eventType: providers/google.firebase.database/eventTypes/ref.write
resource: projects/_/instances/<project-id>/refs/<path>

Firebase Authentication triggers

User created event

Triggered when:

  • A user creates an email account and password.
  • A user signs in for the first time using a federated identity provider.
  • The developer creates an account using the Firebase Admin SDK.
  • A user signs in to a new anonymous auth session for the first time.
myFunction:
handler: myFunction
events:
- event:
eventType: providers/firebase.auth/eventTypes/user.create
resource: projects/<project-id>

User deleted event

Triggered when a user account is deleted.

myFunction:
handler: myFunction
events:
- event:
eventType: providers/firebase.auth/eventTypes/user.delete
resource: projects/<project-id>

Google Analytics for Firebase*

We couldn’t test this one out, but it should be as suggested below. If you are able to test it, please reach out to me with feedback using the comment section.

Event logged

Triggered when a conversion event is logged.

myFunction:
handler: myFunction
events:
- event:
eventType: providers/google.firebase.analytics/eventTypes/event.log
resource: projects/<project-id>/events/<conversion-event-name>

So…

Have you used any of the above triggers with Serverless Framework? What is your experience with this framework and Google Cloud Functions?

Source: Google Cloud Functions Documentation

--

--