AWS Lambda long running http requests

1.7k Views Asked by At

I have an AWS Lambda function which calls a deep learning function on Algorithmia, does some post processing on the results and then returns some data. Algorithmia provides a python client which I am using that just makes things a little easier to send a request to an algorithm on the Algorithmia platform.

The problem is as follows: When an Algorithmia function hasn't been called for a while it is unloaded and the first call to warm it up (cold start) takes a while, possibly 30 seconds. If my Lambda function is going to be waiting for 30 seconds for a response whenever it happens to be triggering the Algorithmia function from a cold start that's going to be very expensive and wasteful.

Is there some way to send off a HTTP request in Lambda and when the request is finished the results are piped into a new Lambda function so as to not require a Lambda function to be waiting the entire time and wasting resources? I'd expect not as I'm not sure how that would practically work - does anyone have other ideas as to how to avoid waiting a while for a response and wasting Lambda resources?

Edit: In most cases (except obviously the ones where the Algorithmia algorithm takes a while to load from cold start) latency is an issue and I can't afford to increase latency by doing some workaround method with the Algorithmia function writing it's response to S3 (for example) and then triggering a Lambda function.

2

There are 2 best solutions below

2
On

A lot of Algorithmia functions that output a file allow you to specify the output location (often an output parameter of the input JSON). If that assumption holds for your case, then you can have the Algorithmia function write directly to an S3 bucket and have S3 trigger a separate lambda function. The process would look like this:

  • Add an S3 data source to your Algorithmia account, and configure the permissions according to your needs.

  • When calling the algorithm, set the output parameter to use that S3 data source, e.g. "output": "s3://algorithm-name/sample-0001.png"

  • Configure Algorithmia's python client to disregard the output. This causes the request to return immediately instead of waiting for the function to complete:

from Algorithmia.algorithm import OutputType

client.algo("username/algoname")
    .set_options(output=OutputType.void)
    .pipe(input)
2
On

You could create a Lambda function just to call Algorithmia API from time to time, just to "keep it warm " for your main processing function. You could use Lambda scheduled event for this.