Example usage for com.amazonaws.services.lambda.model InvocationType Event

List of usage examples for com.amazonaws.services.lambda.model InvocationType Event

Introduction

In this page you can find the example usage for com.amazonaws.services.lambda.model InvocationType Event.

Prototype

InvocationType Event

To view the source code for com.amazonaws.services.lambda.model InvocationType Event.

Click Source Link

Usage

From source file:org.alanwilliamson.amazon.lambda.LambdaAsyncExecute.java

License:Open Source License

/**
 * Executes a lambda function and returns the result of the execution.
 *//*www  .j  a v  a2  s .c o  m*/
@Override
public cfData execute(cfSession _session, cfArgStructData argStruct) throws cfmRunTimeException {

    AmazonKey amazonKey = getAmazonKey(_session, argStruct);

    // Arguments to extract
    String payload = getNamedStringParam(argStruct, "payload", null);
    String functionName = getNamedStringParam(argStruct, "function", null);
    String qualifier = getNamedStringParam(argStruct, "qualifier", null);

    try {

        // Construct the Lambda Client
        InvokeRequest invokeRequest = new InvokeRequest();
        invokeRequest.setInvocationType(InvocationType.Event);
        invokeRequest.setLogType(LogType.Tail);
        invokeRequest.setFunctionName(functionName);
        invokeRequest.setPayload(payload);
        if (qualifier != null) {
            invokeRequest.setQualifier(qualifier);
        }

        // Lambda client must be created with credentials
        BasicAWSCredentials awsCreds = new BasicAWSCredentials(amazonKey.getKey(), amazonKey.getSecret());
        AWSLambda awsLambda = AWSLambdaClientBuilder.standard()
                .withRegion(amazonKey.getAmazonRegion().toAWSRegion().getName())
                .withCredentials(new AWSStaticCredentialsProvider(awsCreds)).build();

        // Execute
        awsLambda.invoke(invokeRequest);

    } catch (Exception e) {
        throwException(_session, "AmazonLambdaAsyncExecute: " + e.getMessage());
        return cfBooleanData.FALSE;
    }

    return cfBooleanData.TRUE;
}