production-ready-serverless-nestjs
production-ready-serverless-nestjs copied to clipboard
Backend starter template for a NestJS GraphQL API on AWS Lambda with security and performance best practices
Production Ready Serverless NestJS
This project contains a backend starter template for running NestJS GraphQL APIs on AWS Lambda with security and performance best practices.
Features
- GraphQL configuration (Code-first approach)
- Prisma Database config with migrations
- Docker database for local development
- AWS Deployment with Serverless Framework
- AWS Lambda NodeJS 18x configuration and optimizations
- GitHub actions for CI/CD
Not included
- Authentication/Authorization
Technology Choices
NestJS
- Opinionated, mature backend framework with active community
- Supports dependency injection out of the box
- First-class typescript support
GraphQL
- Provides a query language that lets clients specify the data they need
- Strong support in frontend frameworks like React
AWS Lambda
- Pay-per-use pricing is ideal for early-stage startups where traffic and access patterns are unknown
- Cold starts can be minimized by keeping package bundles small, caching server between invocations and keeping lambdas warm
Serverless framework
- Strong community of plugins and supports for NodeJS/Typescript
Prisma
- ORM with great developer experience
- Excellent Typescript support
Supabase
- Hosted serverless Postgres database
Dependencies
- NodeJS
- NVM
brew install nvm - Yarn
- Docker
- VSCode
- AWS Account
Setup
Local Development
-
Create a
.envfile using .env.example as an example -
nvm use -
yarn install -
Launch Supabase in Docker with
npx supabase start -
Run database migrations locally in a separate terminal if it's a first time setup with
npx prisma migrate dev -
Seed the database with
npx prisma db seed -
Start the NestJS server with
yarn start:dev -
Call the endpoints in requests.http to test the API.
Integration tests
yarn test:e2e
Webpack bundling
yarn build # with webpack
# run binary locally
node dist/main.js
Serverless Offline
Test AWS Lambda packaging and run locally with:
npx serverless offline
AWS Deployment
Note - create all AWS and Supabase resources in the us-east-1 AWS region.
-
Setup your AWS Credentials for deployments
-
Create a free-tier Supabase database.
-
Create an AWS SSM Parameter in the AWS console named
/dev/database/urlwith the secure string type and save the DB connection string.Confirm it exists with:
aws ssm get-parameter --name /dev/database/url --with-decryption -
Install dependencies:
nvm use 18 yarn install -
Deploy the app to AWS:
npx serverless deploy
Update requests.http with the output of the deployment to call the API.
Database Migrations
# create migrations
npx prisma migrate dev --name <MIGRATION_NAME>
# run migration
npx prisma migrate dev
# reset development database
npx prisma migrate reset
Note - an ERD diagram for the database schema will be generated under /docs - see diagram.
Production Database Migrations
Production database migrations should be run in a CI/CD pipeline but can also be run locally with:
npx prisma migrate deploy
See the Prisma docs for more information.