AAWSLearn
Back to Blog
Serverless9 min read

Building Event-Driven Architectures with Serverless

Design scalable event-driven systems using Lambda, EventBridge, SQS, and DynamoDB streams.

#Lambda#EventBridge#Serverless

Traditional synchronous APIs block execution until the downstream processes complete. This tightly couples your microservices and creates a single point of failure.

Event-driven architecture (EDA) breaks this coupling by using events to trigger asynchronous actions. In AWS, combining event-driven design with Serverless resources like Lambda, SQS, and EventBridge yields highly scalable, self-healing platforms.


The Event Flow

Consider an e-commerce checkout flow:

  1. The client sends a POST /order request to Amazon API Gateway.
  2. An Order API Lambda handler saves the order to Amazon DynamoDB and publishes an OrderCreated event to Amazon EventBridge.
  3. Multiple microservices (e.g., Payment Service, Inventory Service, Email Service) listen for OrderCreated and process their work concurrently.

Writing a Lambda Event Publisher

Here is a sample TypeScript AWS Lambda handler using the AWS SDK v3 to publish a custom event payload to Amazon EventBridge:

import { EventBridgeClient, PutEventsCommand } from "@aws-sdk/client-eventbridge";

const eventBridge = new EventBridgeClient({ region: "us-east-1" });

interface OrderPayload {
  orderId: string;
  amount: number;
  customerId: string;
}

export const handler = async (event: any): Promise<any> => {
  const order: OrderPayload = JSON.parse(event.body);

  const command = new PutEventsCommand({
    Entries: [
      {
        Source: "awslearn.order-service",
        DetailType: "OrderCreated",
        Detail: JSON.stringify(order),
        EventBusName: "default",
      },
    ],
  });

  try {
    const response = await eventBridge.send(command);
    return {
      statusCode: 201,
      body: JSON.stringify({
        message: "Order placed and event published!",
        eventId: response.Entries?.[0]?.EventId,
      }),
    };
  } catch (error) {
    console.error("Failed to publish event:", error);
    return {
      statusCode: 500,
      body: JSON.stringify({ message: "Failed to place order" }),
    };
  }
};

By processing payments, invoices, and emails asynchronously, the front-end API returns immediately to the client, improving user experience and resilience.

Related Articles

Architecture

AWS Well-Architected Framework: A Practical Guide

Learn the six pillars of the Well-Architected Framework and how to apply them to real-world cloud workloads.

DevOps

Terraform Modules: Best Practices for Production

Structure reusable Terraform modules with proper state management, versioning, and CI/CD integration.

Kubernetes

EKS Production Checklist: From Cluster to Deployment

A comprehensive checklist for running production-grade Kubernetes on Amazon EKS with security and observability.

Search AWSLearn

Search for AWS VPC, Karpenter, Docker builds, Linux runlevels, or Terraform states.