Is it a good idea to switch handling multiple routes in the same lambda to api gateway for the same lambda?

0

Hello guys, My backend architecture is a normal API Gateway routing for the lambda function. Due to some business reasons, each group of resources are directed to the same lambda function.

Inside the lambda function, a layer of switching is created to handle the multiple resources along with MVC architecture. According to resource and httpMethod in the incoming event the correct controller and function are being called.

Some suggested that we modify Api Gateway's settings so that we add the fields "controller" and "function" based on which resource is being called and dynamically call them inside the lambda.

What do you think of this approach and what are its pros and cons? How will it affect the performance? Is there any security aspect we should consider ?

  • please accept the answer if it was useful

1 Answer
0

multiple routes in the same lambda It's possible, but it contradicts the micro-services paradigm. It's better to split the logic into different Lambda functions. Functions should be decoupled if possible (e.g., using a queue). In this case, you can scale and monitor all parts of the application independently.

Due to some business reasons, each group of resources are directed to the same lambda function. if this approach can not be changed, that's ok

Is there any security aspect we should consider ? if you split one function into many, you can create permissions for every small function according to the Least Privilege Principle

How will it affect the performance? smaller functions load faster and are cheaper at the end

profile picture
EXPERT
answered 19 days ago
  • Thank you for your answer but what do you think of modifying the event in API Gateway and dynamically call the controller inside lambda with its respective function instead of routing inside the lambda function ?

  • You can try this, but it's complicating the architecture. The simpler thing is to use different API Gateway routes to different Lambda Functions

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