This simple tutorial demonstrates writing, deploying, and triggering an Event-Driven Cloud Run functions 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.
Objectives
- Write and deploy an Event-Driven Cloud Run function.
- Trigger the function by publishing a message to a Pub/Sub topic.
Costs
In this document, you use the following billable components of Google Cloud:
- Cloud Run functions
- Cloud Build
- Pub/Sub
- Artifact Registry
- Eventarc
- Cloud Logging
For details, see Cloud Run functions pricing.
To generate a cost estimate based on your projected usage,
use the pricing calculator.
Before you begin
- Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Cloud Functions, Cloud Build, Artifact Registry, Eventarc, Logging, and Pub/Sub APIs.
- Install the Google Cloud CLI.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Cloud Functions, Cloud Build, Artifact Registry, Eventarc, Logging, and Pub/Sub APIs.
- Install the Google Cloud CLI.
-
To initialize the gcloud CLI, run the following command:
gcloud init
- Prepare your development environment.
If you already have the gcloud CLI installed, update it by running the following command:
gcloud components update
Prerequisites
Create a Pub/Sub topic:
gcloud pubsub topics create YOUR_TOPIC_NAME
This is a mandatory step before you can deploy your function. In Cloud Run functions, Pub/Sub topics are not automatically created when you deploy a function.
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.
C#
git clone https://github.com/GoogleCloudPlatform/dotnet-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.
PHP
git clone https://github.com/GoogleCloudPlatform/php-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/v2/helloPubSub/
Python
cd python-docs-samples/functions/v2/pubsub/
Go
cd golang-samples/functions/functionsv2/hellopubsub/
Java
cd java-docs-samples/functions/v2/pubsub/
C#
cd dotnet-docs-samples/functions/helloworld/HelloPubSub/
Ruby
cd ruby-docs-samples/functions/helloworld/pubsub/
PHP
cd php-docs-samples/functions/helloworld_pubsub/
Take a look at the sample code:
Node.js
Python
Go
Java
C#
Ruby
PHP
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 nodejs-pubsub-function \ --gen2 \ --runtime=nodejs20 \ --region=REGION
\ --source=. \ --entry-point=helloPubSub \
--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 python-pubsub-function \ --gen2 \ --runtime=python312 \ --region=REGION
\ --source=. \ --entry-point=subscribe \
--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 go-pubsub-function \ --gen2 \ --runtime=go121 \ --region=REGION
\ --source=. \ --entry-point=HelloPubSub \
--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 \ --gen2 \ --runtime=java17 \ --region=REGION
\ --source=. \ --entry-point=functions.SubscribeToTopic \ --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.
C#
gcloud functions deploy csharp-pubsub-function \ --gen2 \ --runtime=dotnet6 \ --region=REGION
\ --source=. \ --entry-point=HelloPubSub.Function \
--trigger-topic=YOUR_TOPIC_NAME
Use the --runtime
flag to specify the runtime ID of a
supported .NET version to run
your function.
Ruby
gcloud functions deploy ruby-pubsub-function \ --gen2 \ --runtime=ruby32 \ --region=REGION
\ --source=. \ --entry-point=hello_pubsub \
--trigger-topic=YOUR_TOPIC_NAME
Use the --runtime
flag to specify the runtime ID of a
supported Ruby version to run
your function.
PHP
gcloud functions deploy php-pubsub-function \ --gen2 \ --runtime=php82 \ --region=REGION
\ --source=. \ --entry-point=helloworldPubsub \
--trigger-topic=YOUR_TOPIC_NAME
Use the --runtime
flag to specify the runtime ID of a
supported PHP version to run
your function.
where YOUR_TOPIC_NAME
is the name of the Pub/Sub
topic to which the function will be subscribed.
You must create a topic before you run the deploy
command, using either the
Google Cloud console or the
gcloud
command-line tool. Unlike Cloud Run functions (1st gen),
in Cloud Run functions, Pub/Sub topics are not automatically created
when you deploy a function with a Pub/Sub trigger.
Triggering the function
To test the Pub/Sub function:
Publish a message to the topic:
gcloud pubsub topics publish my-topic --message="Friend"
Read the function logs to see the result:
gcloud functions logs read \ --gen2 \ --region=REGION \ --limit=5 \ FUNCTION_NAME
Replace the following:
- REGION is the name of the Google Cloud region where you deployed your function
(for example,
us-west1
). - FUNCTION_NAME is the name you gave your
function when you deployed it (for example, the
Node.js function in this tutorial was
deployed with the function name
nodejs-pubsub-function
).
You should see logging output that includes your new "Friend" message.
- REGION is the name of the Google Cloud region where you deployed your function
(for example,
Clean up
To avoid incurring charges to your Google Cloud account for the resources used in this tutorial, either delete the project that contains the resources, or keep the project and delete the individual resources.
Deleting the project
The easiest way to eliminate billing is to delete the project that you created for the tutorial.
To delete the project:
- In the Google Cloud console, go to the Manage resources page.
- In the project list, select the project that you want to delete, and then click Delete.
- In the dialog, type the project ID, and then click Shut down to delete the project.
Deleting the Cloud Run function
Deleting Cloud Run functions does not remove any resources stored in Cloud Storage.
To delete the Cloud Run function you created in this tutorial, run the following command:
Node.js
gcloud functions delete nodejs-pubsub-function --gen2 --region REGION
Python
gcloud functions delete python-pubsub-function --gen2 --region REGION
Go
gcloud functions delete go-pubsub-function --gen2 --region REGION
Java
gcloud functions delete java-pubsub-function --gen2 --region REGION
C#
gcloud functions delete csharp-pubsub-function --gen2 --region REGION
Ruby
gcloud functions delete ruby-pubsub-function --gen2 --region REGION
PHP
gcloud functions delete php-pubsub-function --gen2 --region REGION
You can also delete Cloud Run functions from the Google Cloud console.