简体   繁体   中英

How to create/invoke AWS Lambda functions in Java?

I'm implementing a third party application to simply handle all the functionalities of AWS Lambda using AWS SDK. I'm using JetBrains IntelliJ Idea IDE for coding. Can someone explain how to create a lambda object in Java to create/invoke aws lambda functions?

I referred to documentation but it doesn't provide the steps to create a lambda object.

You don't have to create a lambda object. Lambda functions are frozen when not in use. That why global variables don't lose their data. You just have to send a invoke call, AWS will take care of the rest.

I thought the above info is good to know. Now you can find an example to invoke a lambda function in this link

Try with this class.

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() will be like this.

    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());

Note that above-explained code supports only for lambda functions with no parameters. To pass the parameters, edit invokeRequest.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM