Skip to content

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.
  • Labs icon Lab
  • A Cloud Guru

Triggering a Cloud Function with Cloud Pub/Sub

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.


Path Info

Clock icon Beginner
Clock icon 30m
Clock icon Mar 26, 2019

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.

Table of Contents

  1. Challenge

    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.
  2. Challenge

    Create Pub/Sub Topic

    1. From the main console navigation, go to Cloud Pub/Sub (Now found under Analytics, Pub/Sub)
    2. Click Create a topic.
    3. In the dialog, enter a name greetings for the topic.
    4. Click Create.
  3. Challenge

    Create a Cloud Function

    1. From the main console, go to Cloud Functions.
    2. Click Create function.
    3. Configure the function with the following values:
      • Name: acg-pubsub-function
      • Trigger: Cloud Pub/Sub
      • Topic: [Topic just created]
    4. Click Save.
    5. Make sure to expand out the "Runtime, build, connections and security settings" section, and set the "Maximum number of instances" to 1.
    6. Click Next.
      • Runtime: Python 3.9
      • Source code: Inline editor
    7. In the field, enter the following code:
    import base64
    def greetings_pubsub(data, context):
        if 'data' in data:
            name = base64.b64decode(data['data']).decode('utf-8')
            name = 'folks'
        print('Greetings {} from Linux Academy!'.format(name))
    1. Set Entry Point to greetings_pubsub.
    2. Click Deploy.
  4. Challenge

    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.
  5. Challenge

    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.
  6. Challenge

    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.

  7. Challenge

    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.

The Cloud Content team comprises subject matter experts hyper focused on services offered by the leading cloud vendors (AWS, GCP, and Azure), as well as cloud-related technologies such as Linux and DevOps. The team is thrilled to share their knowledge to help you build modern tech solutions from the ground up, secure and optimize your environments, and so much more!

What's a lab?

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.

Provided environment for hands-on practice

We will provide the credentials and environment necessary for you to practice right within your browser.

Guided walkthrough

Follow along with the author’s guided walkthrough and build something new in your provided environment!

Did you know?

On average, you retain 75% more of your learning if you get time for practice.

Start learning by doing today

View Plans