sejalv / serverless-ml-workshop

Deployment of ML models with Serverless APIs (AWS Lambda) and Docker

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Serverless ML Workshop

This workshop focusses on deployment of ML models with Serverless APIs (AWS Lambda) and Docker. We'll learn how to:

  • Train and serialize a model inside a container
  • Package the service in an image
  • Serve the model with serverless API
  • Build, Test, and Deploy, with CI/CD workflow


  • Docker + ECR: Container & Registy
  • AWS Lambda: Serving API
  • SAM: Serverless Framework (optional)
  • GitHub Actions: CI/CD

Project Structure

|-- service
     |-- source code lambda handler
     |-- to train the model
     |-- Dockerfile: to build the Docker image
     |-- requirements.txt: dependencies
|-- tests
     |-- unit
          | unit test/s for lambda handler
|-- samconfig.toml: configured by SAM
|-- template.yaml: A template that defines the application's AWS resources.



$ aws ecr create-repository --repository-name <repo-name> [--image-scanning-configuration scanOnPush=true]



  1. Using Docker
$ docker build -t serverless-ml ./service
  1. Using SAM
$ sam build

The processed template file is saved in the .aws-sam/build folder.

Testing locally

  1. Using Docker
$ docker run -p 8080:8080 serverless-ml

$ curl -XPOST "http://localhost:8080/2015-03-31/functions/function/invocations" -d '{"body": {"data": ".10"}}'
  1. Using SAM
$ sam local start-api

$ curl -XPOST -H 'Content-Type: application/json' -d '{"data":".10"}'

Unit tests

Tests are defined in the tests folder in this project. Use PIP to install the pytest and run unit tests from your local machine.

$ pip install pytest pytest-mock --user
$ python -m pytest tests/ -v


  1. Using Docker
$ aws ecr get-login-password | docker login --username AWS --password-stdin ${AWS_ACCOUNT}

$ docker push ${AWS_ACCOUNT}
  1. Using SAM
sam deploy --guided


To delete the sample application that you created, use the AWS CLI. Assuming you used your project name for the stack name, you can run the following:

aws cloudformation delete-stack --stack-name <stack-name>


Workshops built on new AWS features:

The Docker-based workflow (multi-stage build) is based on:

ezoic increase your site revenue


Deployment of ML models with Serverless APIs (AWS Lambda) and Docker


Language:Python 90.0%Language:Dockerfile 10.0%