NestJS + Serverless + Lambda + AWS — In shortest steps.

Recently I wanted to try out deploying a NestJS project in AWS as a lambda using Serverless framework. This note is about what I learned, hoping that it may help others too.

Let's get familiarised with the terms used in the header.

NestJS — A framework for Node.js based server-side applications.

What problem does it solve? Architecture. Nest provides an out-of-the-box application architecture that allows developers to create highly testable, scalable, loosely coupled, and easily maintainable applications.

Serverless — A framework that helps you develop and deploy your AWS Lambda functions, along with the AWS infrastructure resources they require.

What problem does it solve? Complexity related to cloud deployment. It’s a CLI that offers structure, automation, and best practices out-of-the-box, allowing you to focus on building sophisticated, event-driven, serverless architectures, comprised of Functions and Events.

The goal is to create a basic NestJS app, use Serverless framework to wrap it as a Lambda, and then deploy the app into AWS.

On a high level there are three steps:

  1. Creating a basic NestJS app
  2. Setup AWS account
  3. Creating a Lambda out of the NestJS app
  4. Deploying to AWS

Below are the steps in detail:

1. Creating a basic NestJS app

Pre-requisites:

NodeJS, Visual Studio Code, NestCLI, Serverless

Open Visual Studio Code, open terminal.

The next step is to create a project with the name that you use. Mine is ‘floaws’ (FirstLambdaOnAWS).

$ nest new floaws

When prompted package manager choice, select ‘npm’.

This will create a project in NestJS framework with a GET endpoint in the current directory. Open the newly created project in VS Code. You should something that looks like below:

Test the project by running it and verifying the GET response.

Terminal 1 in VS Code:

nest start

Terminal 2 in VS Code:

curl http://localhost:3000

The expected output in the terminal is:

Hello World!

2. Setup AWS account

If you do not have it already, signup for an AWS account.

Once an account is active, create a user with the AWS IAM service and get its access key and secret access key. When that information is available create a hidden folder called ‘.aws’ in your working directory with a file name ‘credentials’ and paste the below content. Save the file once done.

[default]
aws_access_key_id = YOUR_ACCESS_KEY
aws_secret_access_key = YOUR_SECRET_ACCESS_KEY

In VS Code terminal of a Mac machine, the commands would be:

mkdir .aws
cd .aws
touch credentials
open .

3. Creating a Lambda out of the NestJS app

API Gateway creates a REST API in front of your lambdas. By default, each Serverless project generates a new API Gateway.

Serverless need an entry point to compile the NestJS app to a lambda function. This entry point will be a handler object exported in the file lambda.ts. Create the lambda.ts file in the src folder of your application as shown below.

In the lambda.ts file copy and paste the following code:

Once the lambda entry point is created, configure Serverless to build this lambda. Create a file serverless.yaml at the root of your project with the content:

With this configuration, Serverless will create the Lambda function main that will serve all the routes and methods of the API.

The next step is to install a few dependencies:

aws-serverless-express : This library enables you to utilize AWS Lambda and Amazon API Gateway to respond to web and API requests using your existing Node.js
aws-lambda: AWS Lambda lets you run code without provisioning or managing servers.
serverless-plugin-typescript: Serverless plugin for Typescript support that works out of the box without the need to install any other compiler or plugins.
serverless-plugin-optimize: Plugin to transpile and minify your code
serverless-offline plugin: Plugin to be able to test your app offline.

npm install --save aws-serverless-express
npm install --save aws-lambda
npm install --save-dev serverless-plugin-typescript
npm install --save-dev serverless-plugin-optimize
npm install --save-dev serverless-offline plugin

Once done, test the serverless configuration and the app by :

sls offline start

If you see a compile error related to ‘incremental’, set that property value as ‘false’ in tsconfig.json file.

If that succeeds, you should see a similar message:

Stop running the app (Ctrl+C), and try deploying it by:

sls deploy -v

If everything goes well you should see:

You can hit the URL provided in ‘ServiceEndPoint’the message and test the GET function.

Once you are happy with the results, do not forget to remove the cloud assets that were created.

The command to remove the lambda deployment is:

serverless remove

Hope this helps!

"Curious to know how things work and interested in making things work". https://nishabe.github.io/