If you are creating a new function, see the Console Quickstart on Cloud Run. The content on this page only applies to existing legacy functions created with the Cloud Functions v1 API.
Stay organized with collections
Save and categorize content based on your preferences.
Pub/Sub triggers (1st gen)
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.
For a function to use a Pub/Sub trigger, it must be implemented as an
event-driven function:
If you use a
background function,
the Pub/Sub event data payload is passed directly to your function
in the
PubsubMessage
format.
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.
This event type is supported for legacy functions already consuming these
events. However, we recommend using the --trigger-topic flag instead, as
the legacy event type might be removed at a future date.
In the Select a Cloud Pub/Sub topic field, select a topic for the
trigger to monitor, or select Create a topic to open a window to
create a new topic. When a message is published to your function's
topic, that triggers a call to your function.
Select or clear the Retry on failure checkbox to control
whether failed function calls are automatically retried. See
Retrying event-driven functions
for more information.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-08-28 UTC."],[[["\u003cp\u003ePub/Sub triggers in Cloud Run functions (1st gen) allow functions to be invoked in response to Pub/Sub messages published to a specified topic.\u003c/p\u003e\n"],["\u003cp\u003eFunctions using Pub/Sub triggers must be implemented as event-driven functions, either CloudEvent functions or background functions, and handle data in the appropriate format.\u003c/p\u003e\n"],["\u003cp\u003eDeployment of functions with Pub/Sub triggers can be configured using either the gcloud CLI with the \u003ccode\u003e--trigger-topic\u003c/code\u003e flag or the Google Cloud console by selecting the Cloud Pub/Sub trigger type.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003e--retry\u003c/code\u003e flag in the gcloud CLI or the "Retry on failure" checkbox in the console controls whether failed function calls triggered by Pub/Sub events are automatically retried.\u003c/p\u003e\n"],["\u003cp\u003eLegacy functions can use a different event type specified by \u003ccode\u003eproviders/cloud.pubsub/eventTypes/topic.publish\u003c/code\u003e, however it is recommended to use \u003ccode\u003e--trigger-topic\u003c/code\u003e as this method might be removed in the future.\u003c/p\u003e\n"]]],[],null,["# Pub/Sub triggers (1st gen)\n==========================\n\nIn Cloud Run functions, a Pub/Sub trigger enables a function to be called\nin response to [Pub/Sub](/pubsub/docs) messages. When you specify a\nPub/Sub trigger for a function, you also specify a Pub/Sub\ntopic. Your function will be called whenever a message is published to the\nspecified topic.\n\nFor a function to use a Pub/Sub trigger, it must be implemented as an\n[event-driven function](/functions/1stgendocs/writing/write-event-driven-functions):\n\n- If you use a\n [CloudEvent function](/functions/1stgendocs/writing/write-event-driven-functions#cloudevent-functions),\n the Pub/Sub event data is passed to your function in the\n [CloudEvents format](/eventarc/docs/cloudevents) and the CloudEvent data\n payload is of type\n [`MessagePublishedData`](https://github.com/googleapis/google-cloudevents/blob/main/proto/google/events/cloud/pubsub/v1/data.proto).\n\n- If you use a\n [background function](/functions/1stgendocs/writing/write-event-driven-functions#background-functions),\n the Pub/Sub event data payload is passed directly to your function\n in the\n [`PubsubMessage`](https://github.com/googleapis/google-cloudevents/blob/main/proto/google/events/cloud/pubsub/v1/data.proto)\n format.\n\nThe\n[Google Events](https://github.com/googleapis/google-cloudevents)\nrepository contains additional resources for working with event data.\n\nDeployment\n----------\n\nYou can specify a Pub/Sub trigger when you deploy a function. See\n[Deploy a Cloud Run function](/functions/1stgendocs/deploy) for\ngeneral instructions on how to deploy a function, and see this\nsection for additional information specific to configuring Pub/Sub\ntriggers during deployment. \n\n### gcloud\n\nIf you are\n[deploying using the gcloud CLI](/functions/1stgendocs/deploy#gcloud),\nthe flags shown here are used to configure Pub/Sub triggers: \n\n```sh\ngcloud functions deploy YOUR_FUNCTION_NAME \\\n--no-gen2 \\\n--trigger-topic=YOUR_PUBSUB_TOPIC \\\n[--retry] \\\n...\n```\n\n- The [`--trigger-topic`](/sdk/gcloud/reference/functions/deploy#--trigger-topic) flag specifies the Pub/Sub topic that the trigger will monitor. Messages published to this topic trigger calls to your function.\n- The [`--retry`](/sdk/gcloud/reference/functions/deploy#--retry) flag controls whether failed function calls are automatically retried. See [Retrying event-driven functions](/functions/1stgendocs/bestpractices/retries) for more information.\n\n**Legacy Pub/Sub events**\n\nLegacy functions in Cloud Run functions (1st gen) use a different event type\nfor Pub/Sub triggers: \n\n```sh\ngcloud functions deploy YOUR_FUNCTION_NAME \\\n--no-gen2 \\\n--trigger-event=providers/cloud.pubsub/eventTypes/topic.publish \\\n--trigger-resource=YOUR_PUBSUB_TOPIC \\\n...\n```\n\nThis event type is supported for legacy functions already consuming these\nevents. However, we recommend using the `--trigger-topic` flag instead, as\nthe legacy event type might be removed at a future date.\n\n### Console\n\nIf you are\n[deploying using the Google Cloud console](/functions/1stgendocs/deploy#console),\nyou can configure a Pub/Sub trigger in the **Trigger** section:\n\n1. In the **Trigger type** field, select **Cloud Pub/Sub**.\n2. In the **Select a Cloud Pub/Sub topic** field, select a topic for the trigger to monitor, or select **Create a topic** to open a window to create a new topic. When a message is published to your function's topic, that triggers a call to your function.\n3. Select or clear the **Retry on failure** checkbox to control whether failed function calls are automatically retried. See [Retrying event-driven functions](/functions/1stgendocs/bestpractices/retries) for more information.\n\nNext steps\n----------\n\n- Learn how to [write event-driven functions](/functions/1stgendocs/writing/write-event-driven-functions).\n- Learn how to [deploy a Cloud Run function](/functions/1stgendocs/deploy).\n- See the [Pub/Sub tutorial](/functions/1stgendocs/tutorials/pubsub-1st-gen) for an example of writing, deploying, and calling a function with a Pub/Sub trigger."]]