Bird
Raised Fist0
GCPcloud~5 mins

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

Choose your learning style10 modes available
Introduction
Sometimes you want your app to react automatically when new messages arrive. Pub/Sub sends messages, and Cloud Functions run code when messages come in, so they work together to automate tasks without you watching.
When you want to process user uploads automatically after they are sent to a storage bucket.
When you need to trigger notifications or alerts based on events happening in your app.
When you want to decouple parts of your app so they communicate by sending messages instead of direct calls.
When you want to run small pieces of code in response to events without managing servers.
When you want to scale event processing automatically as message volume changes.
Config File - function.js
function.js
exports.helloPubSub = (message, context) => {
  const pubsubMessage = message.data
    ? Buffer.from(message.data, 'base64').toString('utf-8')
    : 'Hello, World';
  console.log(pubsubMessage);
};

This is a simple Cloud Function written in JavaScript that triggers when a Pub/Sub message arrives.

exports.helloPubSub: The function name that GCP uses to run this code.

message.data: The message content, encoded in base64, decoded to a string.

console.log: Prints the message content to the function logs.

Commands
Create a Pub/Sub topic named 'example-topic' where messages will be sent.
Terminal
gcloud pubsub topics create example-topic
Expected OutputExpected
Created topic [projects/your-project-id/topics/example-topic].
Deploy the Cloud Function named 'helloPubSub' that runs Node.js 18 code, triggered by messages on 'example-topic'.
Terminal
gcloud functions deploy helloPubSub --runtime nodejs18 --trigger-topic example-topic --entry-point helloPubSub --region us-central1
Expected OutputExpected
Deploying function (may take a while)... Deploying function (helloPubSub)...done. availableMemoryMb: 256 entryPoint: helloPubSub httpsTrigger: {} name: projects/your-project-id/locations/us-central1/functions/helloPubSub runtime: nodejs18 status: ACTIVE trigger: eventType: google.pubsub.topic.publish resource: projects/your-project-id/topics/example-topic updateTime: '2024-06-01T12:00:00Z'
→
--runtime - Specifies the language runtime version.
→
--trigger-topic - Sets the Pub/Sub topic that triggers the function.
→
--entry-point - Specifies the exported function name in the code.
Send a message 'Hello from Pub/Sub!' to the 'example-topic' to trigger the Cloud Function.
Terminal
gcloud pubsub topics publish example-topic --message "Hello from Pub/Sub!"
Expected OutputExpected
messageIds: - '1234567890123456'
→
--message - The message content to send.
Check the logs of the 'helloPubSub' function to see the message it received and processed.
Terminal
gcloud functions logs read helloPubSub --region us-central1 --limit 5
Expected OutputExpected
2024-06-01 12:01:00 helloPubSub: Hello from Pub/Sub! 2024-06-01 12:00:30 helloPubSub: Function execution started 2024-06-01 12:00:31 helloPubSub: Function execution finished
→
--limit - Limits the number of log entries shown.
Key Concept

If you remember nothing else from this pattern, remember: Pub/Sub sends messages and Cloud Functions run your code automatically when those messages arrive.

Common Mistakes
Deploying the Cloud Function without specifying the correct trigger topic.
The function won't run because it doesn't know which Pub/Sub topic to listen to.
Always use the --trigger-topic flag with the exact topic name when deploying.
Sending messages to a different topic than the one the function listens to.
The function won't receive those messages and won't run.
Make sure to publish messages to the same topic specified in the function trigger.
Not decoding the base64 message data inside the function code.
The function will log unreadable base64 strings instead of the actual message content.
Decode message.data from base64 to a string before using it.
Summary
Create a Pub/Sub topic to send messages.
Deploy a Cloud Function triggered by that topic to run code automatically.
Publish messages to the topic to trigger the function.
Check function logs to verify the message was received and processed.

Practice

(1/5)
1.

What is the main role of Google Cloud Pub/Sub when integrated with Cloud Functions?

easy
A. To send messages that trigger Cloud Functions automatically
B. To store large files for Cloud Functions to process
C. To provide a user interface for Cloud Functions
D. To manage user authentication for Cloud Functions

Solution

  1. Step 1: Understand Pub/Sub's purpose

    Pub/Sub is designed to send messages between parts of an app, acting as a messenger.
  2. Step 2: Connect Pub/Sub with Cloud Functions

    Cloud Functions run code automatically when they receive messages from Pub/Sub.
  3. Final Answer:

    To send messages that trigger Cloud Functions automatically -> Option A
  4. Quick Check:

    Pub/Sub triggers Cloud Functions [OK]
