Part-46: Trigger a Cloud Function based on Pubsub Subscriptions in GCP

☁️ Cloud Functions – Cloud Pub/Sub Trigger

Google Cloud Functions integrates seamlessly with Cloud Pub/Sub to execute code whenever a message is published to a topic. This allows for event-driven architectures where functions react to data s…


This content originally appeared on DEV Community and was authored by Latchu@DevOps

☁️ Cloud Functions – Cloud Pub/Sub Trigger

Google Cloud Functions integrates seamlessly with Cloud Pub/Sub to execute code whenever a message is published to a topic. This allows for event-driven architectures where functions react to data streams in real time. 🚀

✅ Step-01: Introduction

We’ll create:

  1. A Cloud Pub/Sub Topic
  2. A Cloud Function triggered by Pub/Sub Events

✅ Step-02: Create Cloud Pub/Sub Topic

Navigate to Cloud Pub/Sub → CREATE TOPIC

p1

Enter the following details:

  • Topic ID: mytopic1
  • Leave the rest of the settings as default

Click CREATE

Review your setup:

  • Check that the Topic was created
  • Verify the Subscription is attached

p2

✅ Step-03: Create Cloud Function with Pub/Sub Trigger

Configuration Tab

  • Service name: cf-demo2-events-pubsub
  • Region: us-central1
  • Trigger: Cloud Pub/Sub
  • Topic: mytopic1
  • More Options → Review defaults
  • Click NEXT

p3

Code Tab

  • Runtime: Node.js 20
  • Use the auto-populated sample code (below)
  • Click Save and DEPLOY

p4

const functions = require('@google-cloud/functions-framework');

// Register a CloudEvent callback with the Functions Framework that will
// be executed when the Pub/Sub trigger topic receives a message.
functions.cloudEvent('helloPubSub', cloudEvent => {
  // The Pub/Sub message is passed as the CloudEvent's data payload.
  const base64name = cloudEvent.data.message.data;

  const name = base64name
    ? Buffer.from(base64name, 'base64').toString()
    : 'World';

  console.log(`Hello, ${name}!`);
});

✅ Step-04: Review Cloud Function Logs

  • Navigate to: Cloud Function → cf-demo2-events-pubsub → Logs
  • You’ll see execution logs once messages are published.

✅ Step-05: Publish Messages to the Topic

Go to Cloud Pub/Sub → mytopic1 → MESSAGES → PUBLISH MESSAGE

Enter:

  • Number of Messages: 10
  • Message Body: My Pub/Message

Click PUBLISH

p5

✅ Step-06: Review Logs Again

  • Go to Cloud Function Logs
  • You should see:
Hello, My Pub/Message!

p6

logged multiple times (once for each published message)


This content originally appeared on DEV Community and was authored by Latchu@DevOps


Print Share Comment Cite Upload Translate Updates
APA

Latchu@DevOps | Sciencx (2025-09-13T05:22:53+00:00) Part-46: Trigger a Cloud Function based on Pubsub Subscriptions in GCP. Retrieved from https://www.scien.cx/2025/09/13/part-46-trigger-a-cloud-function-based-on-pubsub-subscriptions-in-gcp/

MLA
" » Part-46: Trigger a Cloud Function based on Pubsub Subscriptions in GCP." Latchu@DevOps | Sciencx - Saturday September 13, 2025, https://www.scien.cx/2025/09/13/part-46-trigger-a-cloud-function-based-on-pubsub-subscriptions-in-gcp/
HARVARD
Latchu@DevOps | Sciencx Saturday September 13, 2025 » Part-46: Trigger a Cloud Function based on Pubsub Subscriptions in GCP., viewed ,<https://www.scien.cx/2025/09/13/part-46-trigger-a-cloud-function-based-on-pubsub-subscriptions-in-gcp/>
VANCOUVER
Latchu@DevOps | Sciencx - » Part-46: Trigger a Cloud Function based on Pubsub Subscriptions in GCP. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/09/13/part-46-trigger-a-cloud-function-based-on-pubsub-subscriptions-in-gcp/
CHICAGO
" » Part-46: Trigger a Cloud Function based on Pubsub Subscriptions in GCP." Latchu@DevOps | Sciencx - Accessed . https://www.scien.cx/2025/09/13/part-46-trigger-a-cloud-function-based-on-pubsub-subscriptions-in-gcp/
IEEE
" » Part-46: Trigger a Cloud Function based on Pubsub Subscriptions in GCP." Latchu@DevOps | Sciencx [Online]. Available: https://www.scien.cx/2025/09/13/part-46-trigger-a-cloud-function-based-on-pubsub-subscriptions-in-gcp/. [Accessed: ]
rf:citation
» Part-46: Trigger a Cloud Function based on Pubsub Subscriptions in GCP | Latchu@DevOps | Sciencx | https://www.scien.cx/2025/09/13/part-46-trigger-a-cloud-function-based-on-pubsub-subscriptions-in-gcp/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.