Pub/Sub triggers

In Cloud Run functions, a Pub/Sub trigger enables a function to be called in response to Pub/Sub messages. When you specify a Pub/Sub trigger for a function, you also specify a Pub/Sub topic. Your function will be called whenever a message is published to the specified topic.

A Pub/Sub trigger is implemented as an CloudEvent function, in which the Pub/Sub event data is passed to your function in the CloudEvents format, and the CloudEvent data payload is of type MessagePublishedData.

The Google Events repository contains additional resources for working with event data.

Deployment

You can specify a Pub/Sub trigger when you deploy a function. See Deploy a Cloud Run function for general instructions on how to deploy a function, and see this section for additional information specific to configuring Pub/Sub triggers during deployment.

gcloud

If you are deploying using the gcloud CLI, the flags shown here are used to configure Pub/Sub triggers:

gcloud functions deploy YOUR_FUNCTION_NAME \
--trigger-topic=YOUR_PUBSUB_TOPIC \
[--retry] \
...
  • The --trigger-topic flag specifies the Pub/Sub topic that the trigger will monitor. Messages published to this topic trigger calls to your function.
  • The --retry flag controls whether failed function calls are automatically retried. See Retrying event-driven functions for more information.

Console

If you are deploying using the Google Cloud console, you can configure a Pub/Sub trigger in the Trigger section:

  1. In the Trigger type field, choose Cloud Pub/Sub.
  2. In the Cloud Pub/Sub topic field, select a topic for the trigger to monitor. Messages published to this topic trigger calls to your function.
  3. Select or deselect the Retry on failure checkbox to control whether Cloud Run functions automatically retries a failed function invocation. See Retrying event-driven functions for more information.
  4. Click More options to perform additional configuration on your trigger:

    • In the Trigger type field, specify one of Google sources, Custom, or Third-party:

      • Google sources lets you specify triggers for Pub/Sub, Cloud Storage, Firestore, and other Google event providers.In the Eventarc trigger pane, use the Event provider field to select the product that provides the type of event you want to trigger your function. Then in the Event field, select the event you want to use as a trigger.

      • The Custom option lets you produce and consume events from your application code. Follow the prompts in the Eventarc trigger pane to create a channel. A channel is a resource that is used as a pipeline to deliver custom events from producers to consumers. Custom events are published to a channel and an Eventarc trigger subscribes to those events.

      • The Third-party option let you integrate with non-Google providers that offer an Eventarc source. See third-party events in Eventarc for details.

    • In the Event field, select a triggering event. The default is google.cloud.pubsub.topic.v1.messagePublished.

    • Optionally, in the Service account field, select a service account to be used as the identity of the Eventarc trigger. See Trigger identity for more information.

  5. Click Save trigger.

Next steps