Search code examples
firebasegoogle-cloud-platformgoogle-cloud-firestoregoogle-cloud-functionsgcloud

Error Deploying Google Cloud Function with Firestore Trigger Using Eventarc in gcloud


I am attempting to deploy a second-generation Google Cloud Function that triggers on any write operation (create, update, delete) in a specific Firestore document path using the gcloud CLI tool with Eventarc. However, I'm encountering syntax issues with specifying the path pattern.

Environment:

  • Google Cloud Function (2nd Gen)
  • Runtime: Python 3.12
  • Firestore Database
  • gcloud CLI version: 465.0.0

Objective: I need the Cloud Function to trigger on changes to documents located at a path structured like this: MyCollection/{userId}/MySubCollection/{personId}/MyNestedCollection/{sessionId}

Issue: When deploying the function with the gcloud command, I encounter an error related to the --trigger-event-filters-path-pattern flag. The error message indicates there's a bad syntax for the dictionary argument in the path pattern.

Command Used:

gcloud beta functions deploy trigger_generate_insights \
    --gen2 \
    --runtime python312 \
    --entry-point trigger_generate_insights \
    --trigger-event-filters="type=google.cloud.firestore.document.v1.written" \
    --trigger-event-filters-path-pattern="projects/my-project/databases/(default)/documents/MyCollection/{userId}/MySubCollection/{personId}/MyNestedCollection/{sessionId}" \
    --trigger-location="us-central1" \
    --region us-central1 \
    --memory 128MB \
    --allow-unauthenticated

Error Message:

ERROR: (gcloud.beta.functions.deploy) argument --trigger-event-filters-path-pattern: Bad syntax for dict arg: [projects/my-project/databases/(default)/documents/MyCollection/{userId}/MySubCollection/{personId}/MyNestedCollection/{sessionId}]. Please see gcloud topic flags-file or gcloud topic escaping for information on providing list or dictionary flag values with special characters.

Attempts to Resolve: I've tried escaping the curly braces {} and using quotes around the path. Reviewed gcloud topic flags-file and gcloud topic escaping without finding a solution.


Solution

  • I suggest reviewing the documentation for the deploying an example function. This is what they show:

    gcloud functions deploy FUNCTION_NAME \
    --gen2 \
    --runtime=RUNTIME \
    --region=REGION \
    --trigger-location=TRIGGER REGION \
    --source=. \
    --entry-point=ENTRY_POINT \
    --trigger-event-filters=type=google.cloud.firestore.document.v1.written \
    --trigger-event-filters=database='(default)' \
    --trigger-event-filters-path-pattern=document='users/{username}'
    

    Note two things:

    • The path to the document (with wildcards) in the --trigger-event-filters-path-pattern flag isn't like what you're using - the name of the project and database are not part of the required path.
    • You're not using the --trigger-event-filters to correctly specify the default database.

    Both of these flags take dictionaries, which means they each specify a key/value pair as the value of the flag.

    Probably what you're trying to do would be best expressed using:

    --trigger-event-filters=type=google.cloud.firestore.document.v1.written \
    --trigger-event-filters=database='(default)' \
    --trigger-event-filters-path-pattern=document='MyCollection/{userId}/MySubCollection/{personId}/MyNestedCollection/{sessionId}' \