1

I'm using the following approach (source) to build a GCP Cloud Function that handles Firestore events.

const functions = require('firebase-functions');
exports.myFunction = functions.firestore
 .document('my-collection/{docId}')
 .onWrite((change, context) => { /* ... */ });

There is no example on how to deploy this correctly to GCP Functions though, only Firebase ones. using the regular gcloud deploy commands such as this one won't work.

gcloud functions deploy FUNCTION_NAME \
--entry-point ENTRY_POINT \
--runtime RUNTIME \
--trigger-event "providers/cloud.firestore/eventTypes/document.write" \
--trigger-resource "projects/YOUR_PROJECT_ID/databases/(default)/documents/messages/{pushId}"

Any ideas on how to do this?

kob490
  • 3,177
  • 4
  • 25
  • 40
  • why don't you choose [firebase cloud functions](https://firebase.google.com/docs/functions/firestore-events#writing_data) instead of gcp function as you want to trigger it with firestore write? is there any reason? – Roopa M Nov 16 '22 at 14:10
  • Mainly because I'm used to GCP Functions in my stack and workflows and already use it for multiple integrations. but also because I already tried the firebase stack and encountered too many issues I'd rather not deal with at this point. – kob490 Nov 16 '22 at 18:25

1 Answers1

0

You can achieve the required effect with creating Cloud Firestore Trigger of your choice

  • For Cloud Functions (1st gen):
  1. In the Trigger type field, select Cloud Firestore.
  2. Select Event Type: Write
  3. Mention the Document on which you want to trigger this function, eg.users/{doc_id}
  4. Check Retry on failure Check box if you want to retry if it did not trigger
  5. Click On Save
  6. Modify your Function as per your requirement
  7. Click Deploy
  8. Make Modification on provided documented path, in our case users/{doc_id}
  9. Check the logs for cloud functions. You will see the function got triggered.
  • For Cloud Functions (2nd gen):
  1. Under HTTPS, in the Authentication field, select an option depending on whether you want to allow unauthenticated invocations of your function. By default, authentication is required.

  2. Click on Add Eventarc Trigger (A modal will appear)

  3. Choose Trigger Type : First Party

  4. Choose Event Provider: Cloud Firestore

  5. Event: google.firestore.v1.Firestore.Write

  6. Resource : If you are having exact path for document choose Specific Resource else you want to target multiple documents using wildcard pattern choose Path Pattern

  7. Check Retry on failure Check box if you want to retry if it did not trigger

  8. Click on Save Trigger

  9. Click Next

  10. Modify your Function as per your requirement

  11. Click Deploy

  12. Make modification on the targeted documents

  13. Check the logs for cloud functions. You will see the function got triggered.

Roopa M
  • 2,171
  • 4
  • 12