Search in sources :

Example 1 with ServiceException

use of com.amazonaws.services.lambda.model.ServiceException in project aws-doc-sdk-examples by awsdocs.

the class LambdaInvokeFunction method main.

public static void main(String[] args) {
    /*
        Function names appear as arn:aws:lambda:us-west-2:335556330391:function:HelloFunction
        you can retrieve the value by looking at the function in the AWS Console
         */
    if (args.length < 1) {
        System.out.println("Please specify a function name");
        System.exit(1);
    }
    // snippet-start:[lambda.java1.invoke.main]
    String functionName = args[0];
    InvokeRequest invokeRequest = new InvokeRequest().withFunctionName(functionName).withPayload("{\n" + " \"Hello \": \"Paris\",\n" + " \"countryCode\": \"FR\"\n" + "}");
    InvokeResult invokeResult = null;
    try {
        AWSLambda awsLambda = AWSLambdaClientBuilder.standard().withCredentials(new ProfileCredentialsProvider()).withRegion(Regions.US_WEST_2).build();
        invokeResult = awsLambda.invoke(invokeRequest);
        String ans = new String(invokeResult.getPayload().array(), StandardCharsets.UTF_8);
        // write out the return value
        System.out.println(ans);
    } catch (ServiceException e) {
        System.out.println(e);
    }
    System.out.println(invokeResult.getStatusCode());
// snippet-end:[lambda.java1.invoke.main]
}
Also used : AWSLambda(com.amazonaws.services.lambda.AWSLambda) ServiceException(com.amazonaws.services.lambda.model.ServiceException) ProfileCredentialsProvider(com.amazonaws.auth.profile.ProfileCredentialsProvider) InvokeRequest(com.amazonaws.services.lambda.model.InvokeRequest) InvokeResult(com.amazonaws.services.lambda.model.InvokeResult)

Example 2 with ServiceException

use of com.amazonaws.services.lambda.model.ServiceException in project aws-doc-sdk-examples by awsdocs.

the class ListFunctions method main.

public static void main(String[] args) {
    // snippet-start:[lambda.java1.list.main]
    ListFunctionsResult functionResult = null;
    try {
        AWSLambda awsLambda = AWSLambdaClientBuilder.standard().withCredentials(new ProfileCredentialsProvider()).withRegion(Regions.US_WEST_2).build();
        functionResult = awsLambda.listFunctions();
        List<FunctionConfiguration> list = functionResult.getFunctions();
        for (Iterator iter = list.iterator(); iter.hasNext(); ) {
            FunctionConfiguration config = (FunctionConfiguration) iter.next();
            System.out.println("The function name is " + config.getFunctionName());
        }
    } catch (ServiceException e) {
        System.out.println(e);
    }
// snippet-end:[lambda.java1.list.main]
}
Also used : ListFunctionsResult(com.amazonaws.services.lambda.model.ListFunctionsResult) AWSLambda(com.amazonaws.services.lambda.AWSLambda) ServiceException(com.amazonaws.services.lambda.model.ServiceException) FunctionConfiguration(com.amazonaws.services.lambda.model.FunctionConfiguration) Iterator(java.util.Iterator) ProfileCredentialsProvider(com.amazonaws.auth.profile.ProfileCredentialsProvider)

Aggregations

ProfileCredentialsProvider (com.amazonaws.auth.profile.ProfileCredentialsProvider)2 AWSLambda (com.amazonaws.services.lambda.AWSLambda)2 ServiceException (com.amazonaws.services.lambda.model.ServiceException)2 FunctionConfiguration (com.amazonaws.services.lambda.model.FunctionConfiguration)1 InvokeRequest (com.amazonaws.services.lambda.model.InvokeRequest)1 InvokeResult (com.amazonaws.services.lambda.model.InvokeResult)1 ListFunctionsResult (com.amazonaws.services.lambda.model.ListFunctionsResult)1 Iterator (java.util.Iterator)1