ML model deployment with Flask using AWS EC2 - PART II

Hrisav Bhowmick
Analytics Vidhya
Published in
5 min readMay 26, 2021

In Part I, we have seen how to build the model, design the HTML/CSS page and finally run it on localhost. Now in this Part II, we will show how to deploy it on webhost (using Amazon EC2) in 10 easy steps.

The pre-requirement is you must have created the following after going through Part I:

  • ‘loan.py’ (this will build the model and generate the pickle file)
  • ‘loan_approval.pkl’ (pickle file)
  • ‘index.html’ (frontend file)
  • ‘app.py’ (backend file, constituting the Flask app)
  • ‘requirements.txt’ (have a list of all libraries required to run the project)

1. Creating Instance on EC2

  • Go to EC2 Dashboard.
  • Click on Instances.
  • Click on Launch Instances.
  • In step 1, choose an Amazon Machine Image (AMI) - select Ubuntu Server 18.04. Click Next.
  • Choose instance type in step 2 — ‘t2.micro’. Click Next.
  • Ignore step 3, 4 and on step 5 click Add Tag. Key: ‘loan’, Value: ‘loantag’. Click next.
  • In step 6, change security group name to ‘sgloantag’. Click Review and Launch.

2. Launching Instance

  • Click Launch. Create key pair name - ‘kploantag’. Download key pair. Save the PEM file in a new folder (name: deploy) on Desktop. Click Launch Instances.
  • Go to view instances and add a name for the instance - ‘loan’. It should show Running.
  • Now click on instance id. Go to security tab. Click on Security Group link. Edit inbound rules -> Add rule: Custom TCP | Port : 5000 | Source: 0.0.0.0/0 | Save the rule.

3. Getting directory ready

  • Go to the folder (name: loanproject) containing project files. It should contain: dataset, app.py, requirements.txt, pkl file, templates folder having the html file. Make a zip file of the project folder as loanproject.zip.
  • Go to the folder (name: deploy) where PEM file was saved. Copy loanproject.zip and paste it into the same folder where PEM file lies.

4. Checking connection links

  • Go to the instance page on EC2, select instance and select Connect. Below screen will show up.

Last line of the above image will be used to connect to the server.

Fourth line will be used to view the project UI on the browser.

5. Checking instance summary

  • Click on instance name and check if instance summary details are fine (like shown in the figure).

6. Connecting to Instance

  • Open Powershell from that folder by going to address bar and typing PowerShell and after it starts, run the following commands:
> scp -i kploantag.pem loanproject.zip ubuntu@ec2-18-191-25-39.us-east-2.compute.amazonaws.com:

The above command will send our files to the webserver. We could have used WinSCP/FileZilla/PuTTY also instead of scp command.

> ssh -i "kploantag.pem" ubuntu@ec2-18-191-25-39.us-east-2.compute.amazonaws.com

This will help to connect to the server.

7. Getting files on the server

Run the following commands.

> dir> sudo apt install zip unzip> unzip loanproject.zip> cd loanproject

8. Getting environment ready

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

The ‘requirements.txt’ contains all the libraries required to run the project.

If while running the last code any error shows up, check which library failed to install and install them manually like below:

> pip3 install numpy

In my case, I had to install flask and sklearn also manually along with numpy. It was mainly due to version compatibility issue.

9. Running the script

  • Finally run the python file app.py:
> python3 app.py

The server is up and running. Had it failed, it would have thrown up error.

10. Running on browser

  • Go to the SSH client page by connecting to instance, and copy the link to run. This web address will be different for different users.
  • Go to browser and paste the link which we got after connecting to instance. Note that we need to append port at the end -> :5000/

Paste and run on browser:

http://ec2-18-191-25-39.us-east-2.compute.amazonaws.com:5000/

Here’s the web app running and the user entering some input:

And here’s the outcome on the bottom part:

Conclusion

So finally we have successfully deployed the app. Once we input the values and click on Submit button, the screen will show the output if a loan will be sanctioned for the user or not.

Thanks for reading my article! If you like my article do 👏.

For the full code of this project, check the Github link.

Connect with me on Linked-in for more updates or if you are stuck anywhere.

--

--

Hrisav Bhowmick
Analytics Vidhya

A machine learning enthusiast, eager to solve real world problems.