- Newest
- Most votes
- Most comments
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:
- Make sure your AWS credentials and permissions are correctly set up.
- Verify that the function name is correct.
- Check the CloudWatch logs for the Lambda function to see if it's being invoked.
- 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
Relevant content
asked 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()