0
0
GCPcloud~10 mins

Pub/Sub with Cloud Functions integration in GCP - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Pub/Sub with Cloud Functions integration
Message Published to Topic
Pub/Sub Topic Receives Message
Cloud Function Triggered by Topic
Cloud Function Executes Code
Process Message & Perform Action
Function Completes Execution
A message is sent to a Pub/Sub topic, which triggers a Cloud Function subscribed to that topic. The function runs code to process the message.
Execution Sample
GCP
def pubsub_trigger(event, context):
    message = event['data']
    print(f"Received message: {message}")
This Cloud Function runs when a Pub/Sub message arrives, printing the message content.
Process Table
StepEvent ReceivedMessage DataFunction ActionOutput
1Message published to topicHello, Cloud!Trigger functionFunction starts
2Function triggeredHello, Cloud!Decode messageMessage decoded
3Function runningHello, Cloud!Print messageOutput: Received message: Hello, Cloud!
4Function completesHello, Cloud!End executionFunction ends
💡 Function completes after processing the Pub/Sub message
Status Tracker
VariableStartAfter Step 2After Step 3Final
event['data']NoneHello, Cloud!Hello, Cloud!Hello, Cloud!
messageNoneHello, Cloud!Hello, Cloud!Hello, Cloud!
Key Moments - 2 Insights
Why does the function trigger automatically when a message is published?
Because the Cloud Function is configured to listen to the Pub/Sub topic, so any new message triggers it automatically as shown in execution_table step 1 and 2.
What is the format of the message received inside the function?
The message is base64-encoded in the event data, so the function decodes it before use, as seen in execution_table step 2.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the function output at step 3?
AFunction starts
BMessage decoded
COutput: Received message: Hello, Cloud!
DFunction ends
💡 Hint
Check the 'Output' column at step 3 in the execution_table
At which step does the function decode the message data?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the 'Function Action' column in execution_table for decoding
If the message data was empty, what would change in the variable_tracker?
Amessage variable would be None or empty after step 2
Bmessage variable would still be 'Hello, Cloud!'
Cevent['data'] would have a value
DNo change in variables
💡 Hint
Refer to variable_tracker rows for 'message' and 'event["data"]' values
Concept Snapshot
Pub/Sub sends messages to topics.
Cloud Functions can subscribe to these topics.
When a message arrives, the function triggers automatically.
The function receives event data with the message.
Decode and process the message inside the function.
Function completes after processing.
Full Transcript
In this concept, a message is published to a Pub/Sub topic. This triggers a Cloud Function that is subscribed to that topic. The function receives the event containing the message data, decodes it, and processes it. The execution table shows each step: message publishing, function triggering, decoding, printing the message, and function completion. Variables like 'message' hold the decoded data throughout execution. Key points include automatic triggering by Pub/Sub and decoding the base64 message data. The visual quiz tests understanding of these steps and variable changes.