将参数传递给我的Java AWS Lambda函数。

huangapple go评论87阅读模式
英文:

Passing parameters to my java AWS Lambda function

问题

刚开始使用AWS Lambda。我在Java中编写我的函数。我想知道是否可以通过API Gateway将参数传递给AWS Lambda?我的Lambda函数基本上调用一个返回JSON的Web服务,然后从JSON创建POJO,最后生成一个CSV文件,我将其上传到S3。现在,如果需要,你可以通过这个Web服务传递productId,如果不传,它将返回所有产品。

这将返回ID为123456的产品:
www.likssmark.com/test/api/getOrders?productId=123456

这将返回所有订单的JSON负载:
www.likssmark.com/test/api/getOrders

我如何将productId传递给我的Java Lambda?Lambda是通过云监控定时触发的 - 我一直在使用Postman进行测试。

希望这样说得清楚。

非常感谢任何建议。

英文:

Just started with using AWS Lambda's. I'm writing my functions in Java. I'd like to know if you can pass parameters to an AWS Lambda through the API Gateway? My lambda function basically makes a call to a webservice which returns JSON, create's POJO's from the JSON and then a CSV file which I upload to S3. Now this webservice you could pass productId if you wanted to, if you don't it just returns all products.

This would return the product with id of 123456
www.likssmark.com/test/api/getOrders?productId=123456

This would return all orders as JSON payload:
www.likssmark.com/test/api/getOrders

How do I pass productId into my java lambda? The lambda is triggered via cloud watch on a schedule - I've been testing it using Postman.

Hope this makes sense?

Many thanks for any advice.

答案1

得分: 1

有多种方法可以在 API 网关中集成 Lambda。首先,我会创建一个简单的 HTTP API,其中包含默认路由或特定路由。将 Lambda 集成附加到路由上。

这将代理 HTTP 请求到你的 Lambda 函数。你的 Lambda 处理程序将收到一个事件,其中包含有关请求的信息,如路径、cookie 等,还包括查询参数。有关传递 JSON 的详细信息,请参阅文档(https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-develop-integrations-lambda.html)。

要确定实际调用函数的是谁(CloudWatch、API 网关),只需在解析/读取事件之前测试事件的某些字段的内容,以确保你做出适当的响应。

英文:

There are multiple ways to integrate lambda within an API gateway. For starters I would create a simple HTTP API with either a default route or a specific route. Attach a lambda integration to the route.

This should proxy the http request to your lambda function. You lambda handler will receive an event which contains information about the request as path, cookies, ... and also your query parameters. See the documentation for details on the passes json (https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-develop-integrations-lambda.html).

To determine who is actually calling the function (cloudwatch, api gateway) just test the content of the event for some fields before parsing/reading it to make sure you respond appropriate.

答案2

得分: 1

如果您只需要使用CloudWatch,只需将JSON字符串传递给您的Lambda函数:

在您的Lambda函数中,您可以提取数据:

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

...

public class ProductIdLambda implements RequestStreamHandler {

    public void handleRequest(InputStream inputStream, OutputStream outputStream, Context context) throws IOException {
        LambdaLogger logger = context.getLogger();

        ObjectMapper objectMapper = new ObjectMapper();
        JsonNode rootNode = objectMapper.readTree(inputStream);

        String productId = rootNode.path("productId").asInt();

这将从InputStream中提取出productId

如果您既需要CloudWatch事件又需要API Gateway集成,您可以选择使用两个不同的Lambda函数,或者按照@f7o的建议,检查传入流以判断是否为API Gateway调用。您可以像这样处理:

String httpMethod = rootNode.path("httpMethod").asText();
if (httpMethod != null)  // 那么我们是被API Gateway调用的

来自API Gateway的输入将在输入JSON中包含一个可选参数:

"queryStringParameters": {
    "productId": "12345"
},

您可以从中获取您的productId

英文:

If you only need to use Cloudwatch, just pass a JSON string to your Lambda:

将参数传递给我的Java AWS Lambda函数。

In your Lambda you can then pull out the data:

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

...

public class ProductIdLambda implements RequestStreamHandler {

    public void handleRequest(InputStream inputStream, OutputStream outputStream, Context context) throws IOException {
        LambdaLogger logger = context.getLogger();

        ObjectMapper objectMapper = new ObjectMapper();
        JsonNode rootNode = objectMapper.readTree(inputStream);

        String productId = rootNode.path("productId").asInt();

This pulls the productId out of the InputStream.

If you need both CloudWatch events and API Gateway integration you can either have two different Lambda's or, to the suggestion @f7o made, introspect the incoming stream for an API Gateway call. You could have something like:

String httpMethod = rootNode.path("httpMethod").asText();
if( httpMethod != null )  // then we were called by API Gateway

The input from API Gateway will include an optional parameter in the input JSON:

"queryStringParameters": {
    "productId": "12345"
},

that you can then get your productId from.

答案3

得分: 1

你可以使用AWS API Gateway将参数传递给你的AWS Lambda服务。这份AWS文档对此进行了描述: https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-create-api-as-simple-proxy-for-lambda.html

在我的德语博客中,我撰写了一篇关于如何使用Java和Spring Boot实现AWS Lambda服务的文章。在这里,我还通过API Gateway将参数传递给AWS Lambda服务: https://agile-coding.blogspot.com/2020/09/aws-lambda-services-mit-spring-boot.html

英文:

You can use AWS API-Gateway to pass parameters to your AWS lambda service.<br>
This AWS documentation describes it: <br> https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-create-api-as-simple-proxy-for-lambda.html

In my German blog I wrote an article about how to implement an AWS lambda service with Java and Spring Boot. Here I am also passing parameters over API Gateway to AWS lambda service:<br>
https://agile-coding.blogspot.com/2020/09/aws-lambda-services-mit-spring-boot.html

huangapple
  • 本文由 发表于 2020年10月27日 03:50:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/64544057.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定