How to Deploy a Machine Learning Model on AWS EC2

Raghav Agrawal Last Updated : 12 Oct, 2024
8 min read

This article was published as a part of the Data Science Blogathon.

Introduction

AWS is a cloud computing service that provides on-demand computing resources for storage, networking, Machine learning, etc on a pay-as-you-go pricing model. AWS is a premier cloud computing platform around the globe, and most organization uses AWS for global networking and data storage. The article is for those Machine learning practitioners who know the model building and even they have deployed some projects on other platforms but want to learn how to deploy on major cloud platforms like AWS.

In the article, our main aim is to learn deployment over AWS, but we will walk through each step from the development of the Machine learning model to deployment over AWS from scratch.

Build a Machine learning Model

The very first step is building a model. Our main aim is to learn deployment, so I have taken a simple dataset and applied an SVM algorithm that gives a very good accuracy even after trying Random forest and decision trees. If you want, you can optimize the model accordingly.

Dataset description

We pick a very simple dataset about student placement prediction which is a binary classification problem. The dataset contains input columns such as student CGPA, IQ, and Profile score, and based on 3 input columns you have an output column that indicates whether the student will be placed or not in binary form as 0, 1. You can download the dataset using this GitHub link.

AWS EC2

Data Preparation for model building

Data is simple, and it does not contain any missing values or data is not messy so to save time and learn deployment we load the data and prepare it for model creation.

import numpy as np
import pandas as pd
#Read the Data
df = pd.read_csv('https://raw.githubusercontent.com/Raghavagr/Student-Placement-Predictor/main/students_placement.csv')
#look over sample data
print(df.sample(5))
#separate independent and dependent columns
X = df.drop(columns=['placed'])
y = df['placed']
#train-test-split
from sklearn.model_selection import train_test_split
X_train,X_test,y_train,y_test = train_test_split(X,y,test_size=0.2,random_state=2)

Model training and testing

We first import the SVM model using the scikit-learn library, train it on training data, and test it on test data. After that, we apply the accuracy score as a metric to get the performance of the model which is 93 percent.

from sklearn.metrics import accuracy_score
from sklearn.svm import SVC
svc = SVC(kernel='rbf')
svc.fit(X_train,y_train)
y_pred = svc.predict(X_test)
print(accuracy_score(y_test,y_pred))

Export the Model

We have to deploy the model to the website, and for this first, we need to convert the model into a certain file. For converting the model into a file, we can use Pickle or Joblib library. It implements protocols for serializing and de-serializing the python objects so that by de-serialize them, you can use them for different purposes. We are using Pickle at this time to dump the model in a pickle file.

import pickle 
pickle.dump(svc,open('model.pkl','wb'))

First, we have to import the Pickle module, and there is one function named dump where we have to specify two things: objects to be dumped and which file we want to convert.

Build a Flask Website to Serve as a Model

First, we have to create one project folder where you have to keep the Pickle file. You can open the folder in any python code editor of your choice, whether it can be Pycharm, Spider, or VS code. If you do not know anything about Flask, then you can refer to my Basic Flask article on Analytics Vidhya. We will design a form where we will accept student details, and as students click on submit button, our flask app will accept the details at the backend and pass the details to a model where the model will generate a result that we will display to the student at frontend whether he will get the placement or not.

Designing HTML Form

Create a new folder in your project folder as templates and create a new HTML file in it where we will design an HTML form. I hope that you are a little bit familiar with HTML and if not, then you can refer to any beginner article on the web. We will create 3 input tabs to accept details like student CGPA, IQ, and profile score. And create a button to submit a form that states that on submitting the form, the request is POST and define the action where the form will redirect after submitting that we will handle from the flask app.

Student Placement Predictor

    {% if result %}
{{ result }}
    {% endif %}    
        
     
        

Build a Flask App

Create a new python file named an app where we define a new Flask app and load the model dump in the pickle file. As we run the server, the app will serve the “/” URL where we have to display the home page which is our HTML Form. Now after submitting it will create a POST request that we have redirected to a “/predict” URL, and at that time we will accept the form data, pass it to the model, and display output using JINJA.

If you open the command prompt and direct to the current working directory and run the below command, then your server will start running and get a localhost URL that you can open in your browser, and the app is running.

python app.py

Deploy ML Flask Web APP To AWS

Now the app is running well but it is currently running on a local system means we can only use this by running a server so to make it available for public users we have to deploy it on the cloud.

Prepare requirements file

The first thing is you should have a requirements file where we specify all the dependencies or libraries that we have used to develop the project. Create a new text file, and the name should be requirements.txt and copy the content as given below.

flask
numpy
sklearn

Create AWS account

Visit the AWS site and create a free-tier account that is valid for 12 months, and you can use popular AWS services to a certain extent. But after using each service, you should terminate and delete all the services to avoid any kind of bill. To create a free-tier account, you must have Mastercard, or Visa Debit card, or a credit card. When you first create an account, it just cuts 2 INR and returns after 3-5 business days for background verification.

Create EC2 Instance

