可以使用Java SDK向AWS Lambda发起Invoke请求时,包括头部和查询参数吗?

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

Is it possible to include headers and query parameters with the Invoke request to AWS Lambda using the Java SDK

问题

我正在通过使用AWS Java SDK中的Invoke直接调用Lambda,来替换位于Lambda前面的AWS API Gateway。

InvokeRequest request = new InvokeRequest();
try {
    request.putCustomQueryParameter("city", "Seattle");
    request.withFunctionName(functionName).withPayload(ByteBuffer.wrap(IOUtils.toByteArray(content)));
} catch (IOException e1) {
    e1.printStackTrace();
}

是否有办法在请求中传递头部或查询参数?我尝试了上述代码,但我只能发送有效载荷。

英文:

I'm replacing the AWS API Gateway in front of my Lambda by calling it directly from the AWS Java SDK using Invoke

InvokeRequest request = new InvokeRequest();
try {
    request.putCustomQueryParameter("city", "Seattle");
    request.withFunctionName(functionName).withPayload(ByteBuffer.wrap(IOUtils.toByteArray(content)));
} catch (IOException e1) {
	e1.printStackTrace();
}

Is there a way to pass in headers or query parameters along with the request? I tried the code above but all I'm able to send is the payload.

答案1

得分: 1

快速回答: putCustomRequestHeader 只能用于向发送到 AWS Lambda 服务的 HTTP 请求添加请求头。这些请求头不会转发到 AWS Lambda 函数。

详细回答: 我认为您在这里混淆了两个不同的概念,存在术语上的误解。如果您调用 AWS Lambda 函数,您将向 AWS Lambda 服务发送一个 HTTP 请求,然后该服务将执行实际的 Lambda 函数调用(并将有效载荷数据转发给函数)。例如,AWS Lambda 服务会查看是否有空闲函数并使用它,否则会启动您的函数的新实例并使用该实例。

a) 直接调用 AWS Lambda

如果您想使用 AWS SDK 直接调用 AWS Lambda 函数,您可以在您的代码内部执行此操作,例如 Java 程序:

Java 程序 --> 调用 AWS Lambda 函数

这意味着您的代码会直接调用 Lambda 函数。在这种情况下,您只能向函数提供有效载荷。如果您想要添加任何“额外”的数据,只能扩展有效载荷数据。

b) 间接调用 AWS Lambda

然后还有一种“间接”调用 Lambda 函数的方式,例如一个位于 API Gateway 后面的函数。这样的 Lambda 函数可以接收 HTTP 标头和查询参数,因为 API Gateway 接收到 HTTP 请求并将 HTTP 事件转发给 AWS Lambda 函数:

Java 程序 --> 发送 HTTP 请求 --> API Gateway 接收 HTTP --> API Gateway 使用 HTTP 事件数据调用 AWS Lambda 函数

在 API Gateway 调用 AWS Lambda 函数的最后一步中,API Gateway 也只会“简单”地调用 Lambda 函数。但它会将所有之前接收到的 HTTP 数据(在此之前)作为 Lambda 函数的有效载荷数据。这就是为什么在这种情况下您可以访问 HTTP 标头和查询参数。

解决您的问题

如果您确实需要提供 HTTP 标头或查询参数,那么请在您的函数前面放置一个 API Gateway。然而,如果您只是想从您的代码内部调用函数,那么只需扩展您发送的有效载荷数据,并添加您想要提供的数据。

英文:

Quick answer: the putCustomRequestHeader can only be used to add request headers to the HTTP request which is send to the AWS Lambda Service. They'll not be forwarded to the AWS Lambda Function.

Long answer: I believe you are mixing up two different things here and there's a misunderstanding of the terminology. If you invoke an AWS Lambda Function, you'll send an HTTP request to the AWS Lambda Service which will then perform the actual invocation of a Lambda function (and it forwards your payload data to the function). For example, the AWS Lambda Service looks if there is an idle function and uses it, otherwise it starts a new instance of your function and uses that.

a) Directly Invoke AWS Lambda

If you want to invoke an AWS Lambda Function using the AWS SDK, you do this from within your code, e.g. a Java program:

Java Program --> Invoke AWS Lambda Function

This means, your code is directly invoking a Lambda function. In such a case, you can only provide a payload to the function. If you want to add any "additional" data, you can only extend the payload data.

b) Indirectly Invoke AWS Lambda

Then there is a way to "indirectly" invoke a Lambda function, e.g. a function that sits behind an API Gateway. Such a Lambda function can receive HTTP headers and query parameters because the API Gateway is receiving an HTTP request and forwarding an HTTP event to the AWS Lambda function:

Java Program --> Send HTTP request --> API Gateway receives HTTP --> API Gateway invokes AWS Lambda Function with HTTP event data

In the last step where the API Gateway invokes the AWS Lambda Function, the API Gateway will also "just" invoke the Lambda function. But it provides all the HTTP data (that it received before) as the payload data for the Lambda function. That's why you have access to HTTP headers and query parameters in this scenario.

Solution to your question

If you really need to provide HTTP headers or query parameters, then put an API Gateway in front of your function. However, if you just want to invoke your function from within your code, then simply extend the payload data that you're sending and add the data you'd like to provide.

huangapple
  • 本文由 发表于 2020年9月9日 07:08:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/63802674.html
匿名

发表评论

匿名网友

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

确定