📖 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
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
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
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
Hint
Use gcloud functions deploy with the correct flags for runtime, trigger-topic, and entry-point.
Practice
(1/5)
1. What happens when a Google Cloud Function is triggered by a Pub/Sub message?
easy
A. The function runs automatically when a new message arrives on the Pub/Sub topic.
B. The function must be manually started after the message is published.
C. The function only runs if the message contains a specific keyword.
D. The function runs only once a day regardless of messages.
Solution
Step 1: Understand Pub/Sub trigger behavior
Cloud Functions connected to Pub/Sub topics run automatically when a new message is published to that topic.
Step 2: Confirm automatic execution
There is no need for manual start or keyword filtering by default; the function triggers on every message.
Final Answer:
The function runs automatically when a new message arrives on the Pub/Sub topic. -> Option A
Quick Check:
Pub/Sub triggers auto-run functions on new messages [OK]
Hint: Pub/Sub triggers run functions automatically on new messages [OK]
Common Mistakes:
Thinking the function needs manual start
Assuming filtering happens automatically
Believing the function runs on a schedule
2. Which of the following is the correct way to declare a Cloud Function triggered by Pub/Sub in Node.js?
easy
A. exports.myFunction = (event, context) => { console.log(event.data); };
B. function myFunction(pubsubMessage) { console.log(pubsubMessage.data); }
C. exports.myFunction = (req, res) => { res.send('Hello'); };
D. function myFunction(event) { return event.data; }
Solution
Step 1: Identify correct Cloud Function signature for Pub/Sub
In Node.js, a Pub/Sub triggered function uses (event, context) parameters, where event.data contains the message.
Step 2: Check options for correct syntax
exports.myFunction = (event, context) => { console.log(event.data); }; uses exports with (event, context) and accesses event.data correctly. Others either use wrong parameters or HTTP function style.
What will be printed if the Pub/Sub message data is base64 encoding of 'Hello World'?
medium
A. Received message: event['data']
B. Received message: b'Hello World'
C. Received message: Hello World
D. Error: base64 decoding failed
Solution
Step 1: Decode base64 message data
The function decodes event['data'] from base64 to bytes, then decodes bytes to UTF-8 string.
Step 2: Print the decoded message
The decoded string is 'Hello World', so the print statement outputs 'Received message: Hello World'.
Final Answer:
Received message: Hello World -> Option C
Quick Check:
Base64 decode then UTF-8 decode gives original message [OK]
Hint: Base64 decode Pub/Sub data to get original message text [OK]
Common Mistakes:
Printing raw base64 string instead of decoded
Forgetting to decode bytes to string
Assuming event['data'] is plain text
4. You deployed a Cloud Function triggered by Pub/Sub, but it never runs when messages are published. What is the most likely cause?
medium
A. Cloud Functions do not support Pub/Sub triggers.
B. The function code has a syntax error.
C. The Pub/Sub topic is empty and cannot trigger functions.
D. The function is not subscribed to the correct Pub/Sub topic.
Solution
Step 1: Check Pub/Sub subscription linkage
If the function is not linked to the correct Pub/Sub topic, it will not trigger on published messages.
Step 2: Eliminate other causes
Syntax errors cause deployment failure, empty topics can still trigger if messages arrive, and Cloud Functions do support Pub/Sub triggers.
Final Answer:
The function is not subscribed to the correct Pub/Sub topic. -> Option D
Quick Check:
Correct topic subscription is required for triggers [OK]
Hint: Verify function subscribes to the right Pub/Sub topic [OK]
Common Mistakes:
Assuming empty topic blocks triggers
Ignoring subscription configuration
Thinking Cloud Functions lack Pub/Sub support
5. You want to ensure your Cloud Function triggered by Pub/Sub processes messages exactly once, even if Pub/Sub retries delivery. What is the best approach?
hard
A. Disable retries in Pub/Sub settings to avoid duplicate triggers.
B. Implement idempotent processing in the function using unique message IDs.
C. Use a global variable in the function to track processed messages.
D. Rely on Pub/Sub to guarantee exactly-once delivery automatically.
Solution
Step 1: Understand Pub/Sub delivery guarantees
Pub/Sub guarantees at-least-once delivery, so duplicates can occur if retries happen.
Step 2: Implement idempotency in function
To handle duplicates, the function should process messages idempotently, often by checking unique message IDs to avoid repeated work.
Step 3: Evaluate other options
Disabling retries risks message loss, global variables don't persist across executions, and Pub/Sub does not guarantee exactly-once delivery by itself.
Final Answer:
Implement idempotent processing in the function using unique message IDs. -> Option B
Quick Check:
Idempotent code handles retries safely [OK]
Hint: Make function idempotent using message IDs to avoid duplicates [OK]
Common Mistakes:
Expecting Pub/Sub to do exactly-once delivery
Disabling retries risking lost messages
Using non-persistent variables for tracking
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.