Vishal Jamdade
Vishal Jamdade

Reputation: 302

AWS gateway many API routes to single lambda integration each endpoint mapping to different functions of Lambda (java)

I want to create many routes in AWS API gateway and want to pass the request to AWS lambda function. Each route should pass the request to different method in Lambda function. But right now I only see that we can define only one handler for lambda and that is the only function gets invoked when getting called. Is there any best way we can achieve this with sample code ?

Upvotes: 0

Views: 4414

Answers (1)

s.hesse
s.hesse

Reputation: 2060

You've already recognized it: The problem is that you define the handler of a Lambda function in the Lambda function definition itself. You don't define the handler as part of your API Gateway setup. In the API Gateway setup you only define which Lambda function you'd like to use. Therefore, you can not simply "reuse" a Lambda function and just point to a different handler function. But there are ways around it.

Handle it within your Lambda function

In order to avoid setting up multiple Lambda functions (see other approach below), you can handle the different situations within your Lambda function's code. For example, you still map your API Gateway endpoints to the same Lambda function:

API Gateway
GET /endpoint -> My Lambda function
POST /endpoint -> My Lambda function
or: ANY /endpoint -> My Lambda function

Then, in your Lambda function you can inspect the HTTP request's method. Based on the HTTP method, you can call different methods in your Java code. I suggest having a look at aws-lambda-java-libs on GitHub and the class APIGatewayProxyRequestEvent for this, because it already provides the correct classes for HTTP events. (See this class from this repository aws-lambda-boilerplate as an example how to use the classes) Also, looking at aws-serverless-java-container makes sense as well as they already provide similar integrations there.

Use multiple Lambda functions

As an alternative, you can also reuse the artifact of your Lambda function (i.e. the JAR file) in a different Lambda function. This means, you define two Lambda functions using the same artifact but you provide a different handler function. You can follow this approach:

  1. Create a JAR file that contains two Java handler functions, i.e. implement RequestStream or RequestStreamHandler of the aws-lambda-java-core dependency.
  2. Create two Lambda functions. Both can use the same JAR file, you don't need to create two JAR files.
  3. For each Lambda function, define a different handler function using a full class reference, i.e. package + class name.
  4. Create an API Gateway endpoint pointing to your first Lambda function.
  5. Repeat step 4. doing the same for your second Lambda function.

You can look at this example repository (the aws-lambda-java-starter folder contains an example with two Java Lambda functions) how it works when using CloudFormation + Serverless Application Model (SAM). If you deploy the stack, you'll see the AWS Lambda + API Gateway setup as described above:

API Gateway
`/one` -> `JavaHandlerOne`
`/two` -> `JavaHandlerTwo`

You just need to adjust it to your needs.

Note: using a CloudFormation stack or SAM isn't required in this case but I can highly recommend using it to simplify the management of your Lambda functions, especially if you're planning to reuse the code artifact.

Upvotes: 5

Related Questions