Попробуйте использовать этот класс.
import com.amazonaws.ClientConfiguration;
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.InvocationType;
import com.amazonaws.services.lambda.model.InvokeRequest;
import com.amazonaws.services.lambda.model.InvokeResult;
import java.nio.charset.Charset;
public class AWSLambdaCaller {
private String accessKey;
private String secretKey;
private String region;
private int maxConnections;
private String functionName;
public AWSLambdaCaller() {
}
/* setter methods */
public void setAccessKey(String pAccessKey) {
accessKey = pAccessKey;
}
public void setSecretKey(String pSecretKey) {
secretKey = pSecretKey;
}
public void setRegion(String pRegion) {
region = pRegion;
}
public void setMaxConnections(int pMaxConnections) {
maxConnections = pMaxConnections;
}
public void setFunctionName(String pFunctionName) {
functionName = pFunctionName;
}
/* getter methods */
public String getAccessKey () {
return accessKey;
}
public String getSecretKey () {
return secretKey;
}
public String getRegion () {
return region;
}
public int getMaxConnections() {
return maxConnections;
}
public String getFunctionName () {
return functionName;
}
public String call() {
AWSCredentials awsCredentials = new BasicAWSCredentials(accessKey, secretKey);
ClientConfiguration clientConfiguration = new ClientConfiguration();
clientConfiguration.setMaxConnections(maxConnections);
AWSLambdaClient awsLambdaClient = new AWSLambdaClient(awsCredentials, clientConfiguration);
awsLambdaClient.setRegion(Region.getRegion(Regions.fromName(region)));
InvokeRequest invokeRequest = new InvokeRequest();
invokeRequest.setFunctionName(functionName);
invokeRequest.setInvocationType(InvocationType.RequestResponse);
// invokeRequest.setPayload(jsonBody.toString());
InvokeResult invokeResult = awsLambdaClient.invoke(invokeRequest);
String str = new String(invokeResult.getPayload().array(), Charset.forName("UTF-8"));
return str;
}
}
main () будет выглядеть следующим образом.
AWSLambdaCaller awsLambdaCaller = new AWSLambdaCaller();
awsLambdaCaller.setAccessKey("Your access key");
awsLambdaCaller.setSecretKey("Your secret key");
awsLambdaCaller.setRegion("region");
awsLambdaCaller.setMaxConnections(1100);
awsLambdaCaller.setFunctionName("Your function name");
System.out.println("Calling AWS Lambda function...");
System.out.println("AWS>> " + awsLambdaCaller.call());
Обратите внимание, что приведенный выше код поддерживает только лямбда-функции без параметров.Чтобы передать параметры, отредактируйте invokeRequest.