AWS Lambda:如何从简单的 Java 类调用 lambda 函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34720261/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
AWS Lambda : How to call lambda function from simple java class
提问by Unknown
I have created simple Lambda function and uploadthis to AWS Lambda.
我创建了简单的 Lambda 函数并将其上传到 AWS Lambda。
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
public class Hello implements RequestHandler<String, String> {
@Override
public String handleRequest(String input, Context context) {
String output = "Bonjour, " + input + "!";
return output;
}
}
}
I want to invoke this Lambda function from some other project using java class. I am using aws-java-sdk-lambda-1.10.22
to call the function. But I am not able to succeed in that.
我想使用 java 类从其他项目调用这个 Lambda 函数。我正在使用aws-java-sdk-lambda-1.10.22
调用该函数。但我无法在这方面取得成功。
Here is my InvokeLambda class which is a separate project.
这是我的 InvokeLambda 类,它是一个单独的项目。
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.regions.Region;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.lambda.AWSLambdaClient;
import com.amazonaws.services.lambda.model.InvokeRequest;
public class InvokeLambda {
private static final Log logger = LogFactory.getLog(InvokeLambda.class);
private static final String awsAccessKeyId = "XXXXXX";
private static final String awsSecretAccessKey = "YYYY";
private static final String regionName = "us-west-2";
private static final String functionName = "Hello";
private static Region region;
private static AWSCredentials credentials;
private static AWSLambdaClient lambdaClient;
/**
* The entry point into the AWS lambda function.
*/
public static void main(String... args) {
credentials = new BasicAWSCredentials(awsAccessKeyId,
awsSecretAccessKey);
lambdaClient = (credentials == null) ? new AWSLambdaClient()
: new AWSLambdaClient(credentials);
//lambdaClient.configureRegion(Regions.US_WEST_2);
region = Region.getRegion(Regions.fromName(regionName));
lambdaClient.setRegion(region);
try {
InvokeRequest invokeRequest = new InvokeRequest();
invokeRequest.setFunctionName(functionName);
invokeRequest.setPayload("\" AWS Lambda\"");
System.out.println(byteBufferToString(
lambdaClient.invoke(invokeRequest).getPayload(),
Charset.forName("UTF-8")));
} catch (Exception e) {
logger.error(e.getMessage());
// System.out.println(e.getMessage());
}
}
public static String byteBufferToString(ByteBuffer buffer, Charset charset) {
byte[] bytes;
if (buffer.hasArray()) {
bytes = buffer.array();
} else {
bytes = new byte[buffer.remaining()];
buffer.get(bytes);
}
return new String(bytes, charset);
}
}
How to call the lambda function using java?
如何使用java调用lambda函数?
采纳答案by Joe Taylor
Given the information in your comment, your client code to invoke the function is fine. The problem appears to be with the configuration of the function itself. Specifically, AWS Lambda is not able to find the handler you've specified (com.aws.HelloLambda::handleRequest
) because that doesn't match the name and package of your handler class (Hello
) and the name of your method in that class (handleRequest
).
鉴于您的评论中的信息,您调用该函数的客户端代码没问题。问题似乎出在函数本身的配置上。具体而言,AWS Lambda 无法找到您指定的处理程序 ( com.aws.HelloLambda::handleRequest
),因为它与您的处理程序类 ( Hello
)的名称和包以及该类中的方法名称( )不匹配handleRequest
。
You can update the function handler name through the AWS Console. Choose your function, then the configuration tab and then the Handler property.
您可以通过 AWS 控制台更新函数处理程序名称。选择您的功能,然后是配置选项卡,然后是 Handler 属性。
You probably want to change it from com.aws.HelloLambda::handleRequest
to Hello::handleRequest
.
您可能希望将其从 更改com.aws.HelloLambda::handleRequest
为Hello::handleRequest
。
Before testing the function from your Java client, you could test it directly through the console, this will help you ensure the function is configured correctly.
在从您的 Java 客户端测试该功能之前,您可以直接通过控制台对其进行测试,这将有助于您确保该功能配置正确。
回答by isaranchuk
Another way to invoke lambda from java code is to use LambdaInvokerFactory
and I found this approach cleaner.
You need to do the following:
从 Java 代码调用 lambda 的另一种方法是使用LambdaInvokerFactory
,我发现这种方法更简洁。您需要执行以下操作:
- Define interface representing your function and annotate method with
@LambdaFunction
- Create implementation of the above interface using
LambdaInvokerFactory
Invoke lambda function using just created proxy object (interface implementation)
More detailed example can be found here.
- 定义表示您的函数的接口并使用注释方法
@LambdaFunction
- 使用创建上述接口的实现
LambdaInvokerFactory
使用刚刚创建的代理对象(接口实现)调用 lambda 函数
可以在此处找到更详细的示例。