The packages/functions
folder
The packages/functions
folder is a crucial component of the Blitzship monorepo architecture,
dedicated to housing the code for your AWS Lambda handler functions.
Purpose
The functions package serves as the entry point for serverless compute in your SaaS application. It contains lightweight handlers that act as bridges between your AWS infrastructure and the core business logic.
This package is not meant to contain any business logic. It is simply a lightweight entry point for your AWS Lambda functions. All business logic should be implemented in the core package and be called from here.
The functions package is designed to:
- Contain Lambda function handlers
- Act as lightweight entry points
- Call into the core package for business logic
- Maintain a clear separation of concerns in your serverless application
Folder Structure
/auth
Handles authentication-related lambda functions. The following file is already implemented to show-case the structure.
users.ts
: Receives DynamoDB streams from the Auth table calls methods from the core package to save user data to the main application database table.
/billing
Manages billing-related functions. The following file is already implemented to show-case the structure.
webhooks.ts
: Handles incoming Stripe webhooks, verifies their authenticity, and calls into the core package to handle the business logic for each event type.
A great example to understand how the functions
package integrates with the rest of the monorepo is by considering how Stripe Webhooks are handled.
- In the
infra/api.ts
file we define what AWS infrastructure we need: An API Gateway and a Lambda function. - We point the Lambda function to a handler code that is defined here, in the
packages/functions
directory. - The
packages/functions
is not supposed to contain any business logic, it should only contain the code that receives the API request. - These lambda handlers, in turn, call into functions defined in the
packages/core
package to perform the actual business logic (sending emails, creating users, provioning services etc.)
This is a very scalable way of structuring your code, as it allows you to separate the concerns of infrastructure, business logic and handlers and should guide you for years to come.