Hint: Pub/Sub sends messages; Cloud Functions react to them [OK]
Common Mistakes:
  • Thinking Pub/Sub stores files
  • Confusing Pub/Sub with UI or authentication
  • Assuming Cloud Functions manage user login
2.

Which of the following is the correct way to specify a Pub/Sub trigger in a Cloud Function deployment command?

gcloud functions deploy myFunction --trigger-?
easy
A. --trigger-http my-topic
B. --trigger-bucket my-topic
C. --trigger-topic my-topic
D. --trigger-event my-topic

Solution

  1. Step 1: Identify the trigger type for Pub/Sub

    Pub/Sub triggers require specifying the topic name with --trigger-topic.
  2. Step 2: Match the correct flag

    --trigger-topic is the correct flag; others relate to storage or HTTP triggers.
  3. Final Answer:

    --trigger-topic my-topic -> Option C
  4. Quick Check:

    Pub/Sub trigger uses --trigger-topic [OK]
Hint: Pub/Sub triggers use --trigger-topic flag [OK]
Common Mistakes:
  • Using --trigger-bucket which is for Cloud Storage
  • Using --trigger-http which is for HTTP triggers
  • Confusing event triggers with topic triggers
3.

Given this Cloud Function code triggered by Pub/Sub messages, what will be logged when a message with data "Hello" is published?

def hello_pubsub(event, context):
    import base64
    message = base64.b64decode(event['data']).decode('utf-8')
    print(f"Received message: {message}")
medium
A. Received message: event['data']
B. Received message: Hello
C. Received message: b'Hello'
D. Error: 'data' key missing

Solution

  1. Step 1: Decode the Pub/Sub message data

    The message data is base64 encoded; decoding it gives the original string "Hello".
  2. Step 2: Print the decoded message

    The print statement outputs "Received message: Hello" exactly.
  3. Final Answer:

    Received message: Hello -> Option B
  4. Quick Check:

    Base64 decode then print = Received message: Hello [OK]
Hint: Decode base64 data to get original message [OK]
Common Mistakes:
  • Printing raw base64 bytes instead of decoded string
  • Using event['data'] directly without decoding
  • Assuming 'data' key is missing
4.

What is the likely cause if a Cloud Function triggered by Pub/Sub never runs after publishing messages?

medium
A. The Cloud Function is not subscribed to the correct Pub/Sub topic
B. The Cloud Function code has a syntax error
C. The Pub/Sub topic does not exist
D. The Cloud Function has too much memory allocated

Solution

  1. Step 1: Check the Cloud Function trigger configuration

    If the function is not subscribed to the right topic, it won't receive messages.
  2. Step 2: Rule out other causes

    Syntax errors cause deployment failure, not silent no-run; topic existence is needed but usually checked; memory size doesn't stop triggers.
  3. Final Answer:

    The Cloud Function is not subscribed to the correct Pub/Sub topic -> Option A
  4. Quick Check:

    Wrong topic subscription = no trigger [OK]
Hint: Check topic subscription if function never runs [OK]
Common Mistakes:
  • Assuming syntax error causes silent no-run
  • Ignoring topic existence or permissions
  • Blaming memory size for trigger issues
5.

You want to build a system where multiple Cloud Functions react independently to the same Pub/Sub messages. What is the best way to set this up?

hard
A. Publish messages multiple times to different topics
B. Create multiple Pub/Sub topics, each with one Cloud Function subscription
C. Use one Cloud Function that calls other functions internally
D. Create one Pub/Sub topic and multiple subscriptions, each triggering a different Cloud Function

Solution

  1. Step 1: Understand Pub/Sub fan-out pattern

    One topic can have multiple subscriptions; each subscription can trigger a different Cloud Function independently.
  2. Step 2: Compare options

    Multiple topics add complexity; one function calling others is indirect; publishing multiple times is inefficient.
  3. Final Answer:

    Create one Pub/Sub topic and multiple subscriptions, each triggering a different Cloud Function -> Option D
  4. Quick Check:

    One topic, many subscriptions = multiple triggers [OK]
Hint: Use multiple subscriptions on one topic for parallel triggers [OK]
Common Mistakes:
  • Creating many topics unnecessarily
  • Using one function to call others instead of triggers
  • Publishing duplicate messages to multiple topics
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