This simple tutorial demonstrates writing, deploying, and triggering a Background Cloud Function with a Cloud 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 Google Cloud Pub/Sub Triggers for an overview of working with Pub/Sub topics and subscriptions in Cloud Functions.
If you're looking for code samples for using Pub/Sub itself, please visit the Google Cloud sample browser.
Objectives
- Write and deploy a Background Cloud Function.
- Trigger the function by publishing a message to a Pub/Sub topic.
Costs
This tutorial uses billable components of Cloud Platform, including:
- Cloud Functions
- Pub/Sub
Use the Pricing Calculator to generate a cost estimate based on your projected usage.
New Cloud Platform users might be eligible for a free trial.Before you begin
-
Sign in to your Google Account.
If you don't already have one, sign up for a new account.
-
In the Google Cloud Console, on the project selector page, select or create a Google Cloud project.
-
Projeniz için faturalandırmanın etkinleştirildiğinden emin olun.
- Enable the Cloud Functions and Cloud Pub/Sub APIs.
- Install and initialize the Cloud SDK.
- Prepare your development environment.
If you already have the Cloud SDK installed, update it by running the following command:
gcloud components update
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.
Change to the directory that contains the Cloud 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/
C#
cd dotnet-docs-samples/functions/helloworld/HelloPubSub/
Ruby
cd ruby-docs-samples/functions/helloworld/pubsub/
Take a look at the sample code:
Node.js
Python
Go
Java
C#
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 nodejs10 \You can use the following values for the
--trigger-topic YOUR_TOPIC_NAME
--runtime
flag to specify your preferred Node.js version:
nodejs10
nodejs12
nodejs14
(public preview)
Python
gcloud functions deploy hello_pubsub \ --runtime python37 \You can use the following values for the
--trigger-topic YOUR_TOPIC_NAME
--runtime
flag to specify your preferred Python version:
python37
python38
python39
(public preview)
Go
gcloud functions deploy HelloPubSub \ --runtime go111 \You can use the following values for the
--trigger-topic YOUR_TOPIC_NAME
--runtime
flag to specify your preferred Go version:
go111
go113
Java
gcloud functions deploy java-pubsub-function \ --entry-point functions.HelloPubSub \ --runtime java11 \ --memory 512MB \
--trigger-topic YOUR_TOPIC_NAME
C#
gcloud functions deploy csharp-pubsub-function \ --entry-point HelloPubSub.Function \ --runtime dotnet3 \
--trigger-topic YOUR_TOPIC_NAME
Ruby
gcloud functions deploy hello_pubsub --runtime ruby26 \You can use the following values for the
--trigger-topic YOUR_TOPIC_NAME
--runtime
flag to specify your preferred Ruby version:
ruby26
ruby27
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
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.
Cleaning 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 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 Function
Deleting Cloud Functions does not remove any resources stored in Cloud Storage.
To delete the Cloud Function you created in this tutorial, run the following command:
Node.js
gcloud functions delete helloPubSub
Python
gcloud functions delete hello_pubsub
Go
gcloud functions delete HelloPubSub
Java
gcloud functions delete java-pubsub-function
C#
gcloud functions delete csharp-pubsub-function
Ruby
gcloud functions delete hello_pubsub
You can also delete Cloud Functions from the Google Cloud Console.