0
0
GCPcloud~30 mins

Cloud Functions with Pub/Sub triggers in GCP - Mini Project: Build & Apply

Choose your learning style9 modes available
Cloud Functions with Pub/Sub triggers
📖 Scenario: You are building a simple cloud function that reacts to messages published on a Pub/Sub topic. This function will process incoming messages and log their content.
🎯 Goal: Create a Google Cloud Function triggered by a Pub/Sub topic. The function should receive messages, decode them, and log the message data.
📋 What You'll Learn
Create a Pub/Sub topic named my-topic
Create a Cloud Function named process_pubsub_message
Configure the Cloud Function to trigger on messages published to my-topic
The function should decode the base64 message data and log it
💡 Why This Matters
🌍 Real World
Cloud Functions triggered by Pub/Sub are commonly used to process events asynchronously, such as processing user uploads, notifications, or IoT device messages.
💼 Career
Understanding how to connect Pub/Sub with Cloud Functions is essential for cloud developers and engineers working on event-driven architectures in Google Cloud.
Progress0 / 4 steps
1
Create the Pub/Sub topic
Create a Pub/Sub topic named my-topic using the gcloud CLI command.
GCP
Need a hint?

Use the gcloud pubsub topics create command followed by the topic name.

2
Write the Cloud Function code
Write a Python function named process_pubsub_message that accepts event and context parameters. Inside the function, decode the base64 message data from event['data'] and assign it to a variable called message.
GCP
Need a hint?

Use the base64 module to decode event['data'] and then decode bytes to a UTF-8 string.

3
Add logging of the message
Inside the process_pubsub_message function, import the logging module and add a line to log the decoded message using logging.info().
GCP
Need a hint?

Use logging.info() to log the message with a descriptive text.

4
Deploy the Cloud Function with Pub/Sub trigger
Deploy the Cloud Function named process_pubsub_message using the gcloud CLI. Set the runtime to python39, the trigger to the Pub/Sub topic my-topic, and the entry point to process_pubsub_message.
GCP
Need a hint?

Use gcloud functions deploy with the correct flags for runtime, trigger-topic, and entry-point.