I have created simple Lambda function and upload this to 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.
Here is my InvokeLambda class which is a separate project.
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?