EC2 stands for Elastic compute cloud that provides scalable compute capacity in AWS Cloud. You can also use AWS Lambda and AWS Elastic Beanstalk for deploying ML models, but EC2 is very old. You can install software on EC2, and easy to deploy your application. It is nothing but you take a server on the cloud on rent. Open EC2 Dashboard in your management console through the search tab or services. As you open the EC2 dashboard, you will observe how many instances are running. Click on the Launch Instance tab to launch a new EC2 Instance. Now by configuring 6-7 steps, you can launch your new Instance.

Step-1) Choose an Amazon Machine Image (AMI)

The first step is to choose an operating system, for instance, where you have many options. We will choose the operating system that is free-tier eligible, so click on Ubuntu and select the Ubuntu operating system which is free-tier eligible. You can also try any supported version of Linux.

Step-2) Choose Instance Type

The step is crucial, and you do not need to select anything in the step because the default option is already selected as t2.micro, which supports a free-tier account.

step-3) Create Key-Pair

To secure your instance, we have to set a password and key-pair a way to achieve this. Click on create new key pair and enter any name of your choice without space. And click on create-key-pair.

Step-4) Network settings / Configure security group

Under this, we have different network settings but keep all the settings as default and move forward. Your AWS EC2 instance is private to you and secretly kept unless you define to accept the network traffic from what kind of source, so we create a new and default security group.

step-5) Configure storage

Free-tier eligible customers can get up to 30 GB EBS, and a minimum of 8 GB is allotted to you. We have set it to 8GB which is the default.

step-6) Review and Launch

At last, you have a summary of all your configurations so you can have a look and launch an EC2 instance. After that, if you visit the instances tab then our EC2 instance is successfully and running.

Download and install Putty and WinSCP

WinSCP is used to upload your project files to the server.

Putty is a remote client where by using the SSH key, you take access to your machine. You can open an EC2 command prompt in Putty and install the project dependencies and libraries. To download the putty, visit the official site and download the installer file as per your OS.

Upload the website

Open the WinSCP, and you need to provide the hostname and the key-value pair we have created above. If the file is in PEM format, it will ask you to convert it to PPK format and click on OK. Click on login, and when you are successfully logged in, then you will have 2 parallel screens, where first displays the files of your local system, and the second one is your EC2 instance in a local system. Open the desired project folder and paste each file on the right side (Server folder).

Before Pasting all the files, we need to specify the port and host in the python app file because currently, the app was running in the local file system, but now it will run on the cloud, so edit the suggested changes.

if __name__ == "__main__":
    app.run(host='0.0.0.0', port=8080)

Install Python and Libraries on Server (EC2 Instance)

We will need Putty for this, so open the Putty using the same key-value pair. so one by one, you have to install each library on the EC2 server.

Install PIP using which we can install all the python libraries on AWS EC2. As we successfully install PIP, we can run the pip command to install all the required libraries specified in the requirements file.

sudo apt-get update && sudo apt-get install python3-pip
pip3 install -r requirements.txt

Start the server and Test the website

In the Putty command prompt, you can run the app file using the command python3 app.py so you will get the URL. Visit the SSH client in Instance Dashboard and copy the AWS EC2 instance address and paste it into the browser. At last, write .8080 port and press enter.

The website will only run till when you run the command in Putty. So to stop or avoid this flaw, you need to run one command, a JNU multiplexer in a complex terminology that copies the current screen on a server. The command is given in the below snippet, so copy and run the command in a terminal, and Now until you press CTRL + C, the server will keep running. In the following article, we will also learn how to download the production environment. For now, I hope this article has provided you with an in-depth overview of model deployment over AWS EC2.

screen -R deploy python3 app.py

Conclusion

In this article, we have learned about deploying a Machine learning model on the AWS cloud using a top-rated AWS EC2 service. Let us discuss the key takeaways from that article that you should remember.

  1. AWS is a cloud-service platform that offers on-demand cloud services on a pay-as-you-go pricing model where you have to only pay for the time and storage of service that you have consumed.
  2. AWS provides a 12-month free-tier account in which you can use some services up to a certain limit of storage.
  3. AWS provides different serverless platforms like AWS Lambda or Beanstalk that can also be used to host a machine learning website.
  4. To develop a Machine learning model or to perform data analytics, AWS EC2 offers separate tools for a specific use case that you can check out under the services tab.
  5. Flask is a micro web framework of python that you can use to build small data apps or static websites.

Thank You Note

  • I hope it was easy to cope with each step discussed in the article. If you have any queries, please post them in the comment section below or connect with me.
  • Connect with me on Linkedin.
  • Check out my other articles on Analytics Vidhya and crazy-techie

The media shown in this article is not owned by Analytics Vidhya and is used at the Author’s discretion.

I am a software Engineer with a keen passion towards data science. I love to learn and explore different data-related techniques and technologies. Writing articles provide me with the skill of research and the ability to make others understand what I learned. I aspire to grow as a prominent data architect through my profession and technical content writing as a passion.

Responses From Readers

Clear

Shanmugam T
Shanmugam T

Hi, In the last step, when I lauch the website , I am getting the following error. 172.31.20.72 took too long to respond. Even I created the security group to allow https connections. what will be the issue to lauch the site?

Congratulations, You Did It!
Well Done on Completing Your Learning Journey. Stay curious and keep exploring!

We use cookies essential for this site to function well. Please click to help us improve its usefulness with additional cookies. Learn about our use of cookies in our Privacy Policy & Cookies Policy.

Show details