Skip to main content

Triggering a Cloud Function with Cloud Pub/Sub

Hands-On Lab

 

Photo of Joseph Lowery

Joseph Lowery

Google Cloud Training Architect II in Content

Length

00:30:00

Difficulty

Beginner

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 experience, from setup to confirmation.

What are Hands-On Labs?

Hands-On Labs are scenario-based learning environments where learners can practice without consequences. Don't compromise a system or waste money on expensive downloads. Practice real-world skills without the real-world risk, no assembly required.

Triggering a Cloud Function with Cloud Pub/Sub

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 experience, from setup to confirmation.

How to Log in to Google Lab Accounts

On the lab page, right-click Open GCP Console and select the option to open it in a new private browser window (this option will read differently depending on your browser — e.g., in Chrome, it says "Open Link in Incognito Window"). Then, sign in to Google Cloud Platform using the credentials provided on the lab page.

On the Welcome to your new account screen, review the text, and click Accept. In the "Welcome L.A.!" pop-up once you're signed in, check to agree to the terms of service, choose your country of residence, and click Agree and Continue.

Triggering a Cloud Function with Cloud Pub/Sub

Now, on to the lab!

Enable required APIs.

  1. From the main console navigation, go to APIs and Services > Library.
  2. Search for Pub/Sub.
  3. Select the Cloud Pub/Sub API card.
  4. Click Enable, if displayed.
  5. Repeat the above steps to enable the Cloud Functions API as well.

Create Pub/Sub topic.

  1. Navigate to Pub/Sub > Topics.
  2. Click Create a topic.
  3. In the dialog, enter a name for the topic (e.g., "greetings").
  4. Click Create.

Create a Cloud Function.

  1. Navigate to Cloud Functions.

  2. Click Create function.

  3. Configure the function with the following values:

    • Name: la-pubsub-function
    • Trigger: Cloud Pub/Sub
    • Topic: The listed topic you just created
    • Source code: Inline editor
    • Runtime: Python 3.7
  4. 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))
  5. Set Function to execute to greetings_pubsub.

  6. Click Create.

Publish message to topic from console.

  1. Click the newly created Cloud Function name.
  2. Switch to the Trigger tab.
  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 View Logs.
  4. Locate the most recent log.
  5. Confirm function execution.

Trigger Cloud Function directly from command line.

  1. Click the icon in the top row of the console to activate Cloud Shell.

  2. In the Cloud Shell, enter the following code to set a variable called DATA:

    DATA=$(printf 'my friends' | base64)
  3. Enter the following to trigger the function:

    gcloud functions call la-pubsub-function --data '{"data":"'$DATA'"}'
  4. 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.

Conclusion

Congratulations on completing this hands-on lab!