Triggering Cloud Functions with Google Cloud Pub/Sub

30 minutes
  • 4 Learning Objectives

About this Hands-on Lab

Cloud Functions can be triggered in two ways: through a direct HTTP request or through a background event. One of the most frequently used services for background events is Cloud Pub/Sub, Google Cloud’s platform-wide messaging service. In this pairing, Cloud Functions becomes a direct subscriber to a specific Cloud Pub/Sub topic, which allows code to be run whenever a message is received on a specific topic. In this hands-on lab, we’ll walk through the entire process from deploying the function to testing it by publishing messages to Pub/Sub.

Learning Objectives

Successfully complete this lab by achieving the following learning objectives:

Enable APIs
  1. Inside the GCP console, click the Activate Cloud Shell icon.
  2. When the terminal is ready, enable the following necessary APIs:,, and
Create the Pub/Sub Topic
  1. From the GCP menu, select Pub/Sub in the Big Data section.
  2. Click Create Topic.
  3. Enter "greetings" in the Topic ID, and click Create Topic.
Create the Cloud Function
  1. From the GCP menu, select Cloud Functions in the Compute section. Then click Create Function.
  2. Under Basics, change the function name to "greetings".
  3. Under Trigger, change the trigger type to Cloud Pub/Sub, and choose the topic you just created.
  4. Click Save. Then click Next.
  5. Change the Runtime to Python 3.7 and set the Entry Point to "greetings".
  6. Enter the following code into the editor:

    import base64
    def greetings(data, context):
        if 'data' in data:
            name = base64.b64decode(data['data']).decode('utf-8')
            name = 'folks'
        print('Hello {} from Cloud Functions!'.format(name))
  7. Click Deploy.
Publish Messages to Test the Function
  1. From the GCP menu, select Pub/Sub in the Big Data section.
  2. Select the greetings topic and click Publish Message.
  3. Under Message Body, enter the text "Cloud Gurus".
  4. Click Publish.
  5. Go back to Cloud Functions (in the Compute section of the GCP menu), and select the greetings function.
  6. Click the Logs tab and look in the logs for the function. You should see the message: "Hello Cloud Gurus from Cloud Functions!"
  7. Now publish a message from the Cloud Shell terminal. After a few minutes, the logs should refresh to show the message "Hello Everyone from Cloud Functions!"
  8. You can also trigger the function directly with gcloud, once you have created a base64 payload.

Additional Resources

Log in to Google Cloud Platform by opening in a private browser window. Then, sign in using the credentials provided on the lab page.

What are Hands-on Labs

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Sign In
Welcome Back!

Psst…this one if you’ve been moved to ACG!

Get Started
Who’s going to be learning?