Skip to content

AWS SDK Java V2 invoke Lambda without wait

0

Hello,

Please see below a Java AWS SDK V2 sequence from an asynchronous Lambda invocation:

InvokeRequest request = InvokeRequest.builder()
                .functionName(functionName)
                .payload(SdkBytes.fromUtf8String(functionInput))
                .invocationType(InvocationType.EVENT) 
                .build();
CompletableFuture<InvokeResponse> future = lambdaAsynClient.invoke(request);
 logger.debug("Lambda function just invoked");
future.thenAccept(response -> {
        logger.debug("Lambda function invoked successfully");
 }).exceptionally(ex -> {
        logger.debug("Error invoking Lambda function: " + ex.getMessage());
        return null;
});

This code will never call the target lambda, unless the below line is added, to wait for the response:

future.join();

Is it possible to call asynchronously without waiting, like in an S3 event trigger ?

Thank you,
Mihai ADAM

asked 2 years ago587 views

1 Answer
0
Accepted Answer

Yes, it is possible to invoke an AWS Lambda function asynchronously without waiting for the response, similar to how an S3 event trigger works. The code you've provided is actually correct for asynchronous invocation, and it should work without the need to call future.join().

When you set the invocationType to InvocationType.EVENT, you're telling AWS Lambda to invoke the function asynchronously. This means that Lambda will queue the event for processing and return immediately, without waiting for the function to complete execution.

The CompletableFuture returned by lambdaAsynClient.invoke(request) represents the asynchronous operation of sending the invocation request to Lambda, not the execution of the Lambda function itself. The thenAccept and exceptionally methods you've added are handling the response of this request, not the actual Lambda function execution.

If you're not seeing the Lambda function being invoked, there might be other issues at play:

  1. Make sure your AWS credentials and permissions are correctly set up.
  2. Verify that the function name is correct.
  3. Check the CloudWatch logs for the Lambda function to see if it's being invoked.
  4. Ensure that your application doesn't exit before the asynchronous call has a chance to complete.

If you want to ensure that the invocation request is sent before your application exits, you can use future.get() instead of future.join(). This will wait for the invocation request to be sent, but not for the Lambda function to complete its execution.

Remember, when using asynchronous invocation, you won't receive the function's return value. If you need to handle the function's result, you should consider using Lambda destinations or implement a callback mechanism in your function.
Sources
Invoking a Lambda function asynchronously - AWS Lambda
Invoke - AWS Lambda
AWS Lambda Supports Destinations for Asynchronous Invocations

answered 2 years ago

EXPERT

reviewed 2 years ago

  • Thank you for your answer, From all above it seams that the application exit before the asynchronous call has a chance to complete. In order to ensure that the invocation request is sent before the application exits, I had to use future.get()

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.