I'm working with a Lambda function that processes a request, stores data in RDS, and sends a response back to the client. However, I want to perform an asynchronous action after the response has been sent. Currently, I'm triggering the action right before sending the response, but I've encountered situations where it gets executed before the response is sent, causing the action to fail. Is there a way to implement an 'onResponse' hook that ensures actions occur only after the Lambda function returns? Or is this not feasible due to Lambda's design?
4 Answers
You might want to check out the new Lambda Durable Functions. They allow for background processing after the response is sent, unlike classic Lambda functions which terminate immediately after returning a result. This could solve your issue with timing.
I missed that announcement! I like Lambdas' simplicity, though—just do one thing and return. I'm worried adding more complexity will make things messy.
I recommend keeping it simple. Either have your Lambda do the post-action after confirming the response was received, or if it must be async, write to SQS right after. Just make sure everything is retryable and easy to log, so you don’t end up with a complicated setup.
If you're looking for an async solution, you could have your Lambda send a message to SQS after confirming the response was sent. This would ensure that your post-response action is queued up properly without holding up the client.
As far as I know, Lambda doesn't support an onResponse hook. A good approach is to return your response quickly and trigger any async actions via SNS, SQS, or EventBridge afterward. This way, the client gets a fast response while your additional processing happens separately.

Was this introduced at re:Invent? Sounds like a game changer! It's great for coordinating multiple services with automatic rollback if something goes wrong, but it does make me a bit nervous about complexity.