# atombrenner/aws-lambda-typescript architecture

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

- Interactive diagram: https://gitdiagram.com/atombrenner/aws-lambda-typescript
- Repository: https://github.com/atombrenner/aws-lambda-typescript

## Overview

##### Project type and purpose
- This is a serverless microservice boilerplate for authoring AWS Lambda functions in TypeScript.
- It provides a complete toolchain (build, test, package, deploy) with minimal external dependencies, using esbuild, tsx, CloudFormation, and direct AWS Lambda API calls.

##### File structure
Top-level directories and files reveal three major concerns:
a. Developer Tooling & Configuration
- .babelrc.json, .editorconfig, .prettierrc, .husky/, tsconfig.json, package.json, etc.
- Scripts defined in package.json orchestrate test, build, zip, stack, deploy, and local start.
b. Infrastructure as Code
- infrastructure/cloudformation.yaml: the CloudFormation template defining the Lambda function (and IAM roles, permissions, etc.).
- infrastructure/stack.ts: script to create or update the CloudFormation stack.
- infrastructure/zip.ts: bundles the Lambda artifacts into a ZIP.
- infrastructure/deploy.ts: invokes the Lambda API to deploy the .zip.
c. Lambda Function Source
- src/lambda.ts: the entrypoint loader for Lambda (exports handler).
- src/housekeeping/: business-logic modules and handler implementations:
- handler.ts (actual AWS handler)
- start.ts (lambda startup wiring)
- housekeeping.ts (core housekeeping logic)
- housekeeping.test.ts (unit tests)

##### README for additional insights
- Uses esbuild for bundling and source maps, tsx to run TypeScript scripts directly, Jest for testing.
- aws-cli or zip are not required; deployment is via direct AWS SDK calls in TypeScript.
- CloudFormation is preferred over CDK for simplicity; region/profile hard-coded by default.

1. Developer Environment
- IDE / Local CLI
- Local Lambda emulator (via `npm start` + tsx)
2. Version Control / Repo
- Source files (.ts), infra code, tests, config
3. CI / Build Pipeline
- Jest tests (unit)
- TypeScript compilation (tsc)
- esbuild bundling (`npm run build`)
- Source map generation (`--sourcemap --sources-content=false`)
4. Packaging Step
- Zip creation (`infrastructure/zip.ts` → dist/lambda.zip)
5. Infrastructure Provisioning
- CloudFormation (`infrastructure/cloudformation.yaml`)
- Stack management script (`infrastructure/stack.ts`)
6. Deployment Step
- Deploy script (`infrastructure/deploy.ts`) calls AWS SDK to update Lambda code
7. AWS Runtime
- AWS Lambda Function
- IAM Role & Permissions (from CF template)
- (Optional) CloudWatch Logging

b. Relationships & Data Flow
- CloudFormation provisioning flows: Repo → infra/stack.ts → CloudFormation Service → AWS resources.
- At runtime: AWS API Gateway or manual invoke → Lambda Function → Handler → Core Logic → (returns result).

c. Architectural Patterns & Principles
- Serverless / Microservice: single-purpose Lambda, minimal infrastructure.
- Infrastructure as Code: CloudFormation template + deployment scripts.
- CI/CD scripting in TypeScript via tsx/esbuild, removing external CLIs.
- Zero-copy bundling: only one bundle per function, minified but names kept for stack traces.

d. Key Technologies
- TypeScript (source language)
- esbuild (bundler)
- tsx (script runner)
- Jest + Babel (testing)
- AWS SDK (Lambda & CloudFormation APIs)
- CloudFormation (IaaC)

