简体   繁体   English

通过 API Gateway 运行 Java Lambda

[英]Running Java Lambda through API Gateway in

I am trying to make a local Java program run in AWS Lambda and make it such that it can be called with a HTTP request.我正在尝试在 AWS Lambda 中运行本地 Java 程序,并使其可以通过 HTTP 请求调用。 All I need is just to be able to duplicate the functionality of running java locally from the command line through HTTP in AWS so other people in the company can run the code by just sending a HTTP request in Postman(for now, next step is a web form that just makes the request) instead of downloading the jar and launching the Java command line.我所需要的只是能够通过 AWS 中的 HTTP 从命令行复制本地运行 java 的功能,这样公司中的其他人就可以通过在 Postman 中发送 HTTP 请求来运行代码(现在,下一步是仅发出请求的 Web 表单),而不是下载 jar 并启动 Java 命令行。

I went through the hello world tutorial in the Amazon website and was able to adapt my code, and run it successfully using the test function in the AWS Lambda control panel.我浏览了亚马逊网站上的 hello world 教程,能够调整我的代码,并使用 AWS Lambda 控制面板中的测试功能成功运行它。 I am also able to see the logs in cloudwatch that it ran and also observe the results.我还可以在 cloudwatch 中查看它运行的日志并观察结果。 So it all works from the Lambda control panel test function.所以这一切都可以通过 Lambda 控制面板测试功能运行。

So instead of command line arguments, I'm giving the arguments in JSON format as follows:因此,我以 JSON 格式提供参数,而不是命令行参数,如下所示:

{
  "environment": "dev",
  "username": "Test",
  "password": "Test22",
  "storeId": "TESTMA0001",
  "data": "a,b,c,d"
}

And this works quite well when invoking the lambda from the test function.这在从测试函数调用 lambda 时非常有效。

However I want to be able to enter this in the body of a HTTP request and have my lambda run so I added an api gateway through the gui in the aws lambda panel, chose HTTP API kind and default options.但是,我希望能够在 HTTP 请求的正文中输入它并运行我的 lambda,所以我通过 aws lambda 面板中的 gui 添加了一个 api 网关,选择了 HTTP API 种类和默认选项。

Then I send a HTTP GET request to the api endpoint with the body being the same input I used in the testing panel, but whenever I run it, I get internal server error.然后我向 api 端点发送一个 HTTP GET 请求,主体与我在测试面板中使用的输入相同,但是每当我运行它时,我都会收到内部服务器错误。 I turned on access logs for the gateway api, and I get the following, my lambda is not being launched by the api since there is no lambda log being written when I use the API, it gets written when I launch it from the AWS lambda web panel.我打开了网关 api 的访问日志,我得到以下信息,我的 lambda 没有被 api 启动,因为在我使用 API 时没有写入 lambda 日志,当我从 AWS lambda 启动它时写入它网页面板。

{
    "requestId": "KByVuheeoAMEPLA=",
    "ip": "",
    "requestTime": "27/Mar/2020:02:25:40 +0000",
    "httpMethod": "GET",
    "routeKey": "$default",
    "status": "500",
    "protocol": "HTTP/1.1",
    "responseLength": "35"
}

My handleRequest function takes a string, string map as input and returns a string as output:我的 handleRequest 函数接受一个字符串、字符串映射作为输入并返回一个字符串作为输出:

public class StoreCategoryImporter implements RequestHandler<Map<String,String>, String> {
    @Override
    public String handleRequest(Map<String,String> event, Context context)

I don't even use the context object but it was there in the tutorial so it remained.我什至不使用上下文对象,但它在教程中存在,所以它仍然存在。

I googled for hours and I have not been able to find a solution, any help would be appreciated.我用谷歌搜索了几个小时,但找不到解决方案,任何帮助将不胜感激。 I find most AWS tutorials to skip over some crucial details or they don't have it for POJO developers and use js which I don't understand.我发现大多数 AWS 教程都跳过了一些关键细节,或者他们没有为 POJO 开发人员提供并使用我不理解的 js。

Thank you in advance.先感谢您。

To simulate API Gateway event for tests in your lambda you, the lambda console has some pre-set values.为了在您的 lambda 中模拟 API 网关事件以进行测试,lambda 控制台具有一些预设值。 In your lambda you go to Configure test event and choose the event you want to simulate, eg:在您的 lambda 中,您转到Configure test event并选择要模拟的事件,例如:

在此处输入图片说明

Alternatively, you can print out real event in your lambda to CloudWatch logs, and use that for tests.或者,您可以将 lambda 中的真实事件打印到 CloudWatch 日志,并将其用于测试。

I have solved this problem by more googling and finding a closer solution.我通过更多的谷歌搜索和找到更接近的解决方案解决了这个问题。 Forget about the AWS tutorial, you need to override the handleRequest(APIGatewayProxyRequestEvent, Context) method, not the one that just takes in a Map(String,String) event as described in the hello world tutorial if you want to be able to trigger your lambda through a HTTP request.忘记 AWS 教程,您需要覆盖 handleRequest(APIGatewayProxyRequestEvent, Context) 方法,而不是仅接收 Map(String,String) 事件的方法,如 hello world 教程中所述,如果您希望能够触发您lambda 通过 HTTP 请求。

Also, the "configure test event" in the AWS GUI is useless in this case.此外,在这种情况下,AWS GUI 中的“配置测试事件”是无用的。 It cannot parse JSON that isn't a primitive.它无法解析不是原始类型的 JSON。 Probably the CLI would work better.可能 CLI 会更好地工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM