Deploying a Scikit-Learn Model on AWS Using SKLearn Estimators, Local Jupyter Notebooks, and the Terminal | by Quinn Lanners | Towards Data Science

Amazon Web Services (AWS) is currently the most in-demand cloud platform for Data Science and Machine Learning professionals. Given this, the first collection of blog posts in this Machine Learning Deployment series will outline how to deploy various models in a number of different ways on AWS.
Other posts in this series:
Intro
The first AWS deployment we will cover will be the deployment of a simple Scikit-learn model done completely from one’s local computer using the AWS pre-build Scikit-learn Estimators.
Before we dig into the steps, it’s important to understand the basic concept of how AWS Sagemaker trains and deploys models. If you are unfamiliar with Docker, I recommend reading their overview docs here. Without going into too much detail, AWS essentially uses Docker containers to store a model’s configuration, train the model, and eventually deploy the model. By encapsulating ML models within Docker containers, AWS is able to simultaneously provide a set of preconfigured Docker images for common ML frameworks, while also allowing for complete customization.
Luckily for us, in 2018, AWS added Scikit-learn to its list of supported frameworks. Thus, we can quickly deploy our model using a pre-configured Docker container. As you’ll see below, we will create a training script and execute it inside the container using the AWS Scikit-learn Estimators. So, without further ado, let’s get to it.
You can find the code from this post in the repo I started for this blog series here.
Step 1: Account Setup
Before we begin, we have to set up an AWS account. If you already have an AWS account, then you can skip to the next step. Otherwise, let’s get you signed up for AWS and their 175 (and continually growing) services. To start, navigate to this page to create your free AWS account and get 12 months of free tier access (which will be plenty to complete the code in this post).
Step 2: AWS CLI and Pip Packages
Once you have registered for AWS, we also need to download the AWS command-line interface (CLI) so we can work on our AWS account from our local device. AWS does a good job of explaining how to install, configure, and use their CLI.
We will also need to install a couple of python SDKs in order to access AWS in our python scripts. Specifically, we will need the Boto3 to handle data upload and Sagemaker to create our Scikit-learn estimators. Run the pip commands below to install them.
pip install boto3pip install sagemaker
Step 3: Data Set-Up
In order to focus on the deployment of the model and avoid getting caught up in the weeds regarding data cleaning and model tuning, we are going to be training a simple logistic regression model on the Iris dataset. You can download the dataset here and save it to your local device.
We will use the code block below to strip a few samples from the dataset for testing out API calls once we have deployed the model, and save the data into two new files called train.csv and deploy_test.csv.
Note: We are disregarding ML train/test set guidelines. In practice, you should always be splitting your data into a train/test set. However, for this tutorial, we are just focusing on deploying the model and will check how the model predicts the few points we saved in deploy_test.csv by making an API call at the end.
In order for AWS Sagemaker to access this data when it comes time to train the model, we must upload the data to our AWS account by creating a new Simple Storage Service (S3) bucket and adding our training data to it. To do this, we will be using the boto3 SDK we installed above to create an S3 bucket in a specified region and then save our train.csv file inside a train/ folder in that bucket. Click here for more information on S3.
Note: In order for Sagemaker to access the data, the S3 bucket and Sagemaker session need to be in the same region. And given that Sagemaker is only available in certain regions, be sure to select a region for your S3 bucket from the regions listed here.
To double-check that the above scripts worked correctly, you can navigate to the AWS S3 Console, login, and see if the bucket we created above is there. If it’s there, we are ready to move on to the meat of the work and create our model!
Step 4: The Model Script
In order to deploy a model to AWS using the Scikit-learn Sagemaker SDK, we first have to create a script that tells Sagemaker how to train and deploy our model. While much less work than creating our own Docker container to deploy our model, the SDK does require us to stick to rather strict guidelines.
First off, we will import all the packages we will need and create a couple of dictionaries to convert the target labels from text to numbers and vice versa.
After these imports, we need to wrap the training steps within a __main__ function. We will also include a line to save the model at the end. The SKLearn Sagemaker SDK will run this __main__ function to train a model on the data we pushed to the S3 bucket above and then save the model so that it can be used later when we want to deploy it.
The __main__ function is the only required function. However, we have the ability to modify a number of additional functions that determine how the model will handle API calls once it is deployed. These optional functions are:
model_fn: specifies from where and how to load the model that is being deployed. input_fn: formats the request body sent to the deployed model into a format that can be fed into the model. predict_fn: uses the deployed model loaded by model_fn and the data formatted by input_fn to make predictions. output_fn: reformats the predictions made by predict_fn into the final format that is returned as the response to the API call.
The Gist below shows these optional functions for our model. The comments above each function provide a bit more information on each. For even more information on these functions and their default behavior see here.
Step 5: Create IAM Sagemaker Role
Unfortunately, I lied a bit earlier when I said we were going to do ALL of this from our local computer. If you do not already have a Sagemaker IAM role, we are going to need to navigate to the AWS IAM Console and create a new role. Start by clicking here and logging in. You should see a screen like the one below:
From here, select Sagemaker as the service and push Next.
Skip through the next two pages by selecting Next, until you see the following screen:
On this page, just create your own Role name* and (optional) Role description before pressing Create Role. Just be sure to remember what you named your role, as we will be needing it in the next step!
Step 6: Deploy the Model
With the hard work out of the way, let’s deploy your model. Luckily, with AWS SKLearn class, this only takes a few lines of code. Just make sure that the entry_point path points to the script we saved in step 4 and the role variable is the Role name* you created in step 5. In this snippet, we also specify instance_types for the model and the deployed endpoint. The instance_type specifies how much computing power we want AWS to allocate for our services. Obviously, the more power the more cost, so for this example we use small instances. Check here for a list of all the Sagemaker instance types available.
Note: the SKLearn() constructor has a number of optional arguments you can add to configure the Scikit-learn framework_version, hyperparameters, etc. Click here for more info.
Step 7: Test the Endpoint
To test the endpoint, we will feed send a request with the data samples we saved to deploy_test.csv way back in step 3. In order to send a request to the deployed endpoint, we first need to get our test samples into a format that the model can parse (i.e. a format that can be interpreted by the input_fn function we defined in aws_sklearn_main.py). Since we configured our model to understand requests with multiple samples were each sample’s features are separated by a “,” and each individual sample is separated by a “|” we format our request_body into a format like below:
'147,6.5,3.0,5.2,2.0|148,6.2,3.4,5.4,2.3|149,5.9,3.0,5.1,1.8'
From here, we use boto3 to create a Sagemaker session which will allow us to interact with our deployed model. In order to call our Sagemaker endpoint, we use the invoke_endpoint function. For this function, we must specify an endpoint, a conent_type, and a body. There are also a number of optional arguments you can read more about here. In our case, we will pass the endpoint that we printed out in the previous step, the content_type ‘text/csv’, and a string formatted to look like above. Finally, once we have run this method and received a response, we subscript into the body of our response and read what is returned.
If the expected response matches what was actually returned by the model, you have successfully trained and deployed a working Scikit-learn model on AWS!
Step 8: Clean Up Resources
In order to avoid any AWS charges, be sure to clean up your resources. You can quickly terminate your model’s endpoint by uncommenting the final line in Step 6 and running it.
aws_sklearn_predictor.delete_endpoint()
While this will delete the endpoint, to totally purge your AWS account of all of the resources you used in this tutorial complete the following.
Open the Sagemaker ConsoleIn the side-bar menu under Inferences, delete the resources created on the Models, Endpoint, and Endpoint configurations tabs.Open the S3 Console and delete the bucket we created.Open the IAM Console and delete the role we created.Open the Cloudwatch Console and delete all of the /aws/sagemaker logs.
Open the Sagemaker Console
In the side-bar menu under Inferences, delete the resources created on the Models, Endpoint, and Endpoint configurations tabs.
Open the S3 Console and delete the bucket we created.
Open the IAM Console and delete the role we created.
Open the Cloudwatch Console and delete all of the /aws/sagemaker logs.
Review
Overall, while the learning curve is a bit high, once you are able to navigate your way around the AWS services and understand the Sagemaker principals, AWS SKLearn Estimators become an incredible tool to quickly deploy Scikit-learn models.
Benefits:
Very little configuration needed, but lots available
Plethora of documentation
Can deploy model with just a few lines of code
Drawbacks
The large initial learning curve
Several AWS services to understand and work with
Documentation can be difficult to find
Other Blog Posts in this Series: