Applying a path pattern when filtering in Eventarc


Eventarc

You can now apply a path pattern when filtering in Eventarc. This is especially useful when you need to filter on resource names beyond exact match. Path pattern syntax allows you to define a regex-like expression that matches events as broadly as you like.

Let’s take a look at a concrete example.

Without path patterns

Let’s say you want to listen for new file creations in a Cloud Storage bucket with an AuditLog trigger.

Note: The right way of listening for Cloud Storage events is to use a Cloud Storage trigger. I’m using the AuditLog trigger here to illustrate the path patterns, so please bear with me.

This is how you’d create the Audit Log trigger before:

gcloud eventarc triggers create $TRIGGER_NAME\
  --destination-run-service=$SERVICE_NAME \
  --destination-run-region=$REGION \
  --event-filters="type=google.cloud.audit.log.v1.written" \
  --event-filters="serviceName=storage.googleapis.com" \
  --event-filters="methodName=storage.objects.create" \
  --service-account=$PROJECT_NUMBER-compute@developer.gserviceaccount.com

The trigger above would match events from all buckets, as you’re not filtering on a specific resource (i.e. bucket). You have to filter for the right bucket in the code of the service that receives the event, not ideal.

You could try to add the resource name as a filter but the resource here is the actual file name with the full path. Your trigger needs to look like this to match creation of file1.txt in bucket1:

gcloud eventarc triggers create $TRIGGER_NAME\
  --destination-run-service=$SERVICE_NAME \
  --destination-run-region=$REGION \
  --event-filters="type=google.cloud.audit.log.v1.written" \
  --event-filters="serviceName=storage.googleapis.com" \
  --event-filters="methodName=storage.objects.create" \
  --event-filters="resourceName=/projects/_/buckets/bucket-1/objects/file1.txt" \
  --service-account=$PROJECT_NUMBER-compute@developer.gserviceaccount.com

But the problem is: You probably don’t know the names of created files ahead of time.

With path patterns

Fast forward to path patterns today. Now, you can create a trigger with a pattern for the resource name as follows:

gcloud eventarc triggers create $TRIGGER_NAME\
  --destination-run-service=$SERVICE_NAME \
  --destination-run-region=$REGION \
  --event-filters="type=google.cloud.audit.log.v1.written" \
  --event-filters="serviceName=storage.googleapis.com" \
  --event-filters="methodName=storage.objects.create" \
  --event-filters-path-pattern="resourceName=/projects/_/buckets/bucket-1/objects/*" \
  --service-account=$PROJECT_NUMBER-compute@developer.gserviceaccount.com

This trigger will filter for all file creations within the bucket. This is exactly what you want when you’re listening for new file creation events.

Valid Patterns

Here are some valid patterns from the documentation:

Pattern Description
/projects/_/buckets/bucket-1/objects/file1.txt Specific resource name.
/projects/_/buckets/bucket-1/objects/* Matches any file in the objects directory.
/projects/_/buckets/bucket-1/objects/*.txt Matches all TXT files in the objects directory.
/projects/_/buckets/bucket-1/objects/file-*.txt Matches all TXT files with prefix file- in the objects directory.
/projects/_/*/file-.txt Matches any TXT file with prefix file- for all buckets.
/projects/_/buckets/bucket-/objects/file-.txt Matches all TXT files with prefix file- for any bucket with prefix bucket-.

If you want to learn more, check out the Understand path patterns docs page.

You can also check out the AuditLog step of Trigger Cloud Run with events from Eventarc codelab for an example on how to use path patterns.


Feel free to reach out to me on Twitter @meteatamel for any questions/feedback.


See also