6

I was trying to create a google cloud function which can be triggered by a pubsub topic. I used the google deployment manager template for creating it. But I couldn't find any way to set the subscriber message filter for that cloud function. I tried to create the subscription before and after the cloud function resource was created. But in no ways it worked. If I create it before creating cloud function then after the resource is created it overrides the subscriber function and removes the filter.

Is it possible to set subscriber message filter for any pubsub cloud function using google deployment manager template?

1 Answer 1

13

In fact, it's not possible at all. When you deploy a Cloud functions in mode trigger-topic, you can't set a filter on the subscription (which is automatically created). And it's not a limitation of deployment manager.

If you want to trigger a Cloud Function on a PubSub message with the filter activated, you need to

  • Deploy your function in HTTP mode (trigger-http)
  • Create a Push Subscription to call in HTTP the Cloud Functions with the message in parameter. When you create the Push Subscription, this time, you can set the filter that you want
  • Secure the communication between the Push Subscription and the Cloud Functions (the security is automatically built in the trigger-topic mode (alias background function))
6
  • Do you know why google hasn't provided this option for event triggered functions ?
    – Akhil
    Commented Aug 23, 2021 at 19:35
  • 1
    I don't know, maybe it was too complex, and the system too old. A new Cloud Functions environment is coming, it will more flexible and I'm sure this feature will be implementable; But I don't know if it will! Commented Aug 23, 2021 at 19:53
  • As someone with the same question as OP, this is very good to know. I do have a follow-up question: Was this answer given with gen1, or "legacy" Cloud Functions in mind? If so, are you aware if this has changed with gen2 Cloud Functions?
    – scoll
    Commented Nov 15, 2023 at 13:49
  • No, it's the same with gen2 and eventarc trigger. If you create a direct connection from Cloud Functions you can't add filter. You still have to define a side push subscription with your filter, that invoke your Cloud Functions (gen2 or gen1) in HTTP mode. Commented Nov 15, 2023 at 14:43
  • That is a good point. That said, I came back to this thread because I was just looking at the GCP document for "Deploy a Cloud Function," and I am wondering if there is a way around that with "trigger-event-filter". This is the snippet I am specifically referring to cloud.google.com/functions/docs/…. Could the "trigger-event-filter" be configured to check if PubSub message is published to topic with the desired attribute?
    – scoll
    Commented Nov 15, 2023 at 16:35