Reputation: 87
I have a Node.js 8.10 Lambda. It responds to POST requests to my API Gateway.
I want to:
My code is this:
function handler(event, context, lambdaCallback) {
//210000 milliseconds is 3.5 minutes
let timeToWait = 210000;
//respond to the caller and continue to process the request
lambdaCallback(null, {"message": "process started"});
setTimeout(() => {
doSomethingUseful();
}, timeToWait);
}
doSomethingUseful() runs after the specified timeout in all cases.
The issue is that I don't get a response. I think that the API gateway response timeout kills the response -- normally I get: { "message": "Endpoint request timed out" }
.
I think that the API gateway response timeout kills the response because if I reduce timeToWait to a trivial value, I get the expected response ({"message": "process started"}
).
Any ideas on how to send a quick response before the end of the setTimeout?
Upvotes: 0
Views: 3808
Reputation: 1047
AWS Step Functions as the accepted answer is a great option (and likely the best for the specific requirement). However, I just also wanted to point out that you can do this with SQS and delayed events which trigger a lambda function.
Note: Maximum time is 15 minutes for those who would want more than the 3.5 minutes OP is looking for.
You could also do an Invoke of another lambda function with Event
for InvocationType
(async invoke). This allows running after the user has a response sent to them, but clearly the actions of the secondary lambda must be fully isolated as we can not return a response to the user from it. This is definitely not the right solution for the OP as there is no reason to pay for 3.5 minutes of lambda run time to have it do nothing, but the pattern can be extremely helpful.
I use the asyncInvoke
pattern when I have in-vpc processing that needs to be done which I don't want the user to have to deal with the dreaded 10s VPC cold start for lambda when it is not a necessary / connected part of the request.
Note: If you use either method above, it is highly recommended you setup a Dead Letter Queue for the lambda function since async invokes will retry execution multiple times when failed.
Anyway, not to detract from the best answer based on OP needs, but figured it can be helpful to see what other possibilities there may be for others that happen upon the post!
Upvotes: 1
Reputation: 3249
Use AWS Step Functions with wait state
Upvotes: 2