Workflow · Production

Serverless deployment of a KNN image classifier on AWS Lambda using Docker and SAM

The problem

Traditional cloud deployments like AWS Elastic Beanstalk require always-active provisioned resources (EC2 instances, Elastic Load Balancers) even when idle, burdening developers with infrastructure management overhead and unnecessary cost.

First attempt

The previous approach using AWS Elastic Beanstalk was largely automated but kept servers provisioned continuously, making it less cost-effective for infrequent ML prediction workloads.

Workflow diagram · grounded in source
1
Train and serialize KNN model
ai_action
“we will train a K-nearest neighbour classifier which we will deploy as a docker container”
2
Store test data in S3
integration
“we will first need to initialise a S3 bucket where we can host our data”
3
Containerize and deploy via SAM
integration
“SAM will also handle all the heavy lifting to deploy on AWS Lambda”
4
HTTP POST triggers Lambda
trigger
“a POST HTTP request to the /predict end point by LambdaAPI will trigger the lambda function”
5
Download features from S3
integration
“Download the test_features data set referenced by the bucket and key variable”
6
KNN inference and response
output
“Return JSON object of the predictions as a numpy array”
Reported outcome

A KNN classifier achieving 96% cross-validation accuracy on MNIST was successfully containerized with Docker and deployed as a serverless AWS Lambda function, accessible via an API Gateway endpoint and invocable on demand.

Reported metrics
KNN cross-validation accuracy96%
Reported stack
AWS SAMDockerAWS S3API Gatewayboto3Amazon ECR
Source
https://mlops.community/blog/serverless-deployment-of-machine-learning-models-on-aws-lambda
Read source ↗

Frequently asked questions

What did this team achieve with this AI workflow?

A KNN classifier achieving 96% cross-validation accuracy on MNIST was successfully containerized with Docker and deployed as a serverless AWS Lambda function, accessible via an API Gateway endpoint and invocable on de…

What tools did this team use?

AWS SAM, Docker, AWS S3, API Gateway, boto3, Amazon ECR.

What results were reported?

KNN cross-validation accuracy: 96% (source-reported, not independently verified).

What failed first in this deployment?

The previous approach using AWS Elastic Beanstalk was largely automated but kept servers provisioned continuously, making it less cost-effective for infrequent ML prediction workloads.

How is this workflow AI workflow structured?

Train and serialize KNN model → Store test data in S3 → Containerize and deploy via SAM → HTTP POST triggers Lambda → Download features from S3 → KNN inference and response.