AWS网关将许多API路由路由到单个Lambda集成,每个端点都映射到Lambda(Java)的不同功能 [英] AWS gateway many API routes to single lambda integration each endpoint mapping to different functions of Lambda (java)

查看:74
本文介绍了AWS网关将许多API路由路由到单个Lambda集成,每个端点都映射到Lambda(Java)的不同功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在AWS API网关中创建许多路由,并希望将请求传递给AWS lambda函数.每个路由都应将请求传递给Lambda函数中的不同方法.但是现在我只看到我们只能为lambda定义一个处理程序,这是在被调用时唯一被调用的函数.有什么最好的方法可以通过示例代码来实现?

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 ?

推荐答案

您已经认识到它:问题是您在Lambda函数定义本身中定义了Lambda函数的处理程序.您没有将处理程序定义为API网关设置的一部分.在API Gateway设置中,您仅定义要使用的Lambda函数.因此,您不能简单地重用"资源.Lambda函数,并指向另一个处理程序函数.但是有很多解决方法.

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.

在Lambda函数中处理它

为了避免设置多个Lambda函数(请参阅下面的其他方法),您可以在Lambda函数的代码中处理不同的情况.例如,您仍将API Gateway端点映射到相同的Lambda函数:

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

然后,您可以在Lambda函数中检查HTTP请求的方法.基于HTTP方法,您可以在Java代码中调用不同的方法.我建议看看 aws-GitHub上的lambda-java-libs 以及为此的类 APIGatewayProxyRequestEvent ,因为它已经为HTTP事件提供了正确的类.(请参见来自该存储库的此类 aws-lambda-boilerplate作为示例,介绍如何使用这些类),另外,请参见 aws-serverless-java-container 很有意义,因为它们已经在那里提供了类似的集成.

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.

使用多个Lambda函数

或者,您也可以在其他Lambda函数中重用Lambda函数的构件(即JAR文件).这意味着,您使用相同的工件定义了两个Lambda函数,但提供了不同的处理函数.您可以按照以下方法进行操作:

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. 创建一个包含两个Java处理程序功能的JAR文件,即实现您可以查看此示例存储库( aws-lambda-java-starter 文件夹包含具有两个Java Lambda函数的示例)在使用CloudFormation +无服务器应用程序模型(SAM)时如何工作.如果部署堆栈,您将看到如上所述的AWS Lambda + API Gateway设置:

    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.

    注意:在这种情况下,不需要使用CloudFormation堆栈或SAM,但我强烈建议您使用它来简化Lambda函数的管理,尤其是在您计划重用Lambda函数的情况下.代码工件.

    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.

    这篇关于AWS网关将许多API路由路由到单个Lambda集成,每个端点都映射到Lambda(Java)的不同功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