ML Zoomcamp 2023 – Serverless – Part 1

In this chapter, we will focus on how to take the trained model and deploy it. There are different ways to deploy a model, and one option is to deploy it using AWS Lambda.

  1. Introduction to serverless and AWS Lambda
    1. Create a sample lambda function
    2. Benefits of using AWS Lambda

Introduction to serverless and AWS Lambda

AWS Lambda enables the deployment of various applications, including machine learning models. The process involves sending the URL of a picture of pants to our deployed model, and the service responds with multiple classes along with their respective scores. For our use case, we will use TensorFlow Lite instead of the standard TensorFlow.

To access AWS Lambda, simply type “lambda” in the AWS console. A service named “Lambda Run Code without Thinking about Servers” will be displayed. This encapsulates the essence of what you can expect from Lambda. All you need to do is write some functions without concerning yourself with EC2 instances or similar infrastructure; AWS Lambda takes care of everything.

Create a sample lambda function

Creating a simple Lambda function is straightforward; just provide some basic parameters. Choose “Author from scratch,” assign a function name, and specify Python 3.x as the runtime. This is sufficient to create the function, resulting in a Python file with the specified function name.

To make it easy just change the code to:

# event is whatever we'll pass to the lambda function
import json
def lambda_handler(event, context):
    print("parameters: ", event)
    return "PONG"

To test the code, click the “Test” button, leading to the configuration of a new test event. Execute the test. However, you may notice that the output is the old result. To reflect changes, deploy them by clicking the “Deploy” button, updating the Lambda function. Upon re-executing the test, you’ll observe a different response from the Lambda function, in this case, responding with “PONG” as expected. You can also review the parameters of the test event.

Now, let’s modify the test event by providing a URL and adjust the Lambda function accordingly.

# test event code
{
    "url": "some-url-of-pants"
}
# lambda function code
import json
def lambda_handler(event, context):
    print("parameters: ", event)
    url = event['url']
    return {"prediction": "pants"}
# actually it should look like...
import json
def lambda_handler(event, context):
    print("parameters: ", event)
    url = event['url']
    results = predict(url)
    return results

Benefits of using AWS Lambda

There are three primary advantages of utilizing Lambda functions:

  1. Infrastructure Abstraction:
    • With Lambda functions, you are relieved from the burden of managing infrastructure for serving models. This eliminates the need to consider EC2 instances, as mentioned earlier.
  2. Cost-Efficiency:
    • AWS Lambda operates on a pay-per-request model, meaning you only incur charges when the Lambda function is actively performing tasks. This pay-as-you-go approach can lead to cost savings compared to maintaining constantly running infrastructure.
  3. Free Tier Usage:
    • AWS Lambda provides a free tier, offering a certain amount of free Lambda requests per month for each account. This can be advantageous for small-scale or experimental projects.

After testing, if you no longer require the Lambda function, you can delete it by navigating to the “Actions” dropdown and selecting “Delete function.”

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.