# nikunjkabariya/aws-microservices architecture

> Architecture diagram of the GitHub repository nikunjkabariya/aws-microservices, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-06-22). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/nikunjkabariya/aws-microservices
- Repository: https://github.com/nikunjkabariya/aws-microservices

## Overview

##### Project type and purpose
- This is a Serverless, event-driven microservices project for an e-commerce domain.
- It uses AWS Lambda for compute, API Gateway for REST endpoints, DynamoDB for persistence, Amazon EventBridge for event routing, SQS for queuing, and AWS CDK (TypeScript) for infrastructure-as-code.

##### File structure
- bin/aws-microservices.ts – CDK app entry point
- lib/ – CDK construct code:
– apigateway.ts
– aws-microservices-stack.ts (assembling the full stack)
– database.ts (DynamoDB tables)
– eventbus.ts (EventBridge bus)
– queue.ts (SQS queues)
– microservice.ts (Lambda + integration helpers)
- src/ – three independent microservices, each with its own package.json, Lambda handler (index.js), DynamoDB client (ddbClient.js), EventBridge client (eventBridgeClient.js) and configuration JSON:
– product
– basket
– ordering
- test/aws-microservices.test.ts – unit/integration test for the CDK stack

##### README for additional insights
- The reference architecture is a real-world e-commerce back end exposing three REST microservices (product, basket, ordering).
- Key patterns: REST + CRUD with API Gateway & Lambda, persistence in DynamoDB, decoupling via EventBridge, cross-service messaging via SQS, all wired by CDK.

