Replacement for AWS Lambda invokeAsync (deprecated)

I am trying to call a Java Lambda function asynchronously from another Java Lambda function. I just want to run and forget, but with .invokeAsync (InvokeRequest), I need to call .get () in the Future, which then blocks and breaks the use case of fire and forget.

Here is the code I'm trying to use:

private void sendToDownloader(String payload) throws InterruptedException, ExecutionException { log.info(payload); InvokeRequest invoke = new InvokeRequest(); invoke.withFunctionName("LambdaTwo") .withPayload(payload) .withInvocationType(InvocationType.Event); lambdaClient.invokeAsync(invoke).get(); } 

If I delete the call to the .get () method, it does not actually call LambdaTwo.

It should be noted that this lambda function ends immediately after calling LambdaTwo.

Meanwhile, I tried using the following code:

 private void sendToDownloader(String payload) throws InterruptedException, ExecutionException { log.info(payload); InvokeAsyncRequest invoke = new InvokeAsyncRequest(); invoke.withFunctionName("LambdaTwo") .withInvokeArgs(payload); lambdaClient.invokeAsync(invoke); } 

This code works, however .invokeAsync is deprecated, and it says to see the "Invoke API" for which I cannot find any documentation.

Can someone lead me to the correct way to call the lambda function in fire and forget mode?

EDIT 1 Per Mark, I rewrote the code below, however it seems to be blocked while the LambdaTwo function is executing.

 public void routeEvent(String lambdaName, String cacheName) throws InterruptedException, ExecutionException, JsonProcessingException { ObjectNode obj = new ObjectNode(JsonNodeFactory.instance); obj.put("nodeName", cacheName); InvokeRequest invoke = new InvokeRequest(); invoke.withFunctionName(lambdaName) .withInvocationType(InvocationType.Event) .withPayload(OBJECT_MAPPER.writeValueAsString(obj)); log.info(System.currentTimeMillis()); lambdaClient.invoke(invoke); log.info(System.currentTimeMillis()); } 

It prints the next milliseconds.

2016-11-28 03:41:35 INFO LambdaFunction: 97 - 1480304495867
2016-11-28 03:41:41 INFO LambdaFunction: 99 - 1480304501432

There is a 5.5 second difference between the two and the duration of Lambda confirms this.

EDIT 2 To clarify, I tried using AWSLambdaAsyncClient calling .invokeAsync (InvokeRequest) and .invoke (InvokeRequest) and none of them worked. When invokeAsync is called, it actually does not call the Lambda function unless I call .get () on it, which then blocks while the second lambda function is executing.

What ultimately worked was calling .invokeAsync (InvokeRequest), but not calling .withInvocationType (InvocationType.Event) of the InvokeRequest object. It is not clear why this caused asynchronous behavior, but what worked and does not use the deprecated method.

 public void routeEventAsync2(String lambdaName, String cacheName) throws InterruptedException, ExecutionException, JsonProcessingException { ObjectNode obj = new ObjectNode(JsonNodeFactory.instance); obj.put("nodeName", cacheName); InvokeRequest invoke = new InvokeRequest(); invoke.withFunctionName(lambdaName) .withPayload(OBJECT_MAPPER.writeValueAsString(obj)); lambdaAsyncClient.invokeAsync(invoke); } 
+5
source share
1 answer

The documentation for deprecating this method is confusing. What they are trying to say is to use AWSLambdaClient.invoke(request) . You will need to set InvocationType to Event in the request object in order to call the lambda function without waiting for a response.

+3
source

Source: https://habr.com/ru/post/1260422/


All Articles