2

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).

1

1 Answer 1

1

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).

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

2 Comments

thanks very much. i tried it but can't get it to work. i think my problem is the json format of the message (jsonStuff) i pass when calling publish: snsClient.publish("arn:aws:sns:eu-west-1:xxxx...xxxx", jsonStuff); - any hints on where i can see examples (in java)? thanks
managed to figure it out. need to create a lambda function that receives an SNS event as the input type. then pass json input message as string (using Gson) to convert.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.