jamiet
jamiet

Reputation: 12344

Can a cloud function be triggered by a subset of messages on a Pub/Sub topic?

I've looked at Cloud Pub/Sub Tutorial which explains how to write a cloud function (in Python) to consume a message from a Pub/Sub topic and act upon it.

My use case is slightly different. There are only a subset of messages on the topic that my cloud function is interested in, they are messages that have a certain attribute upon them. I don't want to interrogate the message within the cloud function to see if it has the correct attribute or not because I'll still have to pay for the cost of executing the function, I would rather only execute the cloud function for messages with the attribute upon them.

Is there a way to specify that my function should only consume messages with a specific attribute upon them?

Upvotes: 0

Views: 1190

Answers (2)

guillaume blaquiere
guillaume blaquiere

Reputation: 76000

If you create a background function, I mean a function directly invoked by a PubSub topic, you can't.

To achieve what you want, you need to create an HTTP functions and to create a Push Subscription with message filtering (the filtering is enforced on the message attributes only)

Upvotes: 3

Doug Stevenson
Doug Stevenson

Reputation: 317798

No, there is not. It sounds like you should be instead using multiple topics, one for each possible case you would like to isolate to separate functions or purposes.

From the documentation (emphasis mine):

Every message published to this topic will trigger function execution with message contents passed as input data.

Upvotes: 1

Related Questions