Featured resource
2025 Tech Upskilling Playbook
Tech Upskilling Playbook

Build future-ready tech teams and hit key business milestones with seven proven plays from industry leaders.

Check it out
  • Lab
    • Libraries: If you want this lab, consider one of these libraries.
    • Cloud
Labs

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.

Lab platform
Lab Info
Level
Beginner
Last updated
Sep 22, 2025
Duration
30m

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

About the author

Pluralsight Skills gives leaders confidence they have the skills needed to execute technology strategy. Technology teams can benchmark expertise across roles, speed up release cycles and build reliable, secure products. By leveraging our expert content, skill assessments and one-of-a-kind analytics, keep up with the pace of change, put the right people on the right projects and boost productivity. It's the most effective path to developing tech skills at scale.

Real skill practice before real-world application

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.

Learn by doing

Engage hands-on with the tools and technologies you’re learning. You pick the skill, we provide the credentials and environment.

Follow your guide

All labs have detailed instructions and objectives, guiding you through the learning process and ensuring you understand every step.

Turn time into mastery

On average, you retain 75% more of your learning if you take time to practice. Hands-on labs set you up for success to make those skills stick.

Get started with Pluralsight