Cloud Pub/Sub Tutorial

This simple tutorial demonstrates writing, deploying, and triggering a Background Cloud Function with a Cloud Pub/Sub trigger.

Objectives

Costs

This tutorial uses billable components of Cloud Platform, including:

  • Google Cloud Functions
  • Google Cloud 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

  1. Sign in to your Google account.

    If you don't already have one, sign up for a new account.

  2. Select or create a Cloud Platform project.

    Go to the Projects page

  3. Enable billing for your project.

    Enable billing

  4. Enable the Cloud Functions and Cloud Pub/Sub APIs.

    Enable the APIs

  5. Install and initialize the Cloud SDK.
  6. Update and install gcloud components:
    gcloud components update &&
    gcloud components install beta
  7. Prepare your environment for Node.js development.

    Go to the setup guide

Preparing the application

  1. Create a Cloud Storage bucket to stage your Cloud Functions files, where [YOUR_STAGING_BUCKET_NAME] is a globally unique bucket name:

    gsutil mb gs://[YOUR_STAGING_BUCKET_NAME]
    

  2. Create a directory on your local system for the application code:

    Linux or Mac OS X

    Create the directory:

    mkdir ~/gcf_pubsub

    Move into the directory:

    cd ~/gcf_pubsub

    Windows

    Create the directory:

    mkdir %HOMEPATH%\gcf_pubsub

    Move into the directory:

    cd %HOMEPATH%\gcf_pubsub

  3. Create an index.js file in the gcf_pubsub directory with the following contents:

    Node.js

    /**
     * Background Cloud Function to be triggered by Pub/Sub.
     *
     * @param {object} event The Cloud Functions event.
     * @param {function} The callback function.
     */
    exports.helloPubSub = function (event, callback) {
      const pubsubMessage = event.data;
      const name = pubsubMessage.data ? Buffer.from(pubsubMessage.data, 'base64').toString() : 'World';
    
      console.log(`Hello, ${name}!`);
    
      callback();
    };

    The helloPubSub function is exported by the module and is executed when a message is published to the function's trigger topic.

Deploying the function

To deploy the helloPubSub function with a Cloud Pub/Sub trigger, run the following command in the gcf_pubsub directory:

gcloud beta functions deploy helloPubSub --stage-bucket [YOUR_STAGING_BUCKET_NAME] --trigger-topic [YOUR_TOPIC_NAME]

where

  • [YOUR_STAGING_BUCKET_NAME] is the name of your staging Cloud Storage bucket.
  • [YOUR_TOPIC_NAME] is the name of your Cloud Pub/Sub topic to which you will publish a message, e.g. my-topic.

Triggering the function

  1. Publish a message to your Cloud Pub/Sub topic:

    gcloud beta pubsub topics publish [YOUR_TOPIC_NAME] '{"name":"helloPubSub"}'
    

    where [YOUR_TOPIC_NAME] is the name of your Cloud Pub/Sub topic, e.g. my-topic.

  2. Watch the logs to be sure the executions have completed:

    gcloud beta functions logs read --limit 50
    

Cleaning up

To avoid incurring charges to your Google Cloud Platform account for the resources used in this tutorial:

Deleting the project

The easiest way to eliminate billing is to delete the project you created for the tutorial.

To delete the project:

  1. In the Cloud Platform Console, go to the Projects page.

    Go to the Projects page

  2. In the project list, select the project you want to delete and click Delete project. After selecting the checkbox next to the project name, click
      Delete project
  3. In the dialog, type the project ID, and then click Shut down to delete the project.

Deleting the Cloud Functions

Deleting Cloud Functions does not remove any resources stored in Cloud Storage.

To delete a Cloud Function, run the following command:

gcloud beta functions delete [NAME_OF_FUNCTION]

where [NAME_OF_FUNCTION] is the name of the function to delete.

You can also delete Cloud Functions from the Google Cloud Platform Console.

Send feedback about...

Cloud Functions Documentation