Triggering a Cloud Function with Cloud Pub/Sub

30 minutes
  • 7 Learning Objectives

About this Hands-on Lab

Basically, Cloud Functions can either be triggered directly via HTTP or indirectly via a cloud event. One of the most frequently used services for cloud events is Cloud Pub/Sub, Google Cloud’s platform-wide messaging service. Here, 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 experience, from setup to confirmation using both the console and the command line.

Learning Objectives

Successfully complete this lab by achieving the following learning objectives:

Enable required APIs.
  1. Use the API Library to find and enable the Cloud Pub/Sub API.
  2. Use the API Library to find and enable the Cloud Functions API.
  3. Use the API Library to find and enable the Cloud Build API.
Create Pub/Sub topic.
  1. From the main console navigation, go to Cloud Pub/Sub.
  2. Click Create a topic.
  3. In the dialog, enter a name greetings for the topic.
  4. Click Create.
Create a Cloud Function.
  1. From the main console, go to Cloud Functions.
    1. Click Create function.
    2. Configure the function with the following values:
      • Name: acg-pubsub-function
      • Trigger: Cloud Pub/Sub
      • Topic: [Topic just created]
  2. Click Save and then Next.
    • Runtime: Python 3.9
    • Source code: Inline editor
  3. In the main.py field, enter the following code:

    
        import base64
    
    def greetings_pubsub(data, context):
    
        if 'data' in data:
            name = base64.b64decode(data['data']).decode('utf-8')
        else:
            name = 'folks'
        print('Greetings {} from A Cloud Guru!'.format(name))
    
    6. Set Entry Point to **greetings_pubsub**.
    7. Click **Deploy**.
Publish message to topic from console.
  1. Click the newly created Cloud Function name.
  2. Switch to Trigger.
  3. Click the topic link to go to the Cloud Pub/Sub topic.
  4. From the Topic page, click Publish Message.
  5. In the Message field, enter everyone around the world.
  6. Click Publish.
Confirm Cloud Function execution.
  1. Return to the Cloud Functions dashboard.
  2. Click the Cloud Function’s name.
  3. From the Cloud Function navigation, click Logs.
  4. Locate the most recent log.
  5. Confirm function execution.
Trigger Cloud Function directly from command line.
  1. Open the Projects dialog and copy the current project ID.

  2. Click Activate Cloud Shell from the top row of the console.

  3. In the Cloud Shell, enter the following code:
    gcloud config set project [PROJECT_ID]

  4. Next, enter the following code:

    DATA=$(printf 'my friends' | base64)
    gcloud functions call acg-pubsub-function --data '{"data":"'$DATA'"}'
  5. After a moment, refresh the logs page and confirm the Cloud Function execution.

Publish message to topic from command line.
  1. In the Cloud Shell, enter the following command:

    gcloud pubsub topics publish greetings --message "y'all"
  2. After a moment, refresh the log page to confirm the function has executed.

Additional Resources

You’ve been asked to lay the groundwork for one aspect of the new app that relies on code being executed when a message is received from any number of outlets. To do this, you’ll need to establish a Cloud Pub/Sub topic and then create an example Cloud Function that uses the topic as a trigger. Naturally, you’ll need to confirm the Cloud Function has successfully executed.

You’ll need to accomplish the following steps to complete your task:

  1. Enable APIs.
  2. Create Pub/Sub topic.
  3. Create Cloud Function.
  4. Publish message on topic.
  5. Confirm Cloud Function executed via logs.
  6. Trigger directly via the command line.
  7. Confirm Cloud Function executed via logs.
  8. Send message to topic from command line.
  9. Confirm Cloud Function executed via logs.

Note: In addition to the lab mentioned libraries, use the API Library to find and enable the Cloud Build API.

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?