This simple tutorial demonstrates writing, deploying, and triggering an Event-Driven Cloud Run function with a Pub/Sub trigger.
If you are new to Pub/Sub and want to learn more, see the Pub/Sub documentation, particularly managing topics and subscriptions. See Pub/Sub Triggers for an overview of working with Pub/Sub topics and subscriptions in Cloud Run functions.
If you're looking for code samples for using Pub/Sub itself, please visit the sample browser.
Preparing the application
Clone the sample app repository to your local machine:
Node.js
git clone https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git
Alternatively, you can download the sample as a zip file and extract it.
Python
git clone https://github.com/GoogleCloudPlatform/python-docs-samples.git
Alternatively, you can download the sample as a zip file and extract it.
Go
git clone https://github.com/GoogleCloudPlatform/golang-samples.git
Alternatively, you can download the sample as a zip file and extract it.
Java
git clone https://github.com/GoogleCloudPlatform/java-docs-samples.git
Alternatively, you can download the sample as a zip file and extract it.
Ruby
git clone https://github.com/GoogleCloudPlatform/ruby-docs-samples.git
Alternatively, you can download the sample as a zip file and extract it.
Change to the directory that contains the Cloud Run functions sample code for accessing Pub/Sub:
Node.js
cd nodejs-docs-samples/functions/helloworld/
Python
cd python-docs-samples/functions/helloworld/
Go
cd golang-samples/functions/helloworld/
Java
cd java-docs-samples/functions/helloworld/hello-pubsub/
Ruby
cd ruby-docs-samples/functions/helloworld/pubsub/
Take a look at the sample code:
Node.js
Python
Go
Java
Ruby
Deploying the function
To deploy the function with a Pub/Sub trigger, run the following command
in the directory that contains the sample code (or in the case of Java, the
pom.xml
file):
Node.js
gcloud functions deploy helloPubSub \ --runtime nodejs22 \
--trigger-topic YOUR_TOPIC_NAME
Use the --runtime
flag to specify the runtime ID of a
supported Node.js version to run
your function.
Python
gcloud functions deploy hello_pubsub \ --runtime python312 \
--trigger-topic YOUR_TOPIC_NAME
Use the --runtime
flag to specify the runtime ID of a
supported Python version to run
your function.
Go
gcloud functions deploy HelloPubSub \ --runtime go121 \
--trigger-topic YOUR_TOPIC_NAME
Use the --runtime
flag to specify the runtime ID of a
supported Go version to run
your function.
Java
gcloud functions deploy java-pubsub-function \ --entry-point functions.HelloPubSub \ --runtime java17 \ --memory 512MB \
--trigger-topic YOUR_TOPIC_NAME
Use the --runtime
flag to specify the runtime ID of a
supported Java version to run
your function.
Ruby
gcloud functions deploy hello_pubsub --runtime ruby33 \
--trigger-topic YOUR_TOPIC_NAME
Use the --runtime
flag to specify the runtime ID of a
supported Ruby version to run
your function.
where YOUR_TOPIC_NAME
is the name of the Pub/Sub
topic to which the function will be subscribed.
If YOUR_TOPIC_NAME
doesn't already exist, this command
creates it for you. You can also create a topic before you run the deploy
command, using either the
Google Cloud console or the following
gcloud
command:
gcloud pubsub topics create YOUR_TOPIC_NAME
Triggering the function
Publish a message to your Pub/Sub topic. In this example, the message is a name that the function will include in a greeting:
gcloud pubsub topics publish
YOUR_TOPIC_NAME
--messageYOUR_NAME
Replace
YOUR_TOPIC_NAME
with the name of your Pub/Sub topic, andYOUR_NAME
with an arbitrary string.Check the logs to be sure the executions have completed:
gcloud functions logs read --limit 50
You can also publish a message to a Pub/Sub topic from within a function.