Search in sources :

Example 1 with AWSLambda

use of com.amazonaws.services.lambda.AWSLambda 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 AWSLambda

use of com.amazonaws.services.lambda.AWSLambda in project aws-xray-sdk-java by aws.

the class TracingHandlerTest method testLambdaInvokeSubsegmentContainsFunctionName.

@Test
public void testLambdaInvokeSubsegmentContainsFunctionName() {
    // Setup test
    AWSLambda lambda = AWSLambdaClientBuilder.standard().withRequestHandlers(new TracingHandler()).withRegion(Regions.US_EAST_1).withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials("fake", "fake"))).build();
    // Lambda returns "null" on successful fn. with no return value
    mockHttpClient(lambda, "null");
    // Test logic
    Segment segment = AWSXRay.beginSegment("test");
    InvokeRequest request = new InvokeRequest();
    request.setFunctionName("testFunctionName");
    InvokeResult r = lambda.invoke(request);
    System.out.println(r.getStatusCode());
    System.out.println(r);
    Assert.assertEquals(1, segment.getSubsegments().size());
    Assert.assertEquals("Invoke", segment.getSubsegments().get(0).getAws().get("operation"));
    System.out.println(segment.getSubsegments().get(0).getAws());
    Assert.assertEquals("testFunctionName", segment.getSubsegments().get(0).getAws().get("function_name"));
}
Also used : AWSLambda(com.amazonaws.services.lambda.AWSLambda) AWSStaticCredentialsProvider(com.amazonaws.auth.AWSStaticCredentialsProvider) InvokeRequest(com.amazonaws.services.lambda.model.InvokeRequest) InvokeResult(com.amazonaws.services.lambda.model.InvokeResult) BasicAWSCredentials(com.amazonaws.auth.BasicAWSCredentials) Segment(com.amazonaws.xray.entities.Segment) Test(org.junit.Test)

Example 3 with AWSLambda

use of com.amazonaws.services.lambda.AWSLambda 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

AWSLambda (com.amazonaws.services.lambda.AWSLambda)3 ProfileCredentialsProvider (com.amazonaws.auth.profile.ProfileCredentialsProvider)2 InvokeRequest (com.amazonaws.services.lambda.model.InvokeRequest)2 InvokeResult (com.amazonaws.services.lambda.model.InvokeResult)2 ServiceException (com.amazonaws.services.lambda.model.ServiceException)2 AWSStaticCredentialsProvider (com.amazonaws.auth.AWSStaticCredentialsProvider)1 BasicAWSCredentials (com.amazonaws.auth.BasicAWSCredentials)1 FunctionConfiguration (com.amazonaws.services.lambda.model.FunctionConfiguration)1 ListFunctionsResult (com.amazonaws.services.lambda.model.ListFunctionsResult)1 Segment (com.amazonaws.xray.entities.Segment)1 Iterator (java.util.Iterator)1 Test (org.junit.Test)1