Applying a path pattern when filtering in 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:

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.

Originally published at https://atamel.dev.

--

--

--

A collection of technical articles and blogs published or curated by Google Cloud Developer Advocates. The views expressed are those of the authors and don't necessarily reflect those of Google.

Recommended from Medium

An invasive surgery: upgrading a full-stack Dart application to Dart2 / Angular5

How To Make Your PWA Offline On Demand

Running shell in a background process

Flutter lets you build cross platform apps — But What About ‘Design’?

Flutter lets you build cross platform apps — But What About ‘Design’?

Restore database in Azure SQL Database using SQLPackage.exe

CS 373 Spring 2021 Blog #2: Tejas Karuturi

Kubernetes Controller — Implement in Java (Part 1)

IOST Mainnet Olympus v3.8.1 Official Release

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Mete Atamel

Mete Atamel

Developer Advocate at Google

More from Medium

Near Real Time Data Replication using Debezium on GKE

Top 5 Apache Kafka Use Cases for 2022

How to Manage the Data Challenges of a Cloud-Native Platform

How to use Google Pub/Sub emulator locally to test Google Pub/Sub on Apple Silicon Chip (M1)