如何从Java代码调用AWS Lambda函数/处理程序 [英] How to invoke the AWS lambda function / handler from Java code
问题描述
我是AWS lambda的新手,我已经创建了带有处理程序的lambda函数
I am new to AWS lambda I have created a lambda function with handler
example.Orders::orderHandler
这是自定义处理程序,现在我想从我的Java程序中调用此程序.
And this is the custom handler, now I want to invoke this from my Java program how do I need to this.
推荐答案
该类中的2种方法应该可以为您提供帮助.一种是用于需要传递有效载荷的情况,另一种是用于有效载荷为空的情况.
The 2 methods in this class should be able to help you. One is for if you need to pass in a payload, the other if the payload is null.
但是,您需要记住一件事:函数名称可能与处理程序不同(后者在此为example.Orders::orderHandler
).调用处理程序功能时,将使用不处理程序名称.
However, you need to bear one thing in mind: the function name may not be the same as the handler (the latter here is example.Orders::orderHandler
). The handler name is not used when invoking its function.
因此,如果您有一个具有函数名称'myFunction'的函数,并且在幕后调用您的example.Orders::orderHandler
handler ,那么您将通过此函数下面的运行方法.
So, if you have a function with the function name 'myFunction' that behind the scenes call your example.Orders::orderHandler
handler, then this is what you would pass into the run methods below.
import com.amazonaws.regions.Regions;
import com.amazonaws.services.lambda.AWSLambdaAsyncClient;
import com.amazonaws.services.lambda.model.InvokeRequest;
import com.amazonaws.services.lambda.model.InvokeResult;
class LambdaInvoker {
public void runWithoutPayload(String region, String functionName) {
runWithPayload(region, functionName, null);
}
public void runWithPayload(String region, String functionName, String payload) {
AWSLambdaAsyncClient client = new AWSLambdaAsyncClient();
client.withRegion(Regions.fromName(region));
InvokeRequest request = new InvokeRequest();
request.withFunctionName(functionName).withPayload(payload);
InvokeResult invoke = client.invoke(request);
System.out.println("Result invoking " + functionName + ": " + invoke);
}
}
这篇关于如何从Java代码调用AWS Lambda函数/处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!