How parameshg/stateful-serverless-api works
Project Type and Purpose
• This project is an ASP.NET Core Web API that is designed to run as an AWS Lambda function. It leverages the Amazon.Lambda.AspNetCoreServer NuGet package to bridge API requests (via API Gateway or an Application Load Balancer) into the ASP.NET Core framework.
• The application supports both cloud-based serverless execution and local development (using Kestrel), and it can be packaged as a Docker container.
Main Components
• AWS Components:
– API Gateway / Application Load Balancer: These serve as the entry point for HTTP requests when deployed.
– AWS Lambda: Represented by the LambdaEntryPoint class which bootstraps the ASP.NET Core hosting framework for serverless execution.
– Serverless Resources: Indicated by the serverless.tf file (Terraform scripts that declare the necessary AWS resources).
• ASP.NET Core Components:
– LambdaEntryPoint.cs: Acts as the Lambda adapter deriving from APIGatewayProxyFunction (or the variation for ALB), initiating the ASP.NET Core pipeline.
– LocalEntryPoint.cs: Provides a Main() for local execution using Kestrel.
– Startup.cs: The typical startup configuration that registers middleware, services, routing, etc.
• Controllers: Such as HomeController, IntegerController, and TestController—handle API endpoints.
• Domain & Business Logic:
– Domain (e.g., Ticker.cs) represents the core business entities.
– Handlers (e.g., CreateCounterHandler, IncrementCounterHandler, etc.) encapsulate the business logic for processing requests.
– Pipelines & Middlewares: These include ValidationMiddleware, IdentityMiddleware, and ValidationBehavior which help in pre-processing requests and enforcing policies.
• Data Access:
– Repositories: Components like DynamoRepository, NullRepository, and extensions (DynamoExtensions) that abstract data access operations.
• Request Models: Under the Requests folder there are request classes (CreateRequest, IncrementRequest, etc.) representing the data contract for incoming API calls.
Relationships and Interactions
• That requests are forwarded to AWS Lambda (LambdaEntryPoint) which then:
– Bootstraps the ASP.NET Core pipeline (via Startup.cs).
– Passes through configured middleware (IdentityMiddleware, ValidationMiddleware).
– Routes to appropriate controllers (HomeController, etc.).
– Invokes corresponding Handler classes which process business logic.
– Interacts with the Repository layer to persist or retrieve data.
• That for local development the LocalEntryPoint.cs is used, running the service on Kestrel.
• Deployment considerations such as packaging as a Docker image, noting that Dockerfile handles either host-based builds or multi-stage builds.
Architectural Patterns and Design Principles
• Serverless Architecture: Emphasize the design for AWS Lambda and API Gateway integration.
• Separation of Concerns: Clear division between API (Controllers) and business logic (Handlers), with middleware handling cross-cutting concerns.
• Dependency Injection: Standard practice in ASP.NET Core for injecting services and repositories.
• Environment-Specific Configurations: Illustrate how different entry points (LambdaEntryPoint vs. LocalEntryPoint) support distinct deployment environments.
Final Checklist
• Maintain clarity by breaking the system into well-defined components.
• Notable code-level components like Handlers, Middleware, and Repositories that represent the business processing logic.