a. Main components:
- Client (e.g., web/mobile front end or Postman)
- Amazon API Gateway – three resource paths (/product, /basket, /order)
- AWS Lambda functions – product-service, basket-service, ordering-service
- DynamoDB tables – ProductTable, BasketTable, OrderTable
- Amazon EventBridge – custom event bus
- AWS SQS – one or more queues for cross-service messages (e.g., order-process-queue)
- AWS CDK – the IaC layer that defines and deploys all resources
b. Relationships and data flow:
1. Client → API Gateway → specific Lambda (e.g., basket-service)
2. Lambda ↔ DynamoDB for CRUD operations
3. On critical actions (e.g., “checkout basket”), Lambda publishes an event to EventBridge
4. EventBridge rule routes events to SQS queue(s) and/or directly triggers subscribing Lambdas (ordering-service)
5. ordering-service consumes from SQS (or EventBridge) → updates OrderTable → may emit further events
6. CDK code ties all constructs together, specifying permissions, environment variables, event rules, and API mappings
c. Architectural patterns:
- Microservices – each business domain owns its own database and codebase
- Serverless – zero server management via Lambda and managed services
- Event-driven / pub-sub – loose coupling between basket and ordering
- Queue-based load leveling – SQS buffers spikes in event traffic
- Infrastructure as code – reproducible, version-controlled stack via CDK
d. Key technologies & libraries:
- AWS CDK (TypeScript) constructs in lib/
- AWS Lambda (Node.js) handlers in src/
- DynamoDB SDK (ddbClient.js)
- EventBridge SDK (eventBridgeClient.js)
- Jest for CDK/unit tests

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Client and API
    Client["Client (Web/Mobile/Postman)"]:::external
    APIGW["API Gateway"]:::api

    Client -->|REST Request| APIGW

    %% Service: Product
    subgraph "Product Service"
        ProdLambda["ProductService Lambda"]:::compute
        ProdDB["ProductTable"]:::database
    end

    %% Service: Basket
    subgraph "Basket Service"
        BasketLambda["BasketService Lambda"]:::compute
        BasketDB["BasketTable"]:::database
    end

    %% Service: Ordering
    subgraph "Ordering Service"
        OrderLambda["OrderingService Lambda"]:::compute
        OrderDB["OrderTable"]:::database
    end

    %% Messaging
    EventBus["EventBridge Bus"]:::messaging
    SQSQueue["OrderProcessQueue (SQS)"]:::messaging

    %% IaC
    subgraph "Infrastructure as Code (CDK)" 
        direction TB
        CDKApp["CDK App Entry Point"]:::iac
        CDKStack["CDK Stack (aws-microservices-stack)"]:::iac
        APIGWConstruct["API Gateway Construct"]:::iac
        DBConstruct["DynamoDB Construct"]:::iac
        EventConstruct["EventBridge Construct"]:::iac
        QueueConstruct["SQS Queue Construct"]:::iac
        MSHelpers["Microservice Helpers Construct"]:::iac
    end

    %% Flows
    APIGW -->|/product| ProdLambda
    APIGW -->|/basket| BasketLambda
    APIGW -->|/order| OrderLambda

    ProdLambda -->|"CRUD"| ProdDB
    BasketLambda -->|"CRUD"| BasketDB
    OrderLambda -->|"CRUD"| OrderDB

    BasketLambda -->|Publishes BasketCheckedOut| EventBus
    EventBus -->|Direct Invoke| OrderLambda
    EventBus -->|Sends event| SQSQueue
    SQSQueue -->|Message| OrderLambda

    %% CDK relationships
    CDKApp --> CDKStack
    CDKStack --> APIGWConstruct
    CDKStack --> DBConstruct
    CDKStack --> EventConstruct
    CDKStack --> QueueConstruct
    CDKStack --> MSHelpers

    APIGWConstruct --> APIGW
    DBConstruct --> ProdDB
    DBConstruct --> BasketDB
    DBConstruct --> OrderDB
    EventConstruct --> EventBus
    QueueConstruct --> SQSQueue
    MSHelpers --> ProdLambda
    MSHelpers --> BasketLambda
    MSHelpers --> OrderLambda

    %% Click Events
    click CDKApp "https://github.com/nikunjkabariya/aws-microservices/blob/main/bin/aws-microservices.ts"
    click CDKStack "https://github.com/nikunjkabariya/aws-microservices/blob/main/lib/aws-microservices-stack.ts"
    click APIGWConstruct "https://github.com/nikunjkabariya/aws-microservices/blob/main/lib/apigateway.ts"
    click DBConstruct "https://github.com/nikunjkabariya/aws-microservices/blob/main/lib/database.ts"
    click EventConstruct "https://github.com/nikunjkabariya/aws-microservices/blob/main/lib/eventbus.ts"
    click QueueConstruct "https://github.com/nikunjkabariya/aws-microservices/blob/main/lib/queue.ts"
    click MSHelpers "https://github.com/nikunjkabariya/aws-microservices/blob/main/lib/microservice.ts"
    click ProdLambda "https://github.com/nikunjkabariya/aws-microservices/blob/main/src/product/index.js"
    click ProdDB "https://github.com/nikunjkabariya/aws-microservices/blob/main/src/product/ddbClient.js"
    click EventBus "https://github.com/nikunjkabariya/aws-microservices/blob/main/src/product/eventBridgeClient.js"
    click BasketLambda "https://github.com/nikunjkabariya/aws-microservices/blob/main/src/basket/index.js"
    click BasketDB "https://github.com/nikunjkabariya/aws-microservices/blob/main/src/basket/ddbClient.js"
    click SQSQueue "https://github.com/nikunjkabariya/aws-microservices/blob/main/src/basket/checkoutbasketevents.json"
    click OrderLambda "https://github.com/nikunjkabariya/aws-microservices/blob/main/src/ordering/index.js"
    click OrderDB "https://github.com/nikunjkabariya/aws-microservices/blob/main/src/ordering/package.json"
    click ProdLambda "https://github.com/nikunjkabariya/aws-microservices/blob/main/src/product/package.json"
    click BasketLambda "https://github.com/nikunjkabariya/aws-microservices/blob/main/src/basket/package.json"
    click OrderLambda "https://github.com/nikunjkabariya/aws-microservices/blob/main/src/ordering/package.json"
    click CDKApp "https://github.com/nikunjkabariya/aws-microservices/blob/main/test/aws-microservices.test.ts"

    %% Styles
    classDef compute fill:#cce5ff,stroke:#00529B,stroke-width:1px
    classDef database fill:#ffe5b4,stroke:#996515,stroke-width:1px
    classDef messaging fill:#d4edda,stroke:#155724,stroke-width:1px
    classDef api fill:#e2e3e5,stroke:#383d41,stroke-width:1px
    classDef external fill:#f8f9fa,stroke:#6c757d,stroke-width:1px
    classDef iac fill:#f5f5f5,stroke:#343a40,stroke-dasharray: 5 5,stroke-width:1px
```
