Reputation: 652
My goal is to start a Lambda function from within another Lambda function. Specifically I want the 2nd function to start, leaving the 1st function to finish independently of the 2nd. No response needs to be returned to the 1st function.
I have spent a lot of time looking at examples as well as the AWS documentation.
I am able to successfully start the 2nd function but cannot get it run independently of the first. The 1st function always waits for the 2nd one to finish before it finishes.
I use thise code:
AWSLambdaAsync lambda = AWSLambdaAsyncClientBuilder.standard().withRegion(Regions.EU_WEST_1).build();
InvokeRequest req = new InvokeRequest().withFunctionName(functionName).withPayload(jsonStuff);
// req.setInvocationType(InvocationType.Event);
Future<InvokeResult> future_res = lambda.invokeAsync(req);
This does not start the 2nd function (with or without setting the invocation type).
If I include this code:
try {
InvokeResult thisResult = future_res.get();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Then the second function is fired but the first function waits for the second to finish before it continues.
How do I start the second function without having to wait for it to complete. At present it ads 2 seconds to the time taken for the first function to complete (the first function, without starting the second, takes 4 seconds to complete).
Upvotes: 2
Views: 564
Reputation: 13974
You could publish a message to Amazon SNS. When you go to configure the topic select AWS Lambda and put in the ARN of the lambda to trigger.
There's even an Amazon How To for triggering Lambda from SNS.
This is personally what I would do: there's a little more infrastructure here, but it even further separates the concerns of Lambda A and Lambda B (because what if, 6 months from now, you realize Lambda B has to be a traditional microservice? Easy: just reconfigure the SNS topic. Lambda A doesn't have to know).
Upvotes: 1