Building a Pub/Sub Message Router with Cloud Run

 🛠️ What You’ll Build

You’ll create a message router that listens to Google Cloud Pub/Sub messages, inspects them, and routes them to appropriate endpoints based on message content.


📦 Requirements

Google Cloud account


gcloud CLI installed and authenticated


A GCP project with:


Cloud Run


Pub/Sub


Cloud Build


Artifact Registry (optional, for container storage)


🧭 Architecture Overview

A Pub/Sub topic receives messages.


A Cloud Run service is subscribed to this topic (via a Push subscription).


The Cloud Run service:


Parses the message.


Applies routing logic.


Forwards the message to appropriate service(s) (e.g., via HTTP).


🚀 Step-by-Step Guide

Step 1: Create a Pub/Sub Topic

bash

Copy

Edit

gcloud pubsub topics create message-router-topic

Step 2: Write Your Cloud Run App

You can use any language. Here’s a simple Python (Flask) example:


python

Copy

Edit

# main.py

from flask import Flask, request, jsonify

import requests

import json

import os


app = Flask(__name__)


@app.route("/", methods=["POST"])

def route_message():

    envelope = request.get_json()


    if not envelope or 'message' not in envelope:

        return "Bad Request: No message", 400


    pubsub_message = envelope["message"]

    data = json.loads(base64.b64decode(pubsub_message["data"]).decode("utf-8"))


    # Example routing logic

    message_type = data.get("type")

    

    if message_type == "order":

        endpoint = os.environ.get("ORDER_SERVICE_URL")

    elif message_type == "user":

        endpoint = os.environ.get("USER_SERVICE_URL")

    else:

        endpoint = os.environ.get("DEFAULT_SERVICE_URL")


    # Forward message

    try:

        response = requests.post(endpoint, json=data)

        response.raise_for_status()

        return "Message routed successfully", 200

    except requests.exceptions.RequestException as e:

        return f"Error forwarding message: {str(e)}", 500

requirements.txt:


ini

Copy

Edit

Flask==2.3.2

requests==2.31.0

Step 3: Dockerize the App

Dockerfile:


Dockerfile

Copy

Edit

FROM python:3.10-slim


WORKDIR /app

COPY requirements.txt .

RUN pip install --no-cache-dir -r requirements.txt


COPY . .


CMD ["python", "main.py"]

Step 4: Deploy to Cloud Run

Build & deploy the container:


bash

Copy

Edit

gcloud builds submit --tag gcr.io/PROJECT_ID/message-router

Deploy to Cloud Run:


bash

Copy

Edit

gcloud run deploy message-router \

  --image gcr.io/PROJECT_ID/message-router \

  --platform managed \

  --region YOUR_REGION \

  --allow-unauthenticated \

  --set-env-vars ORDER_SERVICE_URL=https://order.example.com,USER_SERVICE_URL=https://user.example.com,DEFAULT_SERVICE_URL=https://default.example.com

Step 5: Create Pub/Sub Push Subscription

bash

Copy

Edit

gcloud pubsub subscriptions create message-router-sub \

  --topic=message-router-topic \

  --push-endpoint=https://YOUR_CLOUD_RUN_URL/ \

  --push-auth-service-account=YOUR_SERVICE_ACCOUNT@YOUR_PROJECT.iam.gserviceaccount.com

Make sure the service account has the roles/run.invoker role on the Cloud Run service.


✅ Test It!

Publish a message to the topic:


bash

Copy

Edit

gcloud pubsub topics publish message-router-topic \

  --message='{"type":"order","order_id":123}'

Your Cloud Run service should receive and route this message to the correct endpoint.


🧠 Tips

Validate and log every incoming message.


Consider retry/dead-letter topics for failed routing.


Secure your endpoints and Pub/Sub subscription.

Learn Google Cloud Data Engineering Course

Read More

Creating a Multi-Tenant Event Bus with Cloud Pub/Sub

Using Pub/Sub as an Audit Trail for Regulatory Compliance

Integrating Pub/Sub with SAP Systems for Real-Time Messaging

Real-Time Notification Systems with Pub/Sub and Firebase

Visit Our Quality Thought Training in Hyderabad

Get Directions 


Comments

Popular posts from this blog

Understanding Snowflake Editions: Standard, Enterprise, Business Critical

Why Data Science Course?

How To Do Medical Coding Course?