1. Producer Lambda

Publishes event to EventBridge.

importjson
importboto3
importuuid

eventbridge=boto3.client("events")

deflambda_handler(event,context):

order= {
"orderId":str(uuid.uuid4()),
"userId":"user-101",
"amount":250,
"status":"CREATED"
    }

eventbridge.put_events(
Entries=[
            {
"Source":"app.orders",
"DetailType":"OrderCreated",
"Detail":json.dumps(order),
"EventBusName":"default"
            }
        ]
    )

return {
"statusCode":200,
"body":json.dumps(order)
    }

2. SQS Worker Lambda → DynamoDB

Consumes from SQS and writes to DynamoDB.

importjson
importboto3

dynamodb=boto3.resource("dynamodb")

table=dynamodb.Table("OrdersTable")

deflambda_handler(event,context):

forrecordinevent["Records"]:

body=json.loads(record["body"])

# SNS wraps message inside body
message=json.loads(body["Message"])

table.put_item(
Item={
"orderId":message["orderId"],
"userId":message["userId"],
"amount":message["amount"],
"status":message["status"]
            }
        )

print("Inserted into DynamoDB")

return {
"statusCode":200
    }

DynamoDB Table Structure

Table Name: OrdersTable

Partition Key:
orderId (String)

3. Analytics Lambda → RDS

Reads event directly from EventBridge and inserts into PostgreSQL/MySQL RDS.

importjson
importos
importpymysql

connection=pymysql.connect(
host=os.environ["DB_HOST"],
user=os.environ["DB_USER"],
password=os.environ["DB_PASSWORD"],
database=os.environ["DB_NAME"]
)

deflambda_handler(event,context):

detail=event["detail"]

cursor=connection.cursor()

query="""
    INSERT INTO order_analytics
    (order_id, user_id, amount, status)
    VALUES (%s, %s, %s, %s)
    """

cursor.execute(
query,
        (
detail["orderId"],
detail["userId"],
detail["amount"],
detail["status"]
        )
    )

connection.commit()

print("Inserted into RDS")

return {
"statusCode":200
    }

RDS Table

CREATETABLE order_analytics (
    idINT AUTO_INCREMENTPRIMARYKEY,
    order_idVARCHAR(255),
    user_idVARCHAR(255),
    amountINT,
    statusVARCHAR(50)
);

4. DynamoDB Read Lambda

Fetch order by ID.