The repo contains an example NestJS app running on AWS Lambda with an EventBridge source. The use case is a simple service that emails a customer when a payment event is received using Twilio Sendgrid as the email delivery service.
This project uses NestJS Standalone applications with AWS Lambda. NestJS Standalone apps can use the NestJS DI system without starting an entire webserver.
-
Create a SendGrid account
-
Get a Sendgrid API key
-
Add your Sendgrid API key to the AWS SSM with the prefix
/dev/sendgrid/apiKey
using the Secure String type. -
Update the
serverless.yml
file with your verified email domain.
export AWS_PROFILE=<YOUR_PROFILE>
# package with webpack
yarn build
npx serverless deploy --stage dev
The project uses Webpack to bundle the Typescript for NodeJS. The total bundled deployment size is 1.2mb
.
See: https://docs.nestjs.com/faq/serverless#benchmarks
The service reacts to one payments.InvoicePaid
event.
The Event-Carried State Transfer pattern is used to pass around the relevant information about the User that's needed to send an email.
Example event:
{
"version": "0",
"id": "c82f1e7d-4b0b-9d75-3f11-89be92935a12",
"detail-type": "payments.InvoicePaid",
"source": "payments.api",
"account": "111111111111",
"time": "2022-12-04T11:22:08Z",
"region": "us-east-1",
"resources": [],
"detail": {
"user": {
"id": 1,
"email": "user@gmail.com"
}
}
}