0
votes

AWS Lambda functions are supposed to respond quickly to events. I would like to create a function that fires off a quick request to a slow API, and then terminates without waiting for a response. Later, when a response comes back, I would like a different Lambda function to handle the response. I know this sounds kind of crazy, when you think about what AWS would have to do to hang on to an open connection from one Lambda function and then send the response to another, but this seems to be very much in the spirit of how Lambda was designed to be used.

Ideas:

  • Send messages to an SQS queue that represent a request to be made. Have some kind of message/HTTP proxy type service on an EC2 / EB cluster listen to the queue and actually make the HTTP requests. It would put response objects on another queue, tagged to identify the associated request, if necessary. This feels like a lot of complexity for something that would be trivial for a traditional service.
  • Just live with it. Lambda functions are allowed to run for 60 seconds, and these API calls that I make don't generally take longer than 10 seconds. Not sure how costly it would to have LFs spend 95% of their running time waiting on a response, but "waiting" isn't what LFs are for.
  • Don't use Lambda for anything that interacts with 3rd party APIs that aren't lightning fast :( That is what most of my projects do these days, though.
1
Why not have it publish an SNS message and have a lambda digest that? OR you can set up Call-B as an API endpoint under the API endpoint tab and http request that endpoint at the end of Call-A - that's probably better.iSkore
If I understand correctly, would that mean that Call-A would make an HTTP request, wait for the response, and then send that off to another task? That would still mean that Call-A, a Lambda func, is sitting idle, waiting on an external API, no?Dan Ross
Oh. Do you need a response? With the SNS publish you will definitely not have to wait. Also you could fire off an HTTP and not wait. Just fire off info to the API and succeed the lambda.iSkore
I'll check out SNS more thoroughly, I haven't actually used it yet. If it can make an HTTP request and then fire an event into Lambda with the response, then it is perfect for this.Dan Ross
Ya it's very strange that AWS doesn't allow Lambda invoke from SQS. I don't get that at all. SNS is nearly the same thing. The simple difference is: SQS - many publish to source | SNS - source publish to manyiSkore

1 Answers

1
votes

It depends how many calls will this lambda execute monthly, and how many memory are you allocating for those lambda. The new timeout for lambda is 5 minutes, which should (hopefully :p) be more than enough for an API to respond. I think you should let lambda deal with all of it to not over complicate the workflow. Lambda pricing is generally really cheap.

E.g: a lambda executed 1 million times with 128 MB allocated during 10 seconds would cost approximatively 20$ - this without considering the potential free tier.