Cheapest way to run Machine Learning Inference Model in AWS

I have a pre-trained machine learning model and I wanna be able to straight run an inference on it via Rest API . I have a couple of competing thoughts:

  1. Run a t3.medium instance with the model on it. Make a Flask app and call the model based when hitting the REST Endpoint
  2. Run API Gateway to trigger a lambda function to trigger a SageMaker endpoint running an ml.t2.medium instance (~2.3 GB Model should be okay on that size, right?).

I am aware that the best practice is #2, but I’m also unsure on how to set up the communication between the lambda function and the inference endpoint. Any advice, ideas, and tutorials are all helpful!

I’m not sure about best practices but here’s something I’ve been meaning to try. I think it would be very cheap to run since it’s serverless.

Make two lambdas: one for your api gateway integration and one for your inference.

Api gateway integration lambda can be a traditional zip file based lambda and I would personally deploy it using something like aws chalice.

Inference lambda would be a container based lambda whose image you build and store in ECR. Note that these containers can be up to 10gb and use up to 6 cpus. Will be able to handle a variety of ML model inferences as long as they are cpu based.

Then in your apig lambda make an endpoint for invoking your container based inference lambda.

Now you only pay for the number of inferences you make and not for a long lived ec2 instance.

Only thing to add, if you are worried about cold starts, use provisioned lambda. This is going to be the new future imo.

The absolute cheapest way to do it is serve inferences directly on lambda (see the other comment for more detail). It’s doable and you can keep the lambda warm to reduce or prevent cold starts.

If you want to trigger inference from a lambda, you can use boto3 which has a sagemaker specific invoke_endpoint() method that will do it for you: see this aws blog.

It’s the present at my company. Serving inferences on provisioned lambdas works really well and, importantly, is super cheap.

You could use AWS SAM CLI. It has an example that sets up most of this for you. It uses API Gateway and Lambda.

I recently set up serving for a transformer model that is used intermittently from another app via simple REST API running from a docker container.

A few learnings I had was that getting PyTorch to function well inside a pre-forking Flask/gunicorn/.. setup where the webbserver forks workers is pretty much impossible.

I wanted all the worker proceedes to share the memory for the model, so the overall memory requirements would be ~2GB instead of 2GB* number of workers. The way to do this is by loading the model before forking, or by loading the model into a shared memory segment.

However this doesn’t work well with PyTorce due it having its own worker threads and memory management. Ended up with lots of deadlocks and crashes. So loading the model before forking doesn’t work well, nor did shared memory work well for me because it’s not supported on AWS Fargate.

In the end I wrapped the model in an AWS Lambda container and deployed it behind an AWS ALB. It uses ALB health checks every 300 seconds to keep the Lambda warm’ish and generally avoid the 30s cold starts. This seems to come out cheaper than provisioned capacity in my case which sees very intermittent usage.

I can really recommend using ALB+Lambda instead of API Gateway+Lambda, the ALB is much easier to work with than API Gateway.

This setup was a success for us, I found it very easy to setup and deploy the container. And it runs smoothly and without issues, and the cost is negligible because we pay only for the execution time of each inference rather than paying for provisioned instances.

Lambda supports 10GB memory and 10GB docker images these days, and with the ALB integration, pay for what you use, and optionally provisioned capacity it’s pretty darn good for serving ML models I think.

https://docs.aws.amazon.com/elasticloadbalancing/latest/application/lambda-functions.html
https://aws.amazon.com/blogs/aws/new-for-aws-lambda-container-image-support/

Very interesting! Anything specific about alb that makes it easier to work with vs api-g when it comes to lambda integration? Anything besides ease of use where alb is a better choice over api-g in this case?

Mainly ease of use I’d say, I’ve configured both at times and the API Gateway is just very very cumbersome to work with, to configure and debug.

Another annoying limit I’ve encountered is that API Gateway had a hard limit on 30 seconds of execution time, which can’t be raised. Lambda and ALB supports much longer execution times.

With the ALB you just create a target group that points to your Lambda, ideally with a health check. Then tell your internal or external ALB to forward /some-path/.. to your target group. Then you create a Route53 alias record and optionally a free ACM SSL certificate and attach it to the ALB. Very straight forward :slightly_smiling_face:

I like this! Trying it out now, but for some reason the ALB isn’t triggering the lambda. Went to check the target group and it’s apparently unavailable. I’m not sure if that’s because I’m using a lambda instead of an EC2 as my target or if I’m not setting up the trigger properly, but this is the last thing stopping me right now.

I found it could take a couple of minutes for my target group to become available after deploying a new container, probably due to the size of the image (several gigs) and startup time (loading the model at startup)

I pretty much just used this guide

https://docs.aws.amazon.com/lambda/latest/dg/python-image.html

And loaded the model as a global variable, and serve a simple REST API from the handler function.

I found it useful to add a /status endpoint and configure the target group to do health checks. And I could see the error log and test it in the Lambda console, to figure out why something wasn’t working.

Best of luck! It’s doable and makes for a very smooth operations if you get it to work.

My models basically just build a new image on commit, and then autodeploys it to Lambda via a simple awscli call. Continuous deployment for ML models, nice and tidy and no need for an ops team :slightly_smiling_face:

“The maximum size of the request body that you can send to a Lambda function is 1 MB”

I learned that the hard way when I thought I could easily pass raw images to an ALB or API gateway, then to a sagemaker model via lambda and get an image back with the bounding boxes and labels, etc.

Easy solution. You’ll write your source image to S3, then call your ALB or API gateway endpoint with a payload that includes the path to the S3 file. You’ll then include code in your lambda function that downloads the image and can use it for inference.

I’m going to try the ALB-only solution and see how much we actually save.