0
0
GCPcloud~5 mins

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

Choose your learning style9 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.