Skip to content
Clean Lambda Function
Lean Mean Clean Apps

Five templates, one search box. Everything in the docs is in here, including the bits we hoped you'd skim.

Try one of these

Results say which template they came from, so you won't end up reading about Lambda when you wanted GraphQL. Again.

Architecture

This solution is loosely based on Clean Architecture. It’s by no means perfect, so we prefer to call it Lean Mean Clean Architecture. The inspiration is Jason Taylor’s Clean Architecture template, with some ideas taken further and others scaled back.

Application sits in the middle and depends on nothing. Presentation and Infrastructure sit around the outside, and both depend on Application. The outside can change without the middle noticing.

PRESENTATIONINFRASTRUCTUREAPPLICATIONdepended on by everyonePresentationThe Lambda handler. Turns JSON into queries.ApplicationEntities, rules, queries and validation.InfrastructureSQLite, EF Core and the repositories.
  1. Presentation exposes the Application layer as an AWS Lambda handler. It builds the service provider once per container, opens a new scope for each invocation, sends a query through Mediator and maps the result into its response model.
  2. Application owns the domain and the business logic. Commands and queries are validated and handled here, and each domain type has its own repository interface. Application doesn’t care what fulfils those contracts, as long as something does.
  3. Infrastructure owns the data. It implements the repository interfaces and maps its own data models onto the Application entities, so persistence details don’t leak upwards.

There’s a little CQRS in the style of the commands and queries, but reads and writes use the same database underneath. CQRS-curious, if you like.

In textbook Clean Architecture, Presentation never references Infrastructure. Here it does, so it can wire up dependency injection without a fourth project whose only job is to call two methods.

To keep that from turning into a free-for-all, every class in Infrastructure is internal. Presentation can call AddInfrastructure and nothing else. If you try to new up a repository from the handler, the compiler will politely decline.

HandlerMediatorValidationQuery handlerRepositoryPresentationApplicationInfrastructure
  1. Lambda deserialises the request with the source-generated serializer and calls a method on Handler.
  2. The handler opens a dependency injection scope and sends a query through Mediator, with a cancellation token that fires when the invocation runs out of time.
  3. The ValidationBehaviour runs every FluentValidation validator for the query. Any failure stops here with a ValidationException.
  4. The query handler calls a repository interface.
  5. The repository in Infrastructure runs the query against SQLite with Entity Framework Core and maps the results back to Application entities.
  6. The handler maps the entities to its response models with Mapperly, and Lambda serialises the response.