Bird
Raised Fist0
GCPcloud~7 mins

Cloud Functions with Pub/Sub triggers in GCP - Commands & Configuration

Choose your learning style10 modes available
Introduction
Sometimes you want your code to run automatically when a message is sent to a topic. Cloud Functions with Pub/Sub triggers let you do this by running your code whenever a new message arrives on a Pub/Sub topic.
When you want to process data as soon as it is published to a messaging topic without manual intervention.
When you need to run small pieces of code in response to events from other services or applications.
When you want to decouple your application components so they communicate through messages.
When you want to automatically trigger workflows based on messages sent by other systems.
When you want to scale your processing automatically based on the number of messages.
Config File - function-source/main.py
function-source/main.py
def hello_pubsub(event, context):
    import base64
    if 'data' in event:
        message = base64.b64decode(event['data']).decode('utf-8')
        print(f'Received message: {message}')
    else:
        print('No data in message')

This Python file contains the Cloud Function code. The hello_pubsub function runs when a message arrives. It decodes the message data from base64 and prints it. If no data is present, it prints a different message.

Commands
Create a Pub/Sub topic named 'example-topic' where messages will be sent to trigger the function.
Terminal
gcloud pubsub topics create example-topic
Expected OutputExpected
Created topic [projects/your-project-id/topics/example-topic].
Deploy the Cloud Function named 'helloPubSubFunction' using Python 3.10. It triggers on messages published to 'example-topic'. The function entry point is 'hello_pubsub'.
Terminal
gcloud functions deploy helloPubSubFunction --runtime python310 --trigger-topic example-topic --entry-point hello_pubsub --region us-central1 --quiet
Expected OutputExpected
Deploying function (may take a while)... Deploying function (helloPubSubFunction)...done. availableMemoryMb: 256 entryPoint: hello_pubsub name: projects/your-project-id/locations/us-central1/functions/helloPubSubFunction runtime: python310 status: ACTIVE trigger: eventType: google.pubsub.topic.publish resource: projects/your-project-id/topics/example-topic
→
--runtime - Specifies the language runtime version.
→
--trigger-topic - Sets the Pub/Sub topic that triggers the function.
→
--entry-point - Defines the function name in the source code.
Send a message 'Hello, Cloud Functions!' to the 'example-topic' to trigger the Cloud Function.
Terminal
gcloud pubsub topics publish example-topic --message "Hello, Cloud Functions!"
Expected OutputExpected
messageIds: - "1234567890123456"
→
--message - Specifies the message content to publish.
Check the logs of the Cloud Function to see the output from the triggered function.
Terminal
gcloud functions logs read helloPubSubFunction --region us-central1 --limit 5
Expected OutputExpected
2024-06-01 12:00:00 helloPubSubFunction: Received message: Hello, Cloud Functions! 2024-06-01 11:59:00 helloPubSubFunction: Function execution started 2024-06-01 11:59:01 helloPubSubFunction: Function execution finished
→
--limit - Limits the number of log entries shown.
Key Concept

If you remember nothing else from this pattern, remember: Cloud Functions run your code automatically whenever a message is sent to a Pub/Sub topic you choose.

Common Mistakes
Deploying the function without specifying the correct trigger topic.
The function will not run because it is not connected to any Pub/Sub topic.
Always use the --trigger-topic flag with the exact topic name when deploying the function.
Publishing messages to a different topic than the one the function listens to.
The function will not be triggered because it listens only to its configured topic.
Publish messages only to the topic that triggers your function.
Not decoding the base64 message data inside the function.
The function will print unreadable data instead of the actual message content.
Decode the message data from base64 before using it.
Summary
Create a Pub/Sub topic to send messages.
Deploy a Cloud Function triggered by that topic with your code.
Publish messages to the topic to automatically run your function.
Check function logs to see the output and confirm it ran.

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

  1. 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.
  2. Step 2: Confirm automatic execution

    There is no need for manual start or keyword filtering by default; the function triggers on every message.
  3. Final Answer:

    The function runs automatically when a new message arrives on the Pub/Sub topic. -> Option A
  4. 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

  1. 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.
  2. 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.
  3. Final Answer:

    exports.myFunction = (event, context) => { console.log(event.data); }; -> Option A
  4. Quick Check:

    Pub/Sub functions use (event, context) signature [OK]
Hint: Pub/Sub functions use (event, context) parameters in Node.js [OK]
Common Mistakes:
  • Using HTTP function signature (req, res)
  • Missing exports keyword
  • Using wrong parameter names
3. Given this Python Cloud Function triggered by Pub/Sub:
def hello_pubsub(event, context):
    import base64
    message = base64.b64decode(event['data']).decode('utf-8')
    print(f'Received message: {message}')

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

  1. Step 1: Decode base64 message data

    The function decodes event['data'] from base64 to bytes, then decodes bytes to UTF-8 string.
  2. Step 2: Print the decoded message

    The decoded string is 'Hello World', so the print statement outputs 'Received message: Hello World'.
  3. Final Answer:

    Received message: Hello World -> Option C
  4. 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

  1. 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.
  2. 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.
  3. Final Answer:

    The function is not subscribed to the correct Pub/Sub topic. -> Option D
  4. 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

  1. Step 1: Understand Pub/Sub delivery guarantees

    Pub/Sub guarantees at-least-once delivery, so duplicates can occur if retries happen.
  2. 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.
  3. 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.
  4. Final Answer:

    Implement idempotent processing in the function using unique message IDs. -> Option B
  5. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard