software.amazon.awssdk.services.lambda.model.InvokeRequest Java Examples

The following examples show how to use software.amazon.awssdk.services.lambda.model.InvokeRequest. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ServiceIntegrationTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Test
public void testFunctionOperations() throws IOException {

    // Get function
    GetFunctionResponse getFunc = lambda.getFunction(r -> r.functionName(FUNCTION_NAME)).join();
    checkValid_GetFunctionResponse(getFunc);

    // Get function configuration
    GetFunctionConfigurationResponse getConfig = lambda.getFunctionConfiguration(r -> r.functionName(FUNCTION_NAME)).join();

    checkValid_GetFunctionConfigurationResponse(getConfig);

    // List functions
    ListFunctionsResponse listFunc = lambda.listFunctions(ListFunctionsRequest.builder().build()).join();
    Assert.assertFalse(listFunc.functions().isEmpty());
    for (FunctionConfiguration funcConfig : listFunc.functions()) {
        checkValid_FunctionConfiguration(funcConfig);
    }

    // Invoke the function
    InvokeResponse invokeResult = lambda.invoke(InvokeRequest.builder().functionName(FUNCTION_NAME)
            .invocationType(InvocationType.EVENT).payload(SdkBytes.fromUtf8String("{}")).build()).join();

    Assert.assertEquals(202, invokeResult.statusCode().intValue());
    Assert.assertNull(invokeResult.logResult());
    Assert.assertEquals(0, invokeResult.payload().asByteBuffer().remaining());

    invokeResult = lambda.invoke(InvokeRequest.builder().functionName(FUNCTION_NAME)
            .invocationType(InvocationType.REQUEST_RESPONSE).logType(LogType.TAIL)
            .payload(SdkBytes.fromUtf8String("{}")).build()).join();

    Assert.assertEquals(200, invokeResult.statusCode().intValue());

    System.out.println(new String(BinaryUtils.fromBase64(invokeResult.logResult()), StandardCharsets.UTF_8));

    Assert.assertEquals("\"Hello World\"", invokeResult.payload().asUtf8String());
}
 
Example #2
Source File: TracingInterceptorTest.java    From aws-xray-sdk-java with Apache License 2.0 4 votes vote down vote up
@Test
public void testLambdaInvokeSubsegmentContainsFunctionName() throws Exception {
    SdkHttpClient mockClient = mockSdkHttpClient(generateLambdaInvokeResponse(200));

    LambdaClient client = LambdaClient.builder()
            .httpClient(mockClient)
            .endpointOverride(URI.create("http://example.com"))
            .region(Region.of("us-west-42"))
            .credentialsProvider(StaticCredentialsProvider.create(
                    AwsSessionCredentials.create("key", "secret", "session")
            ))
            .overrideConfiguration(ClientOverrideConfiguration.builder()
                    .addExecutionInterceptor(new TracingInterceptor())
                    .build()
            )
            .build();


    Segment segment = AWSXRay.getCurrentSegment();

    client.invoke(InvokeRequest.builder()
            .functionName("testFunctionName")
            .build()
    );

    Assert.assertEquals(1, segment.getSubsegments().size());
    Subsegment subsegment = segment.getSubsegments().get(0);
    Map<String, Object> awsStats = subsegment.getAws();
    @SuppressWarnings("unchecked")
    Map<String, Object> httpResponseStats = (Map<String, Object>) subsegment.getHttp().get("response");

    Assert.assertEquals("Invoke", awsStats.get("operation"));
    Assert.assertEquals("testFunctionName", awsStats.get("function_name"));
    Assert.assertEquals("1111-2222-3333-4444", awsStats.get("request_id"));
    Assert.assertEquals("extended", awsStats.get("id_2"));
    Assert.assertEquals("Failure", awsStats.get("function_error"));
    Assert.assertEquals("us-west-42", awsStats.get("region"));
    Assert.assertEquals(0, awsStats.get("retries"));
    Assert.assertEquals(2L, httpResponseStats.get("content_length"));
    Assert.assertEquals(200, httpResponseStats.get("status"));
    Assert.assertEquals(false, subsegment.isInProgress());
}
 
Example #3
Source File: TracingInterceptorTest.java    From aws-xray-sdk-java with Apache License 2.0 4 votes vote down vote up
@Test
public void testAsyncLambdaInvokeSubsegmentContainsFunctionName() {
    SdkAsyncHttpClient mockClient = mockSdkAsyncHttpClient(generateLambdaInvokeResponse(200));

    LambdaAsyncClient client = LambdaAsyncClient.builder()
            .httpClient(mockClient)
            .endpointOverride(URI.create("http://example.com"))
            .region(Region.of("us-west-42"))
            .credentialsProvider(StaticCredentialsProvider.create(
                    AwsSessionCredentials.create("key", "secret", "session")
            ))
            .overrideConfiguration(ClientOverrideConfiguration.builder()
                    .addExecutionInterceptor(new TracingInterceptor())
                    .build()
            )
            .build();

    Segment segment = AWSXRay.getCurrentSegment();

    client.invoke(InvokeRequest.builder()
            .functionName("testFunctionName")
            .build()
    ).join();

    Assert.assertEquals(1, segment.getSubsegments().size());
    Subsegment subsegment = segment.getSubsegments().get(0);
    Map<String, Object> awsStats = subsegment.getAws();
    @SuppressWarnings("unchecked")
    Map<String, Object> httpResponseStats = (Map<String, Object>) subsegment.getHttp().get("response");

    Assert.assertEquals("Invoke", awsStats.get("operation"));
    Assert.assertEquals("testFunctionName", awsStats.get("function_name"));
    Assert.assertEquals("1111-2222-3333-4444", awsStats.get("request_id"));
    Assert.assertEquals("extended", awsStats.get("id_2"));
    Assert.assertEquals("Failure", awsStats.get("function_error"));
    Assert.assertEquals("us-west-42", awsStats.get("region"));
    Assert.assertEquals(0, awsStats.get("retries"));
    Assert.assertEquals(2L, httpResponseStats.get("content_length"));
    Assert.assertEquals(200, httpResponseStats.get("status"));
    Assert.assertEquals(false, subsegment.isInProgress());
}
 
Example #4
Source File: TracingInterceptorTest.java    From aws-xray-sdk-java with Apache License 2.0 4 votes vote down vote up
@Test
public void test400Exception() throws Exception {
    SdkHttpClient mockClient = mockSdkHttpClient(generateLambdaInvokeResponse(400));

    LambdaClient client = LambdaClient.builder()
            .httpClient(mockClient)
            .endpointOverride(URI.create("http://example.com"))
            .region(Region.of("us-west-42"))
            .credentialsProvider(StaticCredentialsProvider.create(
                    AwsSessionCredentials.create("key", "secret", "session")
            ))
            .overrideConfiguration(ClientOverrideConfiguration.builder()
                    .addExecutionInterceptor(new TracingInterceptor())
                    .build()
            )
            .build();


    Segment segment = AWSXRay.getCurrentSegment();

    try {
        client.invoke(InvokeRequest.builder()
                .functionName("testFunctionName")
                .build()
        );
    } catch (Exception e) {
        // ignore SDK errors
    } finally {
        Assert.assertEquals(1, segment.getSubsegments().size());
        Subsegment subsegment = segment.getSubsegments().get(0);
        Map<String, Object> awsStats = subsegment.getAws();
        @SuppressWarnings("unchecked")
        Map<String, Object> httpResponseStats = (Map<String, Object>) subsegment.getHttp().get("response");
        Cause cause = subsegment.getCause();

        Assert.assertEquals("Invoke", awsStats.get("operation"));
        Assert.assertEquals("testFunctionName", awsStats.get("function_name"));
        Assert.assertEquals("1111-2222-3333-4444", awsStats.get("request_id"));
        Assert.assertEquals("extended", awsStats.get("id_2"));
        Assert.assertEquals("us-west-42", awsStats.get("region"));
        Assert.assertEquals(0, awsStats.get("retries"));
        Assert.assertEquals(2L, httpResponseStats.get("content_length"));
        Assert.assertEquals(400, httpResponseStats.get("status"));
        Assert.assertEquals(false, subsegment.isInProgress());
        Assert.assertEquals(true, subsegment.isError());
        Assert.assertEquals(false, subsegment.isThrottle());
        Assert.assertEquals(false, subsegment.isFault());
        Assert.assertEquals(1, cause.getExceptions().size());
        Assert.assertEquals(true, cause.getExceptions().get(0).isRemote());
    }
}
 
Example #5
Source File: TracingInterceptorTest.java    From aws-xray-sdk-java with Apache License 2.0 4 votes vote down vote up
@Test
public void testAsync400Exception() {
    SdkAsyncHttpClient mockClient = mockSdkAsyncHttpClient(generateLambdaInvokeResponse(400));

    LambdaAsyncClient client = LambdaAsyncClient.builder()
            .httpClient(mockClient)
            .endpointOverride(URI.create("http://example.com"))
            .region(Region.of("us-west-42"))
            .credentialsProvider(StaticCredentialsProvider.create(
                    AwsSessionCredentials.create("key", "secret", "session")
            ))
            .overrideConfiguration(ClientOverrideConfiguration.builder()
                    .addExecutionInterceptor(new TracingInterceptor())
                    .build()
            )
            .build();

    Segment segment = AWSXRay.getCurrentSegment();

    try {
        client.invoke(InvokeRequest.builder()
                .functionName("testFunctionName")
                .build()
        ).get();
    } catch (Exception e) {
        // ignore exceptions
    } finally {
        Assert.assertEquals(1, segment.getSubsegments().size());
        Subsegment subsegment = segment.getSubsegments().get(0);
        Map<String, Object> awsStats = subsegment.getAws();
        @SuppressWarnings("unchecked")
        Map<String, Object> httpResponseStats = (Map<String, Object>) subsegment.getHttp().get("response");
        Cause cause = subsegment.getCause();

        Assert.assertEquals("Invoke", awsStats.get("operation"));
        Assert.assertEquals("testFunctionName", awsStats.get("function_name"));
        Assert.assertEquals("1111-2222-3333-4444", awsStats.get("request_id"));
        Assert.assertEquals("extended", awsStats.get("id_2"));
        Assert.assertEquals("us-west-42", awsStats.get("region"));
        Assert.assertEquals(0, awsStats.get("retries"));
        Assert.assertEquals(2L, httpResponseStats.get("content_length"));
        Assert.assertEquals(400, httpResponseStats.get("status"));
        Assert.assertEquals(false, subsegment.isInProgress());
        Assert.assertEquals(true, subsegment.isError());
        Assert.assertEquals(false, subsegment.isThrottle());
        Assert.assertEquals(false, subsegment.isFault());
        Assert.assertEquals(1, cause.getExceptions().size());
        Assert.assertEquals(true, cause.getExceptions().get(0).isRemote());
    }
}
 
Example #6
Source File: TracingInterceptorTest.java    From aws-xray-sdk-java with Apache License 2.0 4 votes vote down vote up
@Test
public void testThrottledException() throws Exception {
    SdkHttpClient mockClient = mockSdkHttpClient(generateLambdaInvokeResponse(429));

    LambdaClient client = LambdaClient.builder()
            .httpClient(mockClient)
            .endpointOverride(URI.create("http://example.com"))
            .region(Region.of("us-west-42"))
            .credentialsProvider(StaticCredentialsProvider.create(
                    AwsSessionCredentials.create("key", "secret", "session")
            ))
            .overrideConfiguration(ClientOverrideConfiguration.builder()
                    .addExecutionInterceptor(new TracingInterceptor())
                    .build()
            )
            .build();


    Segment segment = AWSXRay.getCurrentSegment();

    try {
        client.invoke(InvokeRequest.builder()
                .functionName("testFunctionName")
                .build()
        );
    } catch (Exception e) {
        // ignore SDK errors
    } finally {
        Assert.assertEquals(1, segment.getSubsegments().size());
        Subsegment subsegment = segment.getSubsegments().get(0);
        Map<String, Object> awsStats = subsegment.getAws();
        @SuppressWarnings("unchecked")
        Map<String, Object> httpResponseStats = (Map<String, Object>) subsegment.getHttp().get("response");
        Cause cause = subsegment.getCause();

        Assert.assertEquals("Invoke", awsStats.get("operation"));
        Assert.assertEquals("testFunctionName", awsStats.get("function_name"));
        Assert.assertEquals("1111-2222-3333-4444", awsStats.get("request_id"));
        Assert.assertEquals("extended", awsStats.get("id_2"));
        Assert.assertEquals("us-west-42", awsStats.get("region"));
        // throttled requests are retried
        Assert.assertEquals(3, awsStats.get("retries"));
        Assert.assertEquals(2L, httpResponseStats.get("content_length"));
        Assert.assertEquals(429, httpResponseStats.get("status"));
        Assert.assertEquals(true, subsegment.isError());
        Assert.assertEquals(true, subsegment.isThrottle());
        Assert.assertEquals(false, subsegment.isFault());
        Assert.assertEquals(1, cause.getExceptions().size());
        Assert.assertEquals(true, cause.getExceptions().get(0).isRemote());
    }
}
 
Example #7
Source File: TracingInterceptorTest.java    From aws-xray-sdk-java with Apache License 2.0 4 votes vote down vote up
@Test
public void testAsyncThrottledException() {
    SdkAsyncHttpClient mockClient = mockSdkAsyncHttpClient(generateLambdaInvokeResponse(429));

    LambdaAsyncClient client = LambdaAsyncClient.builder()
            .httpClient(mockClient)
            .endpointOverride(URI.create("http://example.com"))
            .region(Region.of("us-west-42"))
            .credentialsProvider(StaticCredentialsProvider.create(
                    AwsSessionCredentials.create("key", "secret", "session")
            ))
            .overrideConfiguration(ClientOverrideConfiguration.builder()
                    .addExecutionInterceptor(new TracingInterceptor())
                    .build()
            )
            .build();

    Segment segment = AWSXRay.getCurrentSegment();

    try {
        client.invoke(InvokeRequest.builder()
                .functionName("testFunctionName")
                .build()
        ).get();
    } catch (Exception e) {
        // ignore exceptions
    } finally {
        Assert.assertEquals(1, segment.getSubsegments().size());
        Subsegment subsegment = segment.getSubsegments().get(0);
        Map<String, Object> awsStats = subsegment.getAws();
        @SuppressWarnings("unchecked")
        Map<String, Object> httpResponseStats = (Map<String, Object>) subsegment.getHttp().get("response");
        Cause cause = subsegment.getCause();

        Assert.assertEquals("Invoke", awsStats.get("operation"));
        Assert.assertEquals("testFunctionName", awsStats.get("function_name"));
        Assert.assertEquals("1111-2222-3333-4444", awsStats.get("request_id"));
        Assert.assertEquals("extended", awsStats.get("id_2"));
        Assert.assertEquals("us-west-42", awsStats.get("region"));
        // throttled requests are retried
        Assert.assertEquals(3, awsStats.get("retries"));
        Assert.assertEquals(2L, httpResponseStats.get("content_length"));
        Assert.assertEquals(429, httpResponseStats.get("status"));
        Assert.assertEquals(true, subsegment.isError());
        Assert.assertEquals(true, subsegment.isThrottle());
        Assert.assertEquals(false, subsegment.isFault());
        Assert.assertEquals(1, cause.getExceptions().size());
        Assert.assertEquals(true, cause.getExceptions().get(0).isRemote());
    }
}
 
Example #8
Source File: TracingInterceptorTest.java    From aws-xray-sdk-java with Apache License 2.0 4 votes vote down vote up
@Test
public void test500Exception() throws Exception {
    SdkHttpClient mockClient = mockSdkHttpClient(generateLambdaInvokeResponse(500));

    LambdaClient client = LambdaClient.builder()
            .httpClient(mockClient)
            .endpointOverride(URI.create("http://example.com"))
            .region(Region.of("us-west-42"))
            .credentialsProvider(StaticCredentialsProvider.create(
                    AwsSessionCredentials.create("key", "secret", "session")
            ))
            .overrideConfiguration(ClientOverrideConfiguration.builder()
                    .addExecutionInterceptor(new TracingInterceptor())
                    .build()
            )
            .build();


    Segment segment = AWSXRay.getCurrentSegment();

    try {
        client.invoke(InvokeRequest.builder()
                .functionName("testFunctionName")
                .build()
        );
    } catch (Exception e) {
        // ignore SDK errors
    } finally {
        Assert.assertEquals(1, segment.getSubsegments().size());
        Subsegment subsegment = segment.getSubsegments().get(0);
        Map<String, Object> awsStats = subsegment.getAws();
        @SuppressWarnings("unchecked")
        Map<String, Object> httpResponseStats = (Map<String, Object>) subsegment.getHttp().get("response");
        Cause cause = subsegment.getCause();

        Assert.assertEquals("Invoke", awsStats.get("operation"));
        Assert.assertEquals("testFunctionName", awsStats.get("function_name"));
        Assert.assertEquals("1111-2222-3333-4444", awsStats.get("request_id"));
        Assert.assertEquals("extended", awsStats.get("id_2"));
        Assert.assertEquals("us-west-42", awsStats.get("region"));
        // 500 exceptions are retried
        Assert.assertEquals(3, awsStats.get("retries"));
        Assert.assertEquals(2L, httpResponseStats.get("content_length"));
        Assert.assertEquals(500, httpResponseStats.get("status"));
        Assert.assertEquals(false, subsegment.isError());
        Assert.assertEquals(false, subsegment.isThrottle());
        Assert.assertEquals(true, subsegment.isFault());
        Assert.assertEquals(1, cause.getExceptions().size());
        Assert.assertEquals(true, cause.getExceptions().get(0).isRemote());
    }
}
 
