Building an Event-Driven Sample Ecommerce Backend with AWS SAM

Managing an ecommerce platform’s backend can be complex, especially when it comes to handling events like order placements, inventory updates, and customer management. A serverless, event-driven architecture using AWS SAM (Serverless Application Model) can provide a scalable, cost-effective solution. In this article, we explore how the ecommerce_event_backend architecture leverages AWS to manage ecommerce events efficiently.

Overview

The ecommerce_event_backend is a serverless application framework built with AWS SAM to handle core ecommerce operations, including order processing and inventory management. By embracing a serverless and event-driven design, this architecture ensures responsiveness, scalability, and reduced operational costs while supporting various ecommerce functionalities.

Key Components

This architecture includes several AWS services working together to process events efficiently:

  1. AWS Lambda Functions

Lambda functions are the building blocks of this serverless backend, each designed to handle specific tasks.

  1. HelloWorldFunction: A placeholder Lambda function that returns a greeting. This function is mainly for testing the serverless setup.
    1. OrderServiceFunction: Manages incoming order requests, processes placements, and triggers events related to order management.
    1. InventoryServiceFunction: Responds to inventory-related events, updating stock levels and providing inventory data.
  2. API Gateway

AWS API Gateway serves as the interface between client requests and the backend, allowing clients to communicate with the Lambda functions via HTTP requests.

  • EventBridge (Event Bus)
    • CustomEventBus: Amazon EventBridge’s custom event bus manages events generated by the OrderServiceFunction, such as order placement and inventory update events. It provides reliable event-driven communication between components.
  • DynamoDB Tables

DynamoDB tables are used to store the application’s critical data, enabling fast, scalable, and flexible database operations.

  1. OrdersTable: Stores details about each order.
  2. CustomersTable: Contains customer-related information.
  3. ProductsTable: Manages product listings.
  4. InventoryTable: Tracks product stock levels.
  5. Event Bridge Rules: 
    1. OrderEventRule: Configured to listen for specific events like order placements, this rule triggers various actions based on the event type (e.g., updating inventory when an order is placed).

Workflow and Event Payloads

The backend system is event-driven, with key events processed and acted upon automatically. Here’s a look at some common event types and their JSON payloads:

  1. OrderPlaced Event
{
    "detailType": "OrderPlaced",
    "orderId": "001",
    "orderStatus": "OrderPlaced",
    "customerId": "001",
    "items": [
        { "productId": "001", "quantity": 1 },
        { "productId": "002", "quantity": 1 }
    ],
    "totalAmount": 279.98
}
  • OrderShipped Event
{
    "detailType": "OrderShipped",
    "orderId": "001",
    "orderStatus": "OrderShipped"
}
  • OrderDelivered Event
{
    "detailType": "OrderDelivered",
    "orderId": "001",
    "orderStatus": "OrderDelivered"
}
  • OrderCanceled Event
{
    "detailType": "OrderCanceled",
    "orderId": "001",
    "orderStatus": "OrderCanceled",
    "customerId": "001",
    "items": [
        { "productId": "001", "quantity": 1 },
        { "productId": "002", "quantity": 1 }
    ]
}

Architecture Diagram

Below is a visual representation of the ecommerce_event_backend architecture, illustrating the interaction between components and the flow of events within the system:

Process Flow

  1. Order Placement and Processing
    1. Client Requests: The process begins with a client (e.g., web or mobile app) making a POST request to the /order endpoint to place an order.
    1. API Gateway: This request is received by Amazon API Gateway, which routes it to the OrderServiceFunction.
    1. OrderServiceFunction Execution:
      1. The function processes the order request, including details like customer information, items ordered, and quantity.
      1. It stores the order data in the OrdersTable (DynamoDB), creating a unique OrderId for future reference.
      1. Once the order is stored, OrderServiceFunction sends an event (OrderPlaced) to the Custom Event Bus (EventBridge) to signal that a new order has been placed.
  2. EventBridge and Event Propagation
    1. Custom Event Bus: The event bus listens for events from OrderServiceFunction, specifically events with source: “ecommerce.order” and detail-type: “OrderPlaced”.
    1. Event Pattern Matching: The OrderEventRule is configured on EventBridge to match events with the types OrderPlaced, OrderShipped, OrderDelivered, and OrderCanceled. This rule filters and routes each event to the appropriate service.
    1. Target Invocation: When an OrderPlaced event is triggered, EventBridge invokes the InventoryServiceFunction to adjust inventory levels based on the items in the placed order.
  3. Inventory Update and Management
    1. InventoryServiceFunction Execution:
      1. This function receives the OrderPlaced event payload from EventBridge, which contains the order details, including product IDs and quantities.
      1. It checks the InventoryTable in DynamoDB to verify stock levels and update them accordingly by reducing the inventory for each product in the order.
      1. The function then logs that inventory levels were updated and returns a success message.
  4. Further Event Handling:
    1. If the order status changes (e.g., OrderShipped, OrderDelivered, or OrderCanceled), EventBridge invokes InventoryServiceFunction again to:
  5. OrderShipped / OrderDelivered: Update the order status in OrdersTable.
  6. OrderCanceled: Revert the inventory quantities for canceled items and update the order status to “Canceled.”

Getting Started

The full project repository is available on GitHub at AWS EventBus OrderFlow. Follow the instructions in readme.md for deployment and setup. Load the values into Orders, Customers and Inventory Tables in dynamodb.

  1. Products
    1. {
       “ProductId”: “001”,
       “Name”: “Wireless Headphones”,
       “Price”: 79.99
      }
    1. {
       “ProductId”: “002”,
       “Name”: “Smartwatch”,
       “Price”: 199.99
      }
  2. Inventory
    1. {
       “ProductId”: “001”,
       “Quantity”: 150
      }
    1. {
       “ProductId”: “002”,
       “Quantity”: 200
      }
  3. Customers
    1. {
       “CustomerId”: “001”,
       “Email”: “alice.johnson@example.com”,
       “Name”: “Alice Johnson”
      }
    1. {
       “CustomerId”: “002”,
       “Email”: “michael.brown@example.com”,
       “Name”: “Michael Brown”
      }

Conclusion

The ecommerce_event_backend is a robust example of how serverless and event-driven architecture can optimize ecommerce backend systems. With AWS services like Lambda, EventBridge, API Gateway, and DynamoDB working together, the system is able to handle dynamic business events and provide a responsive experience to end users. This approach is cost-effective and scales seamlessly with business demands, making it an ideal choice for modern ecommerce applications.

Leave a Reply

Your email address will not be published. Required fields are marked *