Jika Anda membuat fungsi baru, lihat Panduan Memulai Konsol di Cloud Run. Konten di halaman ini hanya berlaku untuk fungsi lama yang sudah ada yang dibuat dengan Cloud Functions v1 API.
Tetap teratur dengan koleksi
Simpan dan kategorikan konten berdasarkan preferensi Anda.
Pemicu Pub/Sub (generasi ke-1)
Di fungsi Cloud Run, pemicu Pub/Sub memungkinkan suatu fungsi dipanggil sebagai respons terhadap pesan Pub/Sub. Saat menentukan pemicu Pub/Sub untuk suatu fungsi, Anda juga menentukan topik Pub/Sub. Fungsi Anda akan dipanggil setiap kali pesan dipublikasikan ke topik yang ditentukan.
Agar fungsi dapat menggunakan pemicu Pub/Sub, pemicu harus diimplementasikan sebagai fungsi berbasis peristiwa:
Jika Anda menggunakan fungsi latar belakang, payload data peristiwa Pub/Sub akan diteruskan langsung ke fungsi Anda dalam format PubsubMessage.
Repositori Google Events berisi resource tambahan untuk menangani data peristiwa.
Deployment
Anda dapat menentukan pemicu Pub/Sub saat men-deploy fungsi. Lihat Men-deploy fungsi Cloud Run untuk mengetahui petunjuk umum tentang cara men-deploy fungsi, dan lihat bagian ini untuk mengetahui informasi tambahan khusus terkait cara mengonfigurasi pemicu Pub/Sub selama deployment.
Flag --trigger-topic menentukan topik Pub/Sub yang akan dipantau oleh pemicu.
Pesan yang dipublikasikan ke topik ini akan memicu panggilan ke fungsi Anda.
Jenis peristiwa ini didukung untuk fungsi lama yang telah menggunakan peristiwa ini. Namun, sebaiknya gunakan flag --trigger-topic, karena jenis peristiwa yang lama mungkin dihapus pada tanggal yang akan datang.
Di kolom Select a Cloud Pub/Sub topic, pilih topik untuk pemicu yang akan dipantau, atau pilih Create a topic guna membuka jendela untuk membuat topik baru. Saat pesan dipublikasikan ke topik fungsi Anda, hal tersebut akan memicu panggilan ke fungsi Anda.
Centang atau hapus centang pada kotak Coba lagi saat gagal untuk mengontrol
apakah panggilan fungsi yang gagal akan dicoba ulang secara otomatis. Lihat Mencoba ulang fungsi berbasis peristiwa untuk mengetahui informasi selengkapnya.
[[["Mudah dipahami","easyToUnderstand","thumb-up"],["Memecahkan masalah saya","solvedMyProblem","thumb-up"],["Lainnya","otherUp","thumb-up"]],[["Sulit dipahami","hardToUnderstand","thumb-down"],["Informasi atau kode contoh salah","incorrectInformationOrSampleCode","thumb-down"],["Informasi/contoh yang saya butuhkan tidak ada","missingTheInformationSamplesINeed","thumb-down"],["Masalah terjemahan","translationIssue","thumb-down"],["Lainnya","otherDown","thumb-down"]],["Terakhir diperbarui pada 2025-08-19 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."]]