Skip to content

Deploy NestJS REST API to AWS Lambda using Serverless Framework

Notifications You must be signed in to change notification settings

burak-kara/nestjs-serverless-aws-lambda

Repository files navigation

NestJS App Serverless

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)

Installation

$ yarn add

Running the app

You 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_NAME and set its value to your Slack channel name. (e.g., general, random)
  • PAT and set its value to your GitHub Personal Access Token. Needs to have repo scope access. Refer here
  • SLACK_WEBHOOK and set its value to your Slack webhook URL. Get one here.
  • SERVERLESS_ACCESS_KEY and 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_ICON in the slack-notifier file.

To use Prisma, you need the following:

  • DATABASE_URL and 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:prod

Local URL: http://localhost:3000/api

Test

# unit tests
$ yarn test

# e2e tests
$ yarn test:e2e

# test coverage
$ yarn test:cov

Deploy

GitHub Actions will deploy the app to AWS CloudFormation stack. You can also deploy manually by running the followings:

Prerequisites

Local Lambda

$ yarn global add serverless # if you don't have serverless installed
$ serverless offline start

Deploy to AWS

This 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