- 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
Table of Contents
-
Challenge
Enable Required APIs
- Use the API Library to find and enable the Cloud Pub/Sub API.
- Use the API Library to find and enable the Cloud Functions API.
- Use the API Library to find and enable the Cloud Build API.
-
Challenge
Create Pub/Sub Topic
- From the main console navigation, go to Cloud Pub/Sub (Now found under Analytics, Pub/Sub)
- Click Create a topic.
- In the dialog, enter a name greetings for the topic.
- Click Create.
-
Challenge
Create a Cloud Function
- From the main console, go to Cloud Functions.
- Click Create function.
- Configure the function with the following values:
- Name: acg-pubsub-function
- Trigger: Cloud Pub/Sub
- Topic: [Topic just created]
- Click Save.
- Make sure to expand out the "Runtime, build, connections and security settings" section, and set the "Maximum number of instances" to 1.
- Click Next.
- Runtime: Python 3.9
- Source code: Inline editor
- 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))
- Set Entry Point to greetings_pubsub.
- Click Deploy.
-
Challenge
Publish Message to Topic From Console
- Click the newly created Cloud Function name.
- Switch to Trigger.
- Click the topic link to go to the Cloud Pub/Sub topic.
- From the Topic page, click Publish Message.
- In the Message field, enter everyone around the world.
- Click Publish.
-
Challenge
Confirm Cloud Function Execution
- Return to the Cloud Functions dashboard.
- Click the Cloud Function's name.
- From the Cloud Function navigation, click Logs.
- Locate the most recent log.
- Confirm function execution.
-
Challenge
Trigger Cloud Function Directly From Command Line
-
Open the Projects dialog and copy the current project ID.
-
Click Activate Cloud Shell from the top row of the console.
-
In the Cloud Shell, enter the following code: gcloud config set project [PROJECT_ID]
-
Next, enter the following code:
DATA=$(printf 'my friends' | base64) gcloud functions call acg-pubsub-function --data '{"data":"'$DATA'"}'
-
After a moment, refresh the logs page and confirm the Cloud Function execution.
-
-
Challenge
Publish Message to Topic From Command Line
-
In the Cloud Shell, enter the following command:
gcloud pubsub topics publish greetings --message "y'all"
-
After a moment, refresh the log page to confirm the function has executed.
-
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.