How To Build Clean NodeJS REST API with AWS Lambda
Building a clean NodeJS REST API with AWS Lambda

How To Build Clean NodeJS REST API with AWS Lambda

Hello 👋🏼,

My name is Andrei, I am a Senior NodeJS Developer & AWS Architect Consultant / Contractor / Freelancer.

This post will guide you through designing a multi-endpoint #lambda manager using #TypeScript by implementing #AWS API Gateway (authenticated with Cognito) + AWS Lambda (for the processing layer) + AWS #DyanmoDB (for the data storage layer)

As Christmas 🎄 is just 2 months away, the application we'll build is about the Christmas market event. (tiny houses around the city with Christmas theme products).

The architecture and the application flow are already present in this article posted one week ago.

GitHub Repository: Link

Clean Lambda Repository: Link

___________________

Prerequisites:

  • An AWS Account
  • AWS CLI configured with the AWS Account
  • AWS SAM to deploy the template
  • Basic knowledge of how lambdas works

______________________

Application Flow:

No alt text provided for this image

The application entry point is the handler method.

  1. Handler

Inside this function, the event is transformed by retrieving only the needed information. (path parameters, query parameters, path, method, body) and then the #dispatcher #function is called.

2. Dispatcher

The dispatcher creates an instance of the ChristmasMarketService and the ValidatorService. A new API string variable is created based on the "${path}:${method}"

Validate method of the Validator is called.

Switch based on the API string is executed on the Endpoints enum which contains all the API combinations.

3. Validator

The validator's constructor is private, the instance is created with the getInstance method. The constructor creates an instance of AJV.

The validate method checks if the schema is already present inside the AJV instance cache

  • If not, it is imported and added to the #AJV cache, after that the method is recalled.
  • If is present and the event has any schema errors, throws an error

4. ChristmasMarketService

The dispatcher invokes the method of the ChristmasMarketService defined inside the #API case.

5. DynamoDBService

This service is a wrapper of the AWS #DynamoDB #SDK to avoid duplicated code.

A completely clean microservice using NodeJS and TypeScript! 🔥

No alt text provided for this image

___________________

Code Structure:

Lambda folder

  1. Lambda's Directory:

  • src 🧮 - inside this folder is present the source code
  • node_modules 📚 - this folder is created by npm to keep external libraries, it is needed inside the lambda
  • dist 💻 - this folder is used to save the NodeJS compiled code by TypeScript
  • package.json 🗒 - this is the JSON file used to handle dependencies & devDependencies (used only for development)
  • tsconfig.json 🗒 - this file has the typescript configurations
  • .eslintrc.js 🗒 - this file has the typescript’s lint rules

2. Source Code's Directory:

No alt text provided for this image

  • event 📝 - inside this folder, we can save the JSON file used to validate the input event
  • interfaces 📒

- input-models.ts - this file contains the input interfaces

- response-models.ts - this file contains the response interfaces

  • services 🚚

- validator-service.ts - this file contains the logic to validate the event based on the JSON schema file

- [feature]-service.ts - this file contains the application logic (Example: CRUD operations on DynamoDB table)

  • utils 🛠

- [aws-service]-wrapper - this file can be created for an AWS service where you can wrap logic to avoid duplicated code

  • logger.ts - this file contains the service to implement the logger
  • dispatcher.ts - this service dispatches the input event to the right application logic
  • handler.ts - this is the input of the lambda - the main()
  • Makefile - file used by SAM to compile the lambda code

______________________

Deployment: 🚀

We have to update the lambda implementation based on the architecture already created from the last article.

Add the following configuration to the Properties of the lambda definition:

  Metadata: # Manage lambda build

   BuildMethod: makefiled        

You can follow the first article for the deployment step.

______________________

That’s it! You can use a #powerful, easy-to-use AWS #Lambda pattern inside your projects! 🎉

I hope it could help your journey into the #AWS world 😎

I have created a clean repository too as a starting development environment.

If you need any help or considerations, feel free to drop me a message.  📩







Nice to read and I hope this series will continue!

To view or add a comment, sign in

More articles by Andrei Marius Diaconovici

Others also viewed

Explore content categories