Recommended approach for building a Lambda Function to Handle multiple Routes like ExpressJS

0

Hi,

I need to build a serverless API that can handle multiple route paths. In a normal NodeJS application this would be done using ExpressJS or one of the myriad other HTTP Server implementations. My question is what is the recommended approach from a good design perspective in Lambda?

  • I could write a switch case in the lambda to handle the different scenarios
  • I could just write different lambda functions for each of the routes and frontend them with API gateway and try to manage routing in API Gateway. This will get complicated for scenarios like /getItem and /getItem/id
  • I could just use expressJS or one of those frameworks in my Lambda code and let it handle the routing part
  • I found https://github.com/awslabs/aws-serverless-express . Or is this the recommended approach?

I am open to comments, suggestions and recommendations including ideas that may not be listed above.

thanks & regards

AWS
Rohit_S
asked 4 years ago2154 views
1 Answer
0
Accepted Answer

Hi,

I think starting with aws-serverless-express is a good place to start if you're looking to quickly migrate an existing ExpressJS application. This will make it much easier to get up and running and debugging locally if you are already familiar with ExpressJS.

As your application grows you may want to start looking towards using something a bit more lightweight like middy and breaking up your functions. The idea here is by moving to a more modular approach you'll be able to take advantage of additional API Gateway functionality, follow the principle of least privilege with your policies, and right sizing your lambda functions (ie: lambda-power-tuning).

A good way to get started would be to pick a serverless framework/toolchain. A few worth exploring are:

if it's something small you can start simple and then ramp up as needed.

Edit: I failed to mention; if you are beginning a new project it may be better to look into designing your architecture with serverless in mind, which would be more towards focusing on creating individual lambdas, rather than a single lambda handling multiple routes

AWS
answered 4 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions