Deploy Machine learning model in AWS lambda using Docker
Bhupin baral
December 24, 2022
AWS lambda is the most used compute service where you can run any code(backend service) without managing or provisioning the server. Lambda offers a balanced memory, network and CPU. Few benefits of lambda are as follows.
- Cost-effective: Because you only pay for the compute time you consume, you can use AWS Lambda for a wide range of workloads without worrying about the cost of idle capacity.
- Scalability: AWS Lambda automatically scales your applications in response to incoming requests, so you don’t have to worry about capacity planning or manually scaling your infrastructure.
- Integration with other AWS services: AWS Lambda integrates with a wide range of other AWS services, making it easy to build serverless applications that leverage other AWS resources.
- Flexibility: AWS Lambda supports multiple programming languages and allows you to bring your own libraries, so you can use the tools and frameworks you are already familiar with.
- Easy to use: AWS Lambda has a simple and intuitive interface, making it easy to get started with serverless computing.
There are few potential disadvantages of using AWS lambda in Machine learning deployments, they are as follows.
- Cold start latencies: AWS Lambda functions are initialized when they are first invoked, and this process can take some time, known as a “cold start.” This can lead to increased latencies for the first request to a newly created or infrequently used function.
- Limited runtime and memory: AWS Lambda has limits on the maximum runtime for 15 min and memory that a function can use. This can be a limitation for machine learning models that require more resources or longer runtimes to complete their computations.
- Dependency management: AWS Lambda does not provide a fully managed environment, so you will need to handle dependency management and ensure that all necessary libraries and frameworks are included in the deployment package.
- Complexity: Building and deploying machine learning models in a serverless environment can be more complex than deploying to a traditional server infrastructure. You may need to consider additional factors such as event triggers, scaling, and security.
- Limited debugging and monitoring: AWS Lambda provides limited debugging and monitoring capabilities compared to more fully featured development environments. This can make it more difficult to troubleshoot issues that may arise during development and deployment.
Despite these potential disadvantages, AWS Lambda can still be a useful platform for deploying machine learning models, especially for simple or small-scale deployments. It’s important to carefully evaluate your needs and use cases then consider whether the benefits of using AWS Lambda outweigh the potential challenges.
AWS lambda is a serverless technology which follows micro-service architecture. In a microservices architecture, an application is divided into a set of smaller, independent services that communicate with each other over well-defined APIs. This can make it easier to develop, deploy, and maintain applications, as each service can be developed and tested independently and can be scaled independently of the others. Also serverless is a broader term that refers to the concept of building and running applications and services without having to worry about infrastructure. AWS Lambda is one example of a serverless technology, but there are also other serverless platforms and tools available, such as Azure Functions and Google Cloud Functions, but here we only talk about lambda.
Its is same as exposing a machine learning model using fast API but instead of fast API we use the handler function . For creating handler function follow the AWS documentation. You can change the handler into predict or any other function name. Put all the model file inside the directory.
You need to have docker desktop setup in your device then create a ```Dockerfile``` inside your project directory then copy and modify accordingly
FROM python:buster as build-image
RUN apt-get update -y
RUN apt-get install -y python-pip python-dev build-essential
RUN apt-get install -y \
g++ \
make \
cmake \
unzip \
libcurl4-openssl-dev
COPY requirements.txt requirements.txt
RUN pip install -r requirements.txt
COPY . ./
ENTRYPOINT [ "/usr/local/bin/python", "-m", "awslambdaric" ]
CMD [" {path}.handler_function_name"]
Follow the serverless documentation and create a file named as ```serverless.yml``` and insert these inside.
service: service_name
provider:
name: aws
runtime: python3.8
stage: ${opt:stage, 'dev'}
profile: profile_name
region: region_name
ecr:
images:
dockerimage:
path: ./
deploymentBucket:
blockPublicAccess: true
name: name_of_bucket
package:
patterns:
- "!venv/**"
- "!aws"
- "!.venv/**"
- "!node_modules/**"
- "!resources/**"
- "!package-lock.json"
- "!yarn.lock"
- "!README.md"
- "!serverless.yml"
- "!.gitignore"
- "!.vscode/**"
- "!.serverless/**"
- "!.sample.env"
- "!.env.**"
- "!dishes.dataset.json"
- "!dishes.dataset2.json"
- "!.editorconfig"
- "!.flake8"
- "!makefile"
- "!.pytest_cache/**"
- "!.mypy_cache/**"
individually: true
plugins:
- serverless-dotenv-plugin
- serverless-iam-roles-per-function
functions:
function_name:
image:
name: dockerimage
events:
- http:
method: PUT #HTTP method
path: path
cors: true
Serverless works with node module so install node in your system along with npm and run
npm i {plugins_name}
Then
sls deploy --stage {stage_name} --verbose
After this command it will automatically built the docker image push in ECR and provides the end point for lambda API. Test the API using postman and check the cloudwatch for debugging.