# leividduan/sls-process-image architecture

> Architecture diagram of the GitHub repository leividduan/sls-process-image, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-08). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/leividduan/sls-process-image
- Repository: https://github.com/leividduan/sls-process-image

## Overview

##### Project Type and Purpose
- This is a Serverless application that exposes HTTP API endpoints to store live stream data and process thumbnail images.
- The system uses AWS to manage serverless infrastructure, in which API Gateway handles HTTP traffic, and Lambda functions (organized by purpose) manage backend logic for processing streams, S3 objects, and queued tasks.

##### File Structure
- The top-level files (package.json, serverless.yml) signal that the project is managed by the Serverless Framework. This file (serverless.yml) is crucial for defining your AWS resources.
- The “src” directory is divided into several parts:
- “clients”: Contains modules for interacting with AWS services such as DynamoDB (dynamoClient.ts), S3 (s3Client.ts), and SQS (sqsClient.ts). This indicates an abstraction layer to facilitate integration with AWS.
- “functions”: Organized into subdirectories (“http”, “queues”, “triggers”) which suggests a clear separation of concerns:
▪ “http”: For API gateway endpoints (e.g., createLive.ts).
▪ “queues”: For functions handling asynchronous processing like image processing (processImage.ts).
▪ “triggers”: For functions that automatically respond to AWS events (newS3ObjectTrigger.ts that likely reacts to new S3 objects).
- “config” and “utils”: Provide configuration settings and helper functions.

a. Main components:
- API Gateway: The entry point for HTTP requests that triggers the HTTP lambda function.
- Lambda Functions: Organized by their type:
- HTTP Lambda (createLive): Receives API input for live stream creation.
- Trigger Lambda (newS3ObjectTrigger): Activates when a new file (or live stream data) is uploaded to S3.
- Queue Lambda (processImage): Processes tasks asynchronously, such as generating thumbnail images from queued messages.
- AWS Services:
- S3: Used to store live streams and image files.
- DynamoDB: Likely used for keeping metadata or state information.
- SQS: Used to queue and manage image processing tasks.

b. Relationships and Interactions:
- HTTP Flow: API Gateway → HTTP Lambda (createLive). This function could handle storing stream data, and possibly triggering an upload to S3.
- S3 Trigger: When a new object is uploaded (maybe after an HTTP call), S3 sends an event to the Trigger Lambda (newS3ObjectTrigger), which might then enqueue a message.
- Queue Processing: The enqueued message in SQS is picked up by the Queue Lambda (processImage) to process or create image thumbnails.
- Database Interaction: Both the HTTP Lambda and the Queue Lambda interact with DynamoDB for storing and updating metadata.

c. Architectural Patterns and Design Principles:
- Separation of Concerns: The clear division between HTTP, trigger, and queue processing lambdas.
- Abstraction over AWS Services: Client modules encapsulate interactions with DynamoDB, S3, and SQS.
- Loose Coupling: Asynchronous processing through SQS ensures that components can operate independently.

d. Technologies, Frameworks, and Libraries:
- AWS Services: API Gateway, Lambda, S3, SQS, DynamoDB.
- Serverless Framework: Used for infrastructure deployment.
- TypeScript: The language used for the implementation.
- Lambda Layers: Although not directly visible in the file structure, the README mentions their use, so include them if your project abstracts shared code.

##### Elements
- Each component should have a clear label (e.g., “API Gateway”, “CreateLive Lambda”, “New S3 Object Trigger Lambda”, “ProcessImage Lambda”, “S3”, “DynamoDB”, “SQS”).
- API Gateway → HTTP Lambda: Receiving and processing HTTP requests.
- S3 → Trigger Lambda: Indicating S3’s event trigger.
- Trigger Lambda → SQS: Queuing image processing tasks.
- SQS → Queue Lambda: Invoking asynchronous processing.
- Lambda functions ↔ DynamoDB: Recording or querying metadata.

## Diagram (Mermaid)

```mermaid
graph TD
    %% API and HTTP Layer
    subgraph "API & HTTP Layer"
        APIGW["API Gateway"]:::apigateway
        HL["CreateLive Lambda"]:::lambda
    end

    %% Data Ingestion
    subgraph "Data Ingestion"
        S3["S3"]:::awsresource
    end

    %% Event Trigger
    subgraph "Event Trigger"
        TSL["New S3 Object Trigger Lambda"]:::lambda
    end

    %% Task Queue
    subgraph "Task Queue"
        SQS["SQS"]:::awsresource
    end

    %% Queue Processing
    subgraph "Queue Processing"
        PIL["ProcessImage Lambda"]:::lambda
    end

    %% Data Storage
    subgraph "Data Storage"
        DDB["DynamoDB"]:::awsresource
    end

    %% AWS Clients
    subgraph "AWS Clients"
        DDB_C["DynamoDB Client"]:::client
        S3_C["S3 Client"]:::client
        SQS_C["SQS Client"]:::client
    end

    %% Connections between main components
    APIGW -->|"HTTPRequest"| HL
    HL -->|"UploadStream"| S3
    HL -->|"StoreMetadata"| DDB
    S3 -->|"S3Event"| TSL
    TSL -->|"QueueTask"| SQS
    SQS -->|"QueueMessage"| PIL
    PIL -->|"UpdateData"| DDB

    %% Connections from Lambdas to AWS Clients (dashed lines)
    HL -.->|"Uses"| S3_C
    HL -.->|"Uses"| DDB_C
    TSL -.->|"Uses"| S3_C
    TSL -.->|"Uses"| SQS_C
    PIL -.->|"Uses"| DDB_C
    PIL -.->|"Uses"| SQS_C

    %% Click Events
    click APIGW "https://github.com/leividduan/sls-process-image/blob/main/serverless.yml"
    click HL "https://github.com/leividduan/sls-process-image/blob/main/src/functions/http/createLive.ts"
    click TSL "https://github.com/leividduan/sls-process-image/blob/main/src/functions/triggers/newS3ObjectTrigger.ts"
    click PIL "https://github.com/leividduan/sls-process-image/blob/main/src/functions/queues/processImage.ts"
    click DDB_C "https://github.com/leividduan/sls-process-image/blob/main/src/clients/dynamoClient.ts"
    click S3_C "https://github.com/leividduan/sls-process-image/blob/main/src/clients/s3Client.ts"
    click SQS_C "https://github.com/leividduan/sls-process-image/blob/main/src/clients/sqsClient.ts"

    %% Styles
    classDef apigateway fill:#cfe2f3,stroke:#0b5394,stroke-width:2px;
    classDef lambda fill:#d9ead3,stroke:#38761d,stroke-width:2px;
    classDef awsresource fill:#fff2cc,stroke:#bf9000,stroke-width:2px;
    classDef client fill:#ead1dc,stroke:#b8549e,stroke-width:2px;
```
