1
votes

I am trying to setup a Google Cloud Function with a Firebase Realtime Database Trigger and cannot make the function to be triggered when I add a document to the database.

What I want to happen is that when there is a new entry to the Firebase database collection yyy under project xxx I want the Cloud Function funtion-1 to be triggered. Function-1 is the default (as per below) and a test worked fine.

I am using the main console and created a function named function-1. I can see the function itself in the firebase console:

https://console.firebase.google.com/u/0/project/xxx/functions/list

The collection I set is under project xxx, named yyy, and I can access it under

https://console.firebase.google.com/u/0/project/xxx/database/firestore/data~2Fyyy

I am in the functions console:

https://console.cloud.google.com/functions/edit/us-central1/function-1?project=xxx

and the setup is as follows:

  • Trigger: Firebase Realtime Database (Beta)
  • Event Type: Create
  • Database: xxx
  • Path: /data/yyy

Runtime is Python 3.7

Code is default Google Cloud Functions code:

def hello_rtdb(event, context):
    """Triggered by a change to a Firebase RTDB reference.
    Args:
         event (dict): Event payload.
         context (google.cloud.functions.Context): Metadata for the event.
    """
    resource_string = context.resource
    # print out the resource string that triggered the function
    print(f"Function triggered by change to: {resource_string}.")
    # now print out the entire event object
    print(str(event))

requirements.txt is empty

I have used other triggers (HTTP, or PubSub) successfully in other Google Cloud Functions but I cannot get the function to be triggered by a database event. I have tried a wide range of options for the path variable but couldn't make it work.

The options I tried for the path variable are:

  • /xxx/database/firestore/data/yyy
  • /database/firestore/data/yyy
  • /data/yyy
  • /yyy
  • yyy etc...

I am sure I am making a basic mistake but sadly the documentation isn't helping (probably because this is such a basic thing). How can I set this up in the right way?

1

1 Answers

1
votes

Are you using Google Firestore or Firebase Firestore? I know they are technically the same product under the covers but I believe they trigger different events. It may depend on whether you created the DB from Google Cloud Platform or Firebase.

$ gcloud functions event-types list

EVENT_PROVIDER                   EVENT_TYPE                                                EVENT_TYPE_DEFAULT  RESOURCE_TYPE       RESOURCE_OPTIONAL
google.firebase.database.ref     providers/google.firebase.database/eventTypes/ref.write   Yes                 firebase database   No
google.firestore.document        providers/cloud.firestore/eventTypes/document.write       Yes                 firestore document  No

I'm using event_provider=google.firestore.document and it works. Here's how I deploy Python functions onto Google Cloud. Assume main.py exists with your above code but function name is called hello_firestore.

$ gcloud functions deploy hello_firestore --entry-point hello_firestore --runtime python37 --trigger-event providers/cloud.firestore/eventTypes/document.write --trigger-resource "projects/$GCP_PROJECT/databases/(default)/documents/$DOC_PATH"

For Firebase Firestore, it should something like this but this is not tested because I created my Firestore from GCP rather than Firebase.

$ gcloud functions deploy hello_rtdb --entry-point hello_rtdb --runtime python37 --trigger-event providers/google.firebase.database/eventTypes/ref.write --trigger-resource "projects/_/instances/$GCP_PROJECT/refs/$DOC_PATH"

Another thing to watch for is that only Firestore native mode supports triggering of events as mentioned here in the section Limitations and guarantees.