0
votes

Is it possible to trigger a Google Cloud Function from a Firestore Event with the Serverless Framework?

I’m using Google Cloud Functions + Serverless Framework and I am trying to figure out if the Serverless Framework supports firestore-events.

I want to use Cloud Firestore triggers, but unsure if it’s support, and if it is, then how I correctly specify the event in the serverless.yml file?

An example of a function that should trigger event when any changes to a document happens. From here: https://firebase.google.com/docs/functions/firestore-events

Note: I am importing functions from firebase in a separate file and then importing it in my index.js.

exports.firestoreEvents = functions.firestore
   .document(‘users/marie’).onWrite((change, context) => {
     // ... Your code here
   });

If supported, how do I configure it in serverless.yml?

 firestoreEvents:
   handler: firestoreEvents
   events:
     - event:
       ????
1
You will get more responses if you tag the specific language that you are using.Dylan
Thanks @Dylan I've added node.js to the tags :) The questions though isn't really about the language. It's more generally about if it is possible to use firestore events with Cloud Functions when using the Serverless Framework.dsjacobsen
@DougStevenson yeah, it’s working fine that way. But I want to find out if it is supported with the serverless-framework: serverless.com - without using firebase CLI.. Am I missing something obvious?dsjacobsen
Has anyone got this to work? If so, can you post a working example please.Keith Coughtrey

1 Answers

3
votes

For triggering a function upon a Firestore document change you should write something like this in your serverless.yml file:

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

I have written an article on how to use Serverless Framework with Firebase triggers: https://medium.com/ponce-agtech/using-firebase-triggers-in-serverless-framework-ad99594b86fa

Hope it helps ;)