[英]AWS Lambda : How to call lambda function from simple java class
I have created simple Lambda function and upload this 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. 我想使用java类从其他项目调用此Lambda函数。 I am using
aws-java-sdk-lambda-1.10.22
to call the function. 我正在使用
aws-java-sdk-lambda-1.10.22
来调用该函数。 But I am not able to succeed in that. 但我无法成功。
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函数?
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
)的名称和包以及您的方法名称不匹配class( handleRequest
)。
You can update the function handler name through the AWS Console. 您可以通过AWS控制台更新功能处理程序名称。 Choose your function, then the configuration tab and then the Handler property.
选择您的函数,然后选择配置选项卡,然后选择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客户端测试函数之前,您可以直接通过控制台对其进行测试,这将有助于确保正确配置该函数。
Another way to invoke lambda from java code is to use LambdaInvokerFactory
and I found this approach cleaner. 从java代码调用lambda的另一种方法是使用
LambdaInvokerFactory
,我发现这种方法更清晰。 You need to do the following: 您需要执行以下操作:
@LambdaFunction
@LambdaFunction
定义表示函数的接口和注释方法 LambdaInvokerFactory
LambdaInvokerFactory
创建上述接口的LambdaInvokerFactory
Invoke lambda function using just created proxy object (interface implementation) 使用刚创建的代理对象调用lambda函数(接口实现)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.