Build and Deploy a Machine Learning Model using AWS and API’s

Subham Sarkar
Nov 14, 2019 · 6 min read


Deploying machine learning models remains a significant challenge.Even though pushing your Machine Learning model to production is one of the most important steps of building a Machine Learning application , there aren’t many tutorials out there showing how to do so. Therefore in this article, I will go over how to productionize a Machine Learning model by building a normal website using the Flask web micro-framework.

Introduction to Amazon Web Services (AWS)

What is AWS? — Amazon Web Services(AWS) is a cloud service from Amazon, which provides services in the form of building blocks, these building blocks can be used to create and deploy any type of application in the cloud.

Introduction to Flask

What is Flask?

Flask is a web framework. This means flask provides you with tools, libraries and technologies that allow you to build a web application. This web application can be some web pages, a blog, a wiki or go as big as a web-based calendar application or a commercial website.

  • jinja2 which is its template engine

7 simple steps to productionize your Machine Learning Model In AWS:

  1. Build a model on your local machine and store the model and other key model related variables in pickle files( .pkl files )
  2. Launch a micro instance on AWS.
  3. Connect to the AWS instance.
  4. Move the files to an AWS-EC2 instance
  5. Install all packages needed on the AWS instance.
  6. Run on the AWS box.
  7. Check the output in the browser.

Software needed: (Anaconda)

To download Anaconda please click the links given below :

Data Acquisition

As an example I have taken the Amazon Fine Food Reviews Dataset. The Amazon Fine Food Reviews data-set consists of reviews of fine foods from Amazon.

Dataset Overview

Number of reviews: 568,454
Number of users: 256,059
Number of products: 74,258
Timespan: Oct 1999 — Oct 2012
Number of Attributes/Columns in data: 10

Let’s run the code in our Local Machine

Let me first show you the folder structure and files inside:

Command Prompt snapshot:

Webpage snapshots:

For Positive Review :

Review Input
Prediction = Positive
Review Input
Prediction = Negative

Let’s understand the code

  1. index.html : This is used to build a simple web page , where we are providing Review_text as input and on clicking the Submit button we are getting a prediction. Here we provide the name of the function (predict()) in form action tag.

Deploying the application on Amazon Web Server(AWS)

As we know that if we run any application on our local machine other people cannot access it, but if we deploy or host it on cloud servers then we can keep the instance running all the time and people can access it anytime. Thus, now let’s see how to deploy the Machine Learning Model in AWS.

Launch a micro-instance on AWS

Create an AWS account :​,

Login: ​

3. After login:

4. Launch the “ EC2 “ instance
5. Choose the “ Ubuntu Free Tire “

6. Click on ‘Select’
7. Choose “ t2.micro “ free tier eligible

8. Click on “ Review and Launch “

9. Click on ‘Launch’

10. Click on “Download Key Pair” and save the “.pem file” then click on “Launch Instance”

11. You will see this screen, you have successfully launched the an “EC2 “ instance, now we need to launch an “ Flask API “ in it
12. Final step:

13. Select the “Network & security” -> Security groups and then click “Create Security Group”

14. Then add the specific security group to ​network interface

15. Connect to your AWS instance

Follow the steps in the figure below to connect to your instance

16. Move the files to an AWS EC2 instance :

Open Command line and type below command to copy files to instance.

17. Install all the packages required to the AWS EC2 instance

sudo apt-get install python3-pip

Packages needed:

pip3, pandas, numpy, sklearn, beautifulsoup4, lxml, flask, re

18. Run “ “ on the AWS box.

19 .Check the output in the browser.


Productionizing your Machine Learning model is a important part of a machine learning project and Flask can be used to create a website with only a few lines of code.

Where can you find my code?



I hope you enjoyed reading this blog ! Thanks …

Analytics Vidhya

Analytics Vidhya is a community of Analytics and Data…