# @cloudi/aws DI-enabled cloud (AWS Lambda) functions. Write cloud functions using Dependency Injection. ## Overview `@cloudi/aws` is a framework for building AWS Lambda functions using **Dependency Injection (DI)**. It provides: - ๐ŸŽฏ **Type-safe abstractions** for AWS event types (API Gateway, SNS, SQS, S3, DynamoDB, EventBridge) - ๐Ÿ’‰ **Dependency Injection** via `@webiny/di` container - ๐Ÿ”— **Middleware chain** - handlers compose via `next()`, enabling pre/post processing - ๐Ÿงช **Testable** - mock dependencies easily for unit testing - ๐Ÿ—๏ธ **SOLID principles** - write clean, maintainable Lambda functions ## Installation ```bash npm install @cloudi/aws @webiny/di # or yarn add @cloudi/aws @webiny/di ``` ## Quick Start ### 1. Define Your Handler Each handler receives `(event, next)`. Call `next()` to pass the event to the next handler in the chain, or return a result to claim the event. ```typescript import { ApiGatewayInputHandler, type APIGatewayEvent, type APIGatewayProxyResult } from "@cloudi/aws"; import type { NextFunction } from "@cloudi/aws"; class ListUsersHandler implements ApiGatewayInputHandler.Interface { constructor(private userService: IUserService) {} async execute(event: APIGatewayEvent, next: NextFunction): Promise { if (event.httpMethod !== "GET" || !event.path.startsWith("/users")) { return next(); } const users = await this.userService.listUsers(); return { statusCode: 200, headers: { "Content-Type": "application/json" }, body: JSON.stringify({ users }) }; } } export const listUsersHandler = ApiGatewayInputHandler.createImplementation({ implementation: ListUsersHandler, dependencies: [UserService] }); ``` ### 2. Create Your Lambda Handler ```typescript import { createEventHandler } from "@cloudi/aws"; import { listUsersHandler } from "./handlers/ListUsersHandler"; import { userService } from "./services/UserService"; export const handler = createEventHandler(async container => { container.register(userService).inSingletonScope(); container.register(listUsersHandler).inSingletonScope(); }); ``` ### 3. Deploy and Invoke Your Lambda function will automatically: - โœ… Walk the registered handler chain on each invocation - โœ… Inject dependencies from the container - โœ… Execute your business logic - โœ… Return the result from the first handler that claims the event ## Core Concepts ### Middleware Chain All registered handlers form a single middleware chain. Each handler receives the event and a `next` function. The chain executes in registration order โ€” first registered runs first. ``` event โ†“ Handler1.execute(event, next) โ†’ calls next() โ€” passes through โ†“ Handler2.execute(event, next) โ†’ returns result โ€” claims the event โ†“ result ``` If no handler claims the event, an error is thrown. ### Handler Interface ```typescript export interface ICloudHandler { execute(event: TEvent, next: NextFunction): Promise; } export type NextFunction = () => Promise; ``` Every handler implements this interface via one of the typed event handler namespaces. ### Event Handler Namespaces Each AWS event type has a dedicated namespace with `.Interface` for type-safe implementation and `.createImplementation()` for DI registration: | Namespace | Event type | Result type | | ------------------------- | --------------------- | ----------------------- | | `ApiGatewayInputHandler` | `APIGatewayEvent` | `APIGatewayProxyResult` | | `SnsInputHandler` | `SNSEvent` | `SnsResult` | | `SqsInputHandler` | `SQSEvent` | `SqsResult` | | `S3InputHandler` | `S3Event` | `S3Result` | | `DynamoDBInputHandler` | `DynamoDBStreamEvent` | `DynamoDBResult` | | `EventBridgeInputHandler` | `EventBridgeEvent` | `EventBridgeResult` | | `RawInputHandler` | `any` | `any` | All namespaces register under the shared `CloudHandler` abstraction so `createEventHandler` can resolve the full chain from the container. ### Dependency Injection Use the DI container to manage dependencies: ```typescript import { Abstraction } from "@webiny/di"; // Define abstraction const Logger = new Abstraction("Logger"); // Create implementation const logger = Logger.createImplementation({ implementation: class ConsoleLogger implements ILogger { info(message: string): void { console.log(message); } }, dependencies: [] }); // Register in container container.register(logger).inSingletonScope(); ``` ### AWS Lambda Context Access Lambda context in your services (e.g., for request ID, remaining time): ```typescript import { AwsLambdaContext } from "@cloudi/aws"; import type { Context } from "aws-lambda"; class ContextAwareLogger implements ILogger { constructor(private context: Context) {} info(message: string): void { console.log({ message, requestId: this.context.awsRequestId }); } } const logger = Logger.createImplementation({ implementation: ContextAwareLogger, dependencies: [AwsLambdaContext] // injected per invocation }); ``` ## Examples ### Single-Function Multi-Event Lambda Handle multiple event types in one Lambda. Each handler checks the event and either claims it or calls `next()`: ```typescript export const handler = createEventHandler(async container => { container.register(logger).inSingletonScope(); container.register(userService).inSingletonScope(); // Registered in order โ€” each checks the event and passes through or handles container.register(apiGatewayHandler).inSingletonScope(); container.register(snsHandler).inSingletonScope(); container.register(sqsHandler).inSingletonScope(); }); ``` ### Pre/Post Processing Because handlers are middleware, you can wrap logic around the rest of the chain: ```typescript const timingHandler = ApiGatewayInputHandler.createImplementation({ implementation: class { async execute(event: APIGatewayEvent, next: NextFunction) { const start = Date.now(); const result = await next(); console.log(`Request took ${Date.now() - start}ms`); return result; } }, dependencies: [] }); export const handler = createEventHandler(async container => { container.register(timingHandler).inSingletonScope(); // runs first container.register(listUsersHandler).inSingletonScope(); // claims the event }); ``` ### Testing Mock dependencies for easy unit testing: ```typescript import { describe, it, expect } from "vitest"; import { createEventHandler } from "@cloudi/aws"; import { ApiGatewayInputHandler } from "@cloudi/aws"; describe("ListUsersHandler", () => { it("should list users", async () => { const MockUserService = UserService.createImplementation({ implementation: class { async listUsers() { return [{ id: "1", name: "Test User" }]; } }, dependencies: [] }); const handler = createEventHandler(container => { container.register(MockUserService); container.register(listUsersHandler); }); const result = await handler({ httpMethod: "GET", path: "/users", headers: {}, requestContext: {} as any, body: null, isBase64Encoded: false }); expect(result.statusCode).toBe(200); expect(JSON.parse(result.body).users).toHaveLength(1); }); }); ``` ### Using with pino-lambda ```typescript import pino from "pino"; import { pinoLambdaDestination } from "pino-lambda"; import { AwsLambdaContext } from "@cloudi/aws"; const logger = Logger.createImplementation({ implementation: class PinoLogger implements ILogger { private logger: pino.Logger; private destination: ReturnType; constructor(context: Context) { this.destination = pinoLambdaDestination(); this.logger = pino({}, this.destination); this.destination.setContext(context); } info(message: string, data?: any): void { this.logger.info(data || {}, message); } }, dependencies: [AwsLambdaContext] }); ``` ## Architecture ### How It Works 1. **Cold start**: `createEventHandler` runs `setup(container)` once โ€” registers all implementations 2. **Per invocation**: - Current `event` and `context` are registered as instances - All `CloudHandler` implementations are resolved from the container - A middleware chain is built in registration order - `chain()` is called โ€” handlers execute sequentially via `next()` - First handler to return a value wins ``` AWS Lambda Invoke โ†“ createEventHandler (warm: skip setup) โ†“ container.resolveAll(CloudHandler) โ†“ Build middleware chain (registration order) โ†“ Handler1.execute(event, next) โ†“ next() Handler2.execute(event, next) โ†’ returns result โ†“ Return result to Lambda ``` ### Project Structure ``` src/ โ”œโ”€โ”€ abstractions/ โ”‚ โ”œโ”€โ”€ functions/ # event handler namespaces (ApiGatewayInputHandler, SnsInputHandler, etc.) โ”‚ โ”œโ”€โ”€ CloudHandler.ts # Base abstraction + ICloudHandler interface โ”‚ โ”œโ”€โ”€ AwsLambdaContext.ts โ”‚ โ””โ”€โ”€ AwsLambdaEvent.ts โ”œโ”€โ”€ createEventHandler.ts # Lambda handler factory โ”œโ”€โ”€ types.ts # NextFunction, FunctionSetup, etc. โ””โ”€โ”€ index.ts ``` ## API Reference ### `createEventHandler(setup)` Creates a Lambda handler. `setup` runs once on cold start. ```typescript type FunctionSetup = (container: Container) => Promise | void; export const handler = createEventHandler(async container => { container.register(myHandler).inSingletonScope(); }); ``` ### Event Handler Namespaces Each namespace exposes: ```typescript // Type for implementing the handler namespace ApiGatewayInputHandler { type Interface = ICloudHandler; function createImplementation>(params: { implementation: I; dependencies: Dependencies; }): Implementation; } ``` Usage: ```typescript class MyHandler implements ApiGatewayInputHandler.Interface { async execute(event: APIGatewayEvent, next: NextFunction): Promise { // ... } } export const myHandler = ApiGatewayInputHandler.createImplementation({ implementation: MyHandler, dependencies: [SomeDependency] }); ``` ### `CloudHandler` The base `Abstraction` that all event handler namespaces register under. Advanced usage: ```typescript import { CloudHandler } from "@cloudi/aws"; // Resolve all handlers directly (createEventHandler does this internally) const handlers = container.resolveAll(CloudHandler); ``` ### `NextFunction` ```typescript type NextFunction = () => Promise; ``` Call `next()` from within `execute` to pass the event to the next handler in the chain. ## Best Practices ### โœ… Do - Keep handlers focused โ€” one responsibility per handler - Guard early with `if (!matches) return next()` at the top of `execute` - Register services in singleton scope when they hold no per-request state - Use `ApiGatewayInputHandler`, `SnsInputHandler`, etc. for type safety even if the event type overlaps ### โŒ Don't - Don't create services inside handlers โ€” use DI - Don't mix business logic with AWS-specific marshalling code - Don't register the same singleton implementation twice (it will appear twice in the chain) - Don't forget to call `return next()` โ€” a handler that falls through without returning will return `undefined` ## Troubleshooting ### No handler processes the event `Error: No registered function implementation handled this event` All registered handlers called `next()` without returning a value. Check that at least one handler recognises the event shape and returns a result. ### Handler executes for wrong event type Each handler is responsible for its own type check. Add an early guard: ```typescript async execute(event: any, next: NextFunction) { if (!event.httpMethod) return next(); // handle API Gateway event } ``` ### Dependency not resolving 1. Ensure the abstraction is registered in the container before it's needed 2. Check the `dependencies` array in `createImplementation` matches constructor parameter order 3. Verify the same `Abstraction` instance is used everywhere (don't `new Abstraction(...)` twice) ## Related Documentation - [@webiny/di](https://www.npmjs.com/package/@webiny/di) โ€” DI container API - [AWS Lambda Abstractions](./src/abstractions/AWS_LAMBDA_ABSTRACTIONS.md) โ€” Context and Event usage - [Examples](./src/features/) โ€” Complete working examples ## License MIT