1

We have a project in node js with serverless architecture. We are planning to use Middy for AWS lambda function implementation. Our problem is that we do not want to create separate lambda function for each and every api end points, We have very large application which might have more than 100 api end points.

We want to use handler in following way. For ex: We have functionality to manager user. It include api endpoints like

  1. Add User
  2. Edit User
  3. Get user

We want to use all these three end points using single lambda function as all three api's shares some logic's and models.

  • Why don't you use express with the serverless framework, it suits your case. check out my repository can guide you https://github.com/jatinmehrotra/Serverless_Express_API – Jatin Mehrotra Sep 03 '21 at 10:54
  • We want to use all the good middlewares and features if Middy also. Can we combine Middy and express routing together? – Nivedita Vijayvargiya Sep 03 '21 at 13:59

1 Answers1

0

Middy doesn't have a core middleware for routing because this pattern goes against security best practices (ie least privilege). In your case the GET user endpoint would have permission to add a user if there was such a vulnerability.

I would recommend having a lambda for each endpoint. Each endpoint could import from the same file. This ensures each endpoint can have its own IAM role and input validation.

However, if you must use the same lambda you could setup API Gateway to have different path point to the same lambda. Then you could use event.requestContext to create a simple router to meet your needs.

Edit: As of v3, Middy now contains @middy/http-router which supports routing within a lambda. This is to cover the ALB use case.

will Farrell
  • 1,733
  • 1
  • 16
  • 21