5

Does anyone know the current and correct way to invoke Amazon AWS Lambda functions asynchronously instead of synchronously?

The InvokeAsync API in the AWS Java SDK is still available but marked as deprecated and they suggest you use use the Invoke API. I can't figure out why they would be forcing us to using sync. I have a web frontend that dispatches some batch jobs. I can't expect the frontend to keep a connection open for several minutes while it waits for the response (which is actually e-mailed to them after about 4-5 minutes of processing).

Ideally I'm trying to figure out how to do this with their API Endpoints rather than the Java SDK because the environment (GAE) that I'm running my backend in doesn't support AWS's use of HttpClient.

4 Answers 4

9

I'm looking at the latest API docs here, and it looks like only AWSLambdaAsyncClient.invokeAsyncAsync() is deprecated. The AWSLambdaAsyncClient.invokeAsync() method is not marked as deprecated. It looks like they are just doing some code cleanup by removing the need for the InvokeAsyncRequest and InvokeAsyncResult classes and the extra invokeAsyncAsync() methods.

You should be able to use the AWSLambdaAsyncClient.invokeAsync() method which uses InvokeRequest and returns InvokeResult. You might have to set the InvocationType on the InvokeRequest to InvocationType.Event. It's not clear if that's needed if you are using the Async client.

Regarding your second question about calling Lambda functions asynchronously without using the SDK, I would look into using API Gateway as a service proxy. This is the recommended way to expose Lambda functions for asynchronous calls.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, that helped clarify things for me!
7

The below code can be used to invoke the Lambda asynchronously from another Lambda

AWSLambdaAsyncClient client = new AWSLambdaAsyncClient();
client.withRegion(Regions.fromName(region));
InvokeRequest request = new InvokeRequest();
request.setInvocationType("Event");
request.withFunctionName(functionName).withPayload(payload);
InvokeResult invoke = client.invoke(request);

4 Comments

The above example is for Lambda in JAVA
how to define payload, if I want to add JsonObject then!!
ObjectMapper can be used to convert the custom Object to JSON and then it can be passed as payload
I want to highlight that the magic here is to set invocationType to Event. Without that line the parent lambda waits until the termination of child function.
6

Approach given in the accepted answer is now deprecated. Answer given by the user @dassum is the approach to be followed, but the answer lacks a bit of explanation.

When creating the InvokeRequest, set the InvocationType as "Event" for asynchronous invocation and "RequestResponse" for synchronous invocation.

AWSLambda lambda = //create your lambda client here
lambda.invoke(new InvokeRequest()
                            .withFunctionName(LAMBDA_FUNCTION_NAME)
                            .withInvocationType(InvocationType.Event) //asynchronous
                            .withPayload(payload))

Reference to docs:

https://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/services/lambda/AWSLambda.html#invoke-com.amazonaws.services.lambda.model.InvokeRequest-

1 Comment

This approach has limitations. You are then bound to the limitations of the event type which means your payload size can only be 262144 bytes.
0

You can try the following:

YourCustomRequestBean request = new YourCustomRequestBean();

request.setData1(data1);
request.setData2(data2);
request.setData3(data3);

ClientConfiguration clientConfiguration = new ClientConfiguration();
clientConfiguration.setSocketTimeout(xxxx);
clientConfiguration.setRequestTimeout(xxxx);

ObjectMapper objectMapper = new ObjectMapper();
String jsonStr = objectMapper.writeValueAsString(request);
AWSLambdaAsync awsLambdaAsync = 
AWSLambdaAsyncClientBuilder.standard().withRegion(<mention your region here>).withClientConfiguration(clientConfiguration).build();

InvokeRequest invokeRequest = new InvokeRequest()
    .withFunctionName(lambda function urn)
    .withPayload(jsonStr);
awsLambdaAsync.invokeAsync(invokeRequest);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.