##### Detailed Capture of Architectural Elements
- Do not lump everything under “backend.” Separate: build, package, infrastructure, deployment, runtime.
- Both CloudFormation provisioning and Lambda code update as two distinct AWS API interactions.
- Call out housekeeping submodule as the core business-logic component within the Lambda.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Developer Environment
    subgraph "Developer Environment"
        DEVC[/"IDE / Local CLI"/]:::service
        EMULATOR["Local Lambda Emulator (npm start + tsx)"]:::service
    end

    %% Version Control
    REPO(["Version Control / Repository"]):::service

    %% Developer Tooling & Configuration
    subgraph "Developer Tooling & Configuration"
        BABEL[".babelrc.json"]:::code
        EDITORCONFIG[".editorconfig"]:::code
        PRETTIER[".prettierrc"]:::code
        HUSKY[".husky/"]:::code
        TSCONFIG["tsconfig.json"]:::code
        PACKAGEJSON["package.json"]:::code
    end

    %% CI/Build Pipeline
    subgraph "CI / Build Pipeline"
        TEST["Unit Tests (Jest)"]:::test
        TSC["TypeScript Compilation (tsc)"]:::script
        ESBUILD["Bundle (esbuild)"]:::script
        SOURCEMAP["Source Map Generation"]:::script
    end

    %% Packaging Step
    subgraph "Packaging Step"
        ZIPSCRIPT["Zip Creation (zip.ts)"]:::script
        LAMBDAZIP["dist/lambda.zip"]:::code
    end

    %% Infrastructure Provisioning
    subgraph "Infrastructure Provisioning"
        CF_TEMPLATE["CloudFormation Template"]:::infra
        STACKSCRIPT["Stack Management Script (stack.ts)"]:::script
    end

    %% Deployment Step
    subgraph "Deployment Step"
        DEPLOYSCRIPT["Deploy Script (deploy.ts)"]:::script
    end

    %% AWS Runtime
    subgraph "AWS Runtime"
        LAMBDA["AWS Lambda Function"]:::aws
        IAM["IAM Role & Permissions"]:::aws
        CLOUDWATCH["CloudWatch Logging"]:::aws
        APIGW["API Gateway / Manual Invoke"]:::aws
        HANDLER["Lambda Handler"]:::service
        CORELOGIC["Housekeeping Core Logic"]:::code
    end

    %% Relationships
    DEVC --> REPO
    REPO --> TEST
    TEST --> TSC
    TSC --> ESBUILD
    ESBUILD --> SOURCEMAP
    SOURCEMAP --> ZIPSCRIPT
    ZIPSCRIPT --> LAMBDAZIP
    ZIPSCRIPT --> STACKSCRIPT
    STACKSCRIPT --> CF_TEMPLATE
    CF_TEMPLATE -->|"Provision Resources"| LAMBDA
    DEPLOYSCRIPT -.->|"Update Code"| LAMBDA
    LAMBDAZIP --> DEPLOYSCRIPT
    REPO --> DEPLOYSCRIPT
    APIGW --> LAMBDA
    LAMBDA --> HANDLER
    HANDLER --> CORELOGIC
    LAMBDA --> IAM
    LAMBDA --> CLOUDWATCH

    %% Click Events
    click BABEL "https://github.com/atombrenner/aws-lambda-typescript/blob/master/.babelrc.json"
    click EDITORCONFIG "https://github.com/atombrenner/aws-lambda-typescript/blob/master/.editorconfig"
    click PRETTIER "https://github.com/atombrenner/aws-lambda-typescript/blob/master/.prettierrc"
    click HUSKY "https://github.com/atombrenner/aws-lambda-typescript/tree/master/.husky/"
    click TSCONFIG "https://github.com/atombrenner/aws-lambda-typescript/blob/master/tsconfig.json"
    click PACKAGEJSON "https://github.com/atombrenner/aws-lambda-typescript/blob/master/package.json"
    click CF_TEMPLATE "https://github.com/atombrenner/aws-lambda-typescript/blob/master/infrastructure/cloudformation.yaml"
    click STACKSCRIPT "https://github.com/atombrenner/aws-lambda-typescript/blob/master/infrastructure/stack.ts"
    click ZIPSCRIPT "https://github.com/atombrenner/aws-lambda-typescript/blob/master/infrastructure/zip.ts"
    click DEPLOYSCRIPT "https://github.com/atombrenner/aws-lambda-typescript/blob/master/infrastructure/deploy.ts"
    click HANDLER "https://github.com/atombrenner/aws-lambda-typescript/blob/master/src/housekeeping/handler.ts"
    click CORELOGIC "https://github.com/atombrenner/aws-lambda-typescript/blob/master/src/housekeeping/housekeeping.ts"

    %% Styles
    classDef code fill:#f9f,stroke:#333,stroke-width:1px
    classDef script fill:#ff9,stroke:#333,stroke-width:1px
    classDef infra fill:#9ff,stroke:#333,stroke-width:1px
    classDef service fill:#f99,stroke:#333,stroke-width:1px
    classDef aws fill:#cfc,stroke:#333,stroke-width:1px
    classDef test fill:#fcf,stroke:#333,stroke-width:1px
```
