The world’s leading publication for data science, AI, and ML professionals.

Deploy XGBoost Models from SageMaker using Lambda Functions

Quick tutorial on how to expose SageMaker ML model endpoints for everyone to use

Photo by Jake Fagan on Unsplash
Photo by Jake Fagan on Unsplash

Creating ML model endpoints inside Amazon Sagemaker is pretty awesome but unless you can enable others to use them, what’s the point? In the previous tutorial:

Train XGBoost models in Amazon SageMaker in 4 Simple Steps

we went over how to create an XGBoost Classifier that can predict a perfect puppy based on a person’s house area. That is, for houses smaller than 500 sq.ft the model would recommend Beagle (0) and for greater than 500, it would recommend German Shepherd (1).

Sources: Left [1] & Right [2]
Sources: Left [1] & Right [2]

Now it’s time to deploy this endpoint on the internet! Here is how:

Image by Author
Image by Author

👉 Game Plan

  1. Create a Lambda Function for processing incoming request payloads (i.e. house area inputs) and pass it to the SageMaker ML model endpoint
  2. Create a REST API using API Gateway to accept Client Requests

Here is the end goal that we are trying to achieve. We want to be able to send POST requests to our SageMaker endpoint from anywhere on the internet:

curl -X POST https://<Your_API_Gateway_REST_API>/resource/method -H 'Content-Type: application/json' -d '{"area":"300"}'

And receive the following output response:

"beagle"

Let’s see how!


1. Create a Lambda Function

It is very easy to create a Lambda function inside AWS with a couple of clicks. Here is the code you’ll need:

Before heading over to Lambda tab in your AWS console, first make sure to Note Down your Xgboost Endpoint Name which you’ll need to store as an Environment Variable inside the Lambda Function.

Let’s Create the Lambda Function

You can paste the lambda code from above here:

Next, enter SageMaker Endpoint Name (that you copied earlier) as an Environment Variable

Next, we need to give our Lambda Function permission to invoke the SageMaker endpoint. Under Permissions, Click on the Execution role:

We’ll give the Lambda function Full Access to SageMaker:

Our Lambda Function now knows which Endpoint to invoke and has the appropriate permissions to do so. Time to create the REST API.

2. Create a REST API using API Gateway

Choose:

  • REST
  • New API

Give it a name, leave default type as Regional and Click: Create API

Next, we need to Create Resource as part of our REST API:

Then Create Method:

Choose POST method:

Let’s point this REST API to the Lambda Function ("demo1")we created in the previous step:

Now we’ll Deploy API

Copy the REST API url:

And then when sending request make sure to add the method name (i.e., demo1) to the copied API url

curl -X POST https://<Your_API_Gateway_REST_API>/test/demo1 -H 'Content-Type: application/json' -d '{"area":"300"}'
Output:
"beagle"

Final Words

And there you have it! Now we have proven that our SageMaker model was successfully exposed on the internet for everyone to use and in case we have a React, Vue.js or Angular frontend application, it will be able to make similar requests and provide users with a perfect puppy recommendation.


Cleanup to stop incurring Costs!

  1. Delete the deployed endpoint by running the following command inside the Jupyter Notebook
xgb_predictor.delete_endpoint()
  1. Stop the SageMaker Notebook Instance

Want more useful articles on ML Engineering?

Subscribe for free to get notified when I publish a new story.

Become a Medium member to read more stories from me and thousands of other writers. You can support me by using my referral link when you sign up. I’ll receive a commission at no extra cost to you.

And make sure to hit that Follow button! Always much appreciated.

Image Sources

[1] https://commons.wikimedia.org/wiki/File:Beagle_puppy_Cadet.jpg

[2] https://pxhere.com/en/photo/1003603


Related Articles