Example usage for com.amazonaws.services.lambda.model InvokeRequest setPayload

List of usage examples for com.amazonaws.services.lambda.model InvokeRequest setPayload

Introduction

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

Prototype

public void setPayload(String payload) 

Source Link

Document

The JSON that you want to provide to your Lambda function as input.

Usage

From source file:jp.classmethod.aws.gradle.lambda.AWSLambdaInvokeTask.java

License:Apache License

private void setupPayload(InvokeRequest request) throws IOException {
    Object payload = getPayload();
    String str;/*from  w  w  w. ja v a  2  s  .  c  o  m*/
    if (payload instanceof ByteBuffer) {
        request.setPayload((ByteBuffer) payload);
        return;
    }
    if (payload != null) {
        if (payload instanceof File) {
            File file = (File) payload;
            str = Files.toString(file, Charsets.UTF_8);
        } else if (payload instanceof Closure) {
            Closure<?> closure = (Closure<?>) payload;
            str = closure.call().toString();
        } else if (payload instanceof String) {
            str = (String) payload;
        } else {
            str = payload.toString();
        }
        request.setPayload(str);
    }
}

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

License:Open Source License

/**
 * Executes a lambda function and returns the result of the execution.
 *///from w  w w.  ja v a 2  s.c  om
@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;
}

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

License:Open Source License

/**
 * Executes a lambda function and returns the result of the execution.
 *//*from  w w w. j  a v a 2  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.RequestResponse);
        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 and process the results
        InvokeResult result = awsLambda.invoke(invokeRequest);

        // Convert the returned result
        ByteBuffer resultPayload = result.getPayload();
        String resultJson = new String(resultPayload.array(), "UTF-8");
        Map<String, Object> resultMap = Jackson.fromJsonString(resultJson, Map.class);

        return tagUtils.convertToCfData(resultMap);

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

}

From source file:org.xmlsh.aws.gradle.lambda.AWSLambdaInvokeTask.java

License:BSD License

private void setupPayload(InvokeRequest request) throws IOException {
    Object payload = getPayload();
    String str;//from  w  ww .  j  a va 2 s  . c  om
    if (payload instanceof ByteBuffer) {
        request.setPayload((ByteBuffer) payload);
        return;
    }
    if (payload instanceof File) {
        File file = (File) payload;
        str = Files.toString(file, Charsets.UTF_8);
    } else if (payload instanceof Closure) {
        Closure<?> closure = (Closure<?>) payload;
        str = closure.call().toString();
    } else {
        str = payload.toString();
    }
    request.setPayload(str);
}