Example #9
Source File: TracingInterceptorTest.java    From aws-xray-sdk-java with Apache License 2.0 4 votes vote down vote up
@Test
public void testAsync500Exception() {
    SdkAsyncHttpClient mockClient = mockSdkAsyncHttpClient(generateLambdaInvokeResponse(500));

    LambdaAsyncClient client = LambdaAsyncClient.builder()
            .httpClient(mockClient)
            .endpointOverride(URI.create("http://example.com"))
            .region(Region.of("us-west-42"))
            .credentialsProvider(StaticCredentialsProvider.create(
                    AwsSessionCredentials.create("key", "secret", "session")
            ))
            .overrideConfiguration(ClientOverrideConfiguration.builder()
                    .addExecutionInterceptor(new TracingInterceptor())
                    .build()
            )
            .build();

    Segment segment = AWSXRay.getCurrentSegment();

    try {
        client.invoke(InvokeRequest.builder()
                .functionName("testFunctionName")
                .build()
        ).get();
    } catch (Exception e) {
        // ignore exceptions
    } finally {
        Assert.assertEquals(1, segment.getSubsegments().size());
        Subsegment subsegment = segment.getSubsegments().get(0);
        Map<String, Object> awsStats = subsegment.getAws();
        @SuppressWarnings("unchecked")
        Map<String, Object> httpResponseStats = (Map<String, Object>) subsegment.getHttp().get("response");
        Cause cause = subsegment.getCause();

        Assert.assertEquals("Invoke", awsStats.get("operation"));
        Assert.assertEquals("testFunctionName", awsStats.get("function_name"));
        Assert.assertEquals("1111-2222-3333-4444", awsStats.get("request_id"));
        Assert.assertEquals("extended", awsStats.get("id_2"));
        Assert.assertEquals("us-west-42", awsStats.get("region"));
        // 500 exceptions are retried
        Assert.assertEquals(3, awsStats.get("retries"));
        Assert.assertEquals(2L, httpResponseStats.get("content_length"));
        Assert.assertEquals(500, httpResponseStats.get("status"));
        Assert.assertEquals(false, subsegment.isError());
        Assert.assertEquals(false, subsegment.isThrottle());
        Assert.assertEquals(true, subsegment.isFault());
        Assert.assertEquals(1, cause.getExceptions().size());
        Assert.assertEquals(true, cause.getExceptions().get(0).isRemote());
    }
}
 
Example #10
Source File: LambdaInvoke.java    From aws-doc-sdk-examples with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) {


        if (args.length < 1) {
            System.out.println("Please specify a function name");
            System.exit(1);
        }

        // snippet-start:[lambda.java2.invoke.main]
        
        /*
        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
        */
        String functionName = args[0];

        InvokeResponse res = null ;
        try {
            Region region = Region.US_WEST_2;
            LambdaClient awsLambda = LambdaClient.builder().region(region).build();

            //Need a SdkBytes instance for the payload
            SdkBytes payload = SdkBytes.fromUtf8String("{\n" +
                    " \"Hello \": \"Paris\",\n" +
                    " \"countryCode\": \"FR\"\n" +
                    "}" ) ;

            //Setup an InvokeRequest
            InvokeRequest request = InvokeRequest.builder()
                    .functionName(functionName)
                    .payload(payload)
                    .build();

            //Invoke the Lambda function
            res = awsLambda.invoke(request);

            //Get the response
            String value = res.payload().asUtf8String() ;

            //write out the response
            System.out.println(value);

        } catch(ServiceException e) {
            e.getStackTrace();
        }
        // snippet-end:[lambda.java2.invoke.main]
    }