AWS Lambda for machine learning engineers

Source: Deep Learning on Medium

AWS Lambda for machine learning engineers

( This tutorial covers the steps in the deployment of machine learning model in Lambda )

Deployment of the machine learning model is a tedious task but with services like AWS lambda, it does not have to be anymore. AWS lambda falls under the concept of serverless computing which means that we can deploy our model easily without the headache of managing servers, runtimes, and infrastructures. Lambda provides runtimes for programming languages like Python, Ruby and more.

Utilizing this benefit of serverless computing, we can easily deploy our machine learning models like classification model, regression model and much more at ease quickly and efficiently. We get to choose the processing power of our own choice and runtimes that apt requirements of our machine learning model.

Note: If you are new to linear regression or polynomial regression then I highly recommend you check out my linear regression for busy data scientists or polynomial regression in R tutorial.

One drawback of AWS lambda is that there is a limit in size of dependencies our model can have. For example libraries like spacy with the English language, the model can take up to a GB of RAM. But most of the models like classification models can easily be deployed since they have dependencies like SkLearn, NumPy or Pandas. And the good news is that lambda provides this stack of data science libraries out of the box.

Wait! If you want to take a quick refresher in pandas, don’t forget to read this short pandas for busy data scientists tutorial.

In this tutorial, I will cover the concepts of how to set up a machine learning model in Lambda. I will not take a walkthrough of each step building the model but, I will try to give to concepts so that you can try on your own. The benefit of this is that actual learning and understanding comes from doing yourself.

Concepts of Layers:

Lambda has the concept of layers, which means that we can add layers into our lambda function. Each layer can be a dependency or group of dependencies depending upon our necessities. For example, pandas and numpy can be one layer or they can be different layers on their own. While creating a lambda function, we get to opt which layer we want to add in our lambda function and it helps to add the same layer in different lambda functions.

Steps in creating and using these layers in lambda functions:

  1. Create a virtual environment in the local computer
  2. Pip install all required libraries
  3. Get inside site-packages directories of the local virtual environment
  4. Zip all the dependencies
  5. If you know what you are doing then, you can also remove unnecessary modules to reduce dependency sizes
  6. Upload dependencies to S3
  7. Create a layer from lambda dashboard
  8. Create a new lambda function and provide ARN to that layer to add it as a layer.
  9. In your Lambda function, add “/opt/” as a path
  10. Import the libraries
  11. Perform Classification

And that’s it. If you want your model to be accessible from outside the world then,

  1. Create a new API gateway
  2. Create a new resource
  3. Add HTTP method, most probably POST request with some parameters
  4. Point that route to Lambda function

Furthermore, if you are actually deploying a machine learning model in production, then you should add Cloudwatch to see logs of your application so that you will know if you encounter some errors.

If you want to deploy your machine learning model in conventional way, don’t forget to also check out my deploy your first machine learning model using flask tutorial.

I also write about other topics like recommendation systems, natural language processing and many more. If you are also exploring these topics, don’t forget to visit my profile and see if there are any posts that interest you.

If you like my article, don’t forget to follow me on Medium, or connect me on Linkedin, or follow me on twitter.