Skip to content

Unable to Filter Pub/Sub Messages by Attributes for Cloud Functions #156

@selimacerbas

Description

@selimacerbas

Description:
The module for deploying Google Cloud Functions with Pub/Sub triggers does not support filtering messages at the subscription level. As a result, all messages published to the topic are sent to all Cloud Functions associated with the topic, even if the message attributes are intended to target specific functions. This leads to unnecessary invocation of functions that are not meant to handle those messages.

Problem Statement:
Google Cloud Pub/Sub supports message attribute filtering at the subscription level, which allows only messages matching specific attributes to be delivered to the subscriber. However, the predefined module being used for deploying Cloud Functions does not provide a way to configure these filters on the subscriptions.

Without subscription-level filtering:

All Cloud Functions subscribed to the same topic will process every message published to that topic.
It creates unnecessary overhead for the functions that receive messages they are not designed to handle.
Developers are forced to implement manual filtering logic inside the Cloud Functions themselves, which is inefficient and increases costs.
Example Scenario:
Objective:

We want to use a single Pub/Sub topic to handle scheduled messages from Cloud Scheduler, but ensure that only specific Cloud Functions are triggered based on message attributes.

Desired Flow:

Cloud Scheduler publishes a message to the scheduled-tasks Pub/Sub topic with an attribute function_target. Example message:

{
  "data": "some-payload",
  "attributes": {
    "function_target": "functionA"
  }
}

A subscription for Function A should filter messages with function_target = "functionA".
A subscription for Function B should filter messages with function_target = "functionB".

Current Behavior:

The predefined module creates subscriptions without filters.
Both Function A and Function B receive all messages published to the topic, regardless of the function_target attribute.

When we define event_filters property as non null, it simply wants to check for the values that exists under the defined event_type schema. As an example;

**############ ERROR ##############**

│ Error: Error creating function: googleapi: Error 400: Validation failed for trigger projects/***/locations/europe-west2/triggers/project_id: The request was invalid: invalid argument: event type **google.cloud.pubsub.topic.v1.messagePublished** not supported: attribute action not found within event type
│ 
│   with module.function.module.cloud-function.google_cloudfunctions2_function.function,
│   on .terraform/modules/function.cloud-function/main.tf line 21, in resource "google_cloudfunctions2_function" "function":
│   21: resource "google_cloudfunctions2_function" "function" {
│ 
╵
╷
│ Error: Error creating function: googleapi: Error 400: Validation failed for trigger projects/***/locations/europe-west6/triggers/project_id: The request was invalid: invalid argument: event type **google.cloud.pubsub.topic.v1.messagePublished** not supported: attribute action not found within event type
│ 
│   with module.function.module.cloud-function.google_cloudfunctions2_function.function,
│   on .terraform/modules/function.cloud-function/main.tf line 21, in resource "google_cloudfunctions2_function" "function":
│   21: resource "google_cloudfunctions2_function" "function" {

################## ERROR END ###################

The current configuration wants to check the schema of MessageDataPublished. Schema can be found under: https://github.com/googleapis/google-cloudevents/blob/main/jsonschema/google/events/cloud/pubsub/v1/MessagePublishedData.json. The "action" attribute is passed via Cloud Scheduler but is is not handled at Subscription level.

image

This part at UI on PubSub (above picture) should be handled properly.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions