0
0
GCPcloud~10 mins

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

Choose your learning style9 modes available
Process Flow - Cloud Functions with Pub/Sub triggers
Pub/Sub Topic receives message
Trigger Cloud Function
Cloud Function executes code
Process message data
Function completes and acknowledges message
When a message is published to a Pub/Sub topic, it triggers the Cloud Function to run and process that 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 and prints the message data.
Process Table
StepEvent ReceivedMessage DataActionOutput
1Message published to topicSGVsbG8gd29ybGQ=Trigger functionFunction starts
2Function triggeredSGVsbG8gd29ybGQ=Decode messageDecoded to 'Hello world'
3Processing messageHello worldPrint messageOutput: Received message: Hello world
4Function completesHello worldAcknowledge messageMessage acknowledged
5No new messagesN/AWait for next messageIdle
💡 No new messages, function waits for next trigger
Status Tracker
VariableStartAfter Step 2After Step 3Final
event['data']SGVsbG8gd29ybGQ=SGVsbG8gd29ybGQ=Hello worldHello world
messageundefinedSGVsbG8gd29ybGQ=Hello worldHello world
Key Moments - 3 Insights
Why does the message data look like random letters before decoding?
The message data is base64 encoded when received (see execution_table step 2). It must be decoded to get the original text.
What triggers the Cloud Function to run?
Publishing a message to the Pub/Sub topic triggers the function immediately (execution_table step 1).
What happens if no messages are published?
The function stays idle waiting for the next message (execution_table step 5).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the message data after decoding at step 2?
ASGVsbG8gd29ybGQ=
BHello world
CReceived message
DN/A
💡 Hint
Check the 'Message Data' column at step 2 in the execution_table
At which step does the function acknowledge the message?
AStep 1
BStep 3
CStep 4
DStep 5
💡 Hint
Look at the 'Action' column for message acknowledgement in the execution_table
If no messages are published, what is the function's state according to the execution table?
AFunction starts
BIdle, waiting for next message
CProcessing message
DMessage acknowledged
💡 Hint
Refer to the last row in the execution_table under 'Output'
Concept Snapshot
Cloud Functions with Pub/Sub triggers:
- Pub/Sub topic receives message
- Message triggers Cloud Function
- Function decodes and processes message
- Function acknowledges message
- Waits idle if no messages
Full Transcript
When a message is published to a Pub/Sub topic, it triggers the associated Cloud Function to run. The function receives the event containing the message data, which is base64 encoded. The function decodes this data to get the original message text. It then processes the message, for example by printing it. After processing, the function acknowledges the message to Pub/Sub. If no messages are published, the function remains idle, waiting for the next trigger.