Using API Gateway to route requests to different Lambda functions

0

Is there any way I can use the API Gateway to route POST requests to different Lambda functions, depending on a simple test of the POST body? eg body.action == "create" ?

3 Answers
0

Yes, it is possible to use Amazon API Gateway to route POST requests to different Lambda functions based on the content of the request body, such as checking if body.action == "create". However, this requires a bit of configuration and possibly a custom authorizer or middleware.

Here are a few approaches you could consider:

  1. Use a single Lambda function as a router: You can set up API Gateway to send all POST requests to a single Lambda function. This function can then examine the request body, determine the appropriate action, and invoke the corresponding Lambda function for that action. This approach keeps your API Gateway configuration simple but puts the routing logic in your code.

  2. Utilize API Gateway's request body mapping: You can use API Gateway's request body mapping templates to examine the content of the POST body and route to different Lambda functions based on the content. This approach requires more complex API Gateway configuration but keeps your Lambda functions focused on their specific tasks.

  3. Implement a custom authorizer: You could create a custom authorizer Lambda function that examines the request body and determines which Lambda function should handle the request. The authorizer can then return the appropriate IAM policy to allow API Gateway to invoke the correct Lambda function.

  4. Use API Gateway's model schema validation: If your POST bodies follow a specific schema, you can use API Gateway's request validation feature with model schemas. Different schemas could route to different Lambda functions.

It's worth noting that while these approaches are possible, they may add complexity to your API design. In many cases, it might be simpler and more in line with RESTful principles to use different API endpoints (e.g., /create, /update) that map directly to specific Lambda functions, rather than routing based on the request body content.

Remember to consider factors like performance, maintainability, and adherence to API design best practices when choosing your approach.
Sources
Integration with AWS Lambda - AWS Serverless Multi-Tier Architectures with Amazon API Gateway and AWS Lambda
Invoking a Lambda function using an Amazon API Gateway endpoint - AWS Lambda
Is it a good idea to switch handling multiple routes in the same lambda to api gateway for the same lambda? | AWS re:Post

profile picture
answered 2 months ago
  • Numbers 2 and 4 sound great - but I can't find any documentation that indicates I can route the requests to different Lambda functions. Is this a hallucination?

0

Hello.

I don't think options 2 and 4 in the AWS re:Post answer will do what you want.
The mapping template in option 2 is a setting that defines how only the necessary information from the request body is passed to Lambda, etc.
This setting does not include any setting to branch the destination Lambda.
https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-override-request-response-parameters.html

Option 4 Model Schema validation is a function that checks the API request body, request headers, and URI on the API Gateway.
So I don't think this setting can route Lambda either.
https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-method-request-validation.html

By the way, the custom authorizer in option 3 is also a setting used for authentication, etc., so I don't think there is a setting to route Lambda.
https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-use-lambda-authorizer.html

I think option 1 is a feasible configuration because the request body can be determined and processed by Lambda behind the API Gateway.

profile picture
EXPERT
answered 2 months ago
0

API Gateway does not have the ability to do dynamic routing based on request content. You have a few options:

  1. Use a Lambda router. This is easy to implement, but will adds additional cost as the router functions needs to run for the duration of the backend function.
  2. Use a CloudFront distribution with a Lambda@Edge in front of your API that will use the content to route the requests to different routes in the backend.
profile pictureAWS
EXPERT
answered 2 months 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