This is a starter project to deploy NestJS REST API to AWS Lambda using Serverless Framework. It also supports Prisma for ORM and Swagger UI for API documentation. It uses the following technologies:
- NodeJS 16
- NestJS
- AWS Lambda
- AWS API Gateway
- AWS RDS Postgres
- AWS S3
- AWS CloudWatch
- AWS CLoudFormation
- GitHub Actions (See this workflow template)
- Docker
- Swagger
- Slack (Notifications)
$ yarn addYou need to create .env file in the root directory and add the necessary environment variables.
Don't forget to add these environment variables to the Secrets in GitHub Repository Settings.
See GitHub Secrets.
Also, you need add them to the yml files in ./github and serverless.yml file.
To use GitHub Actions, you need the followings:
CHANNEL_NAMEand set its value to your Slack channel name. (e.g., general, random)PATand set its value to your GitHub Personal Access Token. Needs to have repo scope access. Refer hereSLACK_WEBHOOKand set its value to your Slack webhook URL. Get one here.SERVERLESS_ACCESS_KEYand set its value to your Serverless Access Key ID. Refer https://app.serverless.com/{your_org_name}}/settings/accessKeys- You can provide your Slack Message icon by modifying the
SLACK_ICONin the slack-notifier file.
To use Prisma, you need the following:
DATABASE_URLand set its value to your database URL. (e.g., postgres://user:password@host:port/database). Don't forget to add it to the Secrets in GitHub Repository Settings and also GitHub Actions.
# If you have Prisma as ORM
# prisma generate
$ npx prisma generate
# prisma migrate
$ npx prisma migrate dev
# development
$ yarn start
# watch mode
$ yarn start:dev
# production mode
$ yarn start:prodLocal URL: http://localhost:3000/api
# unit tests
$ yarn test
# e2e tests
$ yarn test:e2e
# test coverage
$ yarn test:covGitHub Actions will deploy the app to AWS CloudFormation stack. You can also deploy manually by running the followings:
- AWS account
- Create an IAM user with programmatic access. See AWS IAM User
- AWS CLI
- Install. See AWS CLI docs
- Configure credentials. See AWS CLI docs and AWS SAM docs
- Serverless Framework.
- Account. See Serverless
- Serverless installation. See Serverless docs
$ yarn global add serverless
- Docker
- Install. See Docker docs
- Start Docker daemon. See Docker docs
$ yarn global add serverless # if you don't have serverless installed
$ serverless offline startThis will deploy the lambda to AWS and create a new API Gateway endpoint. The endpoint will be printed to the console. You can also find it in the AWS console.
This step uses the AWS CLI and Serverless Framework.
$ yarn install
$ docker build .
$ serverless deploy --stage production