简体   繁体   English

将参数传递给我的 java AWS Lambda 函数

[英]Passing parameters to my java AWS Lambda function

Just started with using AWS Lambda's.刚开始使用 AWS Lambda。 I'm writing my functions in Java.我正在用 Java 编写我的函数。 I'd like to know if you can pass parameters to an AWS Lambda through the API Gateway?我想知道您是否可以通过 API Gateway 将参数传递给 AWS Lambda? 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.我的 lambda 函数基本上调用一个 web 服务,它返回 JSON,从 JSON 创建 POJO,然后是我上传到 S3 的 CSV 文件。 Now this webservice you could pass productId if you wanted to, if you don't it just returns all products.现在这个网络服务,如果你愿意,你可以传递 productId,如果你不这样做,它只会返回所有产品。

This would return the product with id of 123456 www.likssmark.com/test/api/getOrders?productId=123456这将返回 ID 为 123456 的产品www.likssmark.com/test/api/getOrders?productId=123456

This would return all orders as JSON payload: www.likssmark.com/test/api/getOrders这会将所有订单作为 JSON 负载返回: www.likssmark.com/test/api/getOrders

How do I pass productId into my java lambda?如何将 productId 传递到我的 java lambda 中? The lambda is triggered via cloud watch on a schedule - I've been testing it using Postman. lambda 是通过云监视按计划触发的 - 我一直在使用 Postman 对其进行测试。

Hope this makes sense?希望这是有道理的?

Many thanks for any advice.非常感谢您的任何建议。

There are multiple ways to integrate lambda within an API gateway.有多种方法可以在 API 网关中集成 lambda。 For starters I would create a simple HTTP API with either a default route or a specific route.对于初学者,我会创建一个简单的 HTTP API,带有默认路由或特定路由。 Attach a lambda integration to the route.将 lambda 集成附加到路由。

This should proxy the http request to your lambda function.这应该将 http 请求代理到您的 lambda 函数。 You lambda handler will receive an event which contains information about the request as path, cookies, ... and also your query parameters.您的 lambda 处理程序将收到一个事件,其中包含有关请求的信息,如路径、cookie 等,以及您的查询参数。 See the documentation for details on the passes json ( https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-develop-integrations-lambda.html ).有关 pass 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.要确定谁实际调用了该函数(cloudwatch、api 网关),只需在解析/读取它之前测试某些字段的事件内容,以确保您做出适当的响应。

If you only need to use Cloudwatch, just pass a JSON string to your Lambda:如果您只需要使用 Cloudwatch,只需将 JSON 字符串传递给您的 Lambda:

在此处输入图片说明

In your Lambda you can then pull out the data:在您的 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();

This pulls the productId out of the InputStream.这会从 InputStream 中提取productId

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.如果您同时需要 CloudWatch 事件API Gateway 集成,您可以使用两个不同的 Lambda,或者根据 @f7o 提出的建议,内省 API 网关调用的传入流。 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:来自 API Gateway 的输入将在输入 JSON 中包含一个可选参数:

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

that you can then get your productId from.然后您可以从中获取您的productId

You can use AWS API-Gateway to pass parameters to your AWS lambda service.您可以使用 AWS API-Gateway 将参数传递给您的 AWS lambda 服务。
This AWS documentation describes it:此 AWS 文档对其进行了描述:
https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-create-api-as-simple-proxy-for-lambda.html 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.在我的德语博客中,我写了一篇关于如何使用 Java 和 Spring Boot 实现 AWS lambda 服务的文章。 Here I am also passing parameters over API Gateway to AWS lambda service:在这里,我还通过 API Gateway 将参数传递给 AWS lambda 服务:
https://agile-coding.blogspot.com/2020/09/aws-lambda-services-mit-spring-boot.html https://agile-coding.blogspot.com/2020/09/aws-lambda-services-mit-spring-boot.html

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

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