DEV Community

Cover image for How to Deploy the Machine Learning Model on AWS EC2 Instance?
chetan jain
chetan jain

Posted on • Updated on

How to Deploy the Machine Learning Model on AWS EC2 Instance?

First of all the Framework which I used is the flask, so first thing check your model whether it is running or not in your local environment.

Run anaconda prompt and go on the appropriate path of your model and type

python model.py

Then the output must look like this

Local running

Now after verifying on a local machine its time to go on an AWS management console.

  • Open EC2.
  • Then Click on Running Instances.
  • Then Launch Instance.
  • Then Search Ubuntu Because it is free so you can use that for your personal projects. but In companies, they use premium server so that so many things are pre-installed they will not do it manually but we have to do it manually.

Then it might look like this

Ubuntu image

After that

  • Choose a t2-micro version of Ubuntu server for the free tier.
  • Then click on review and launch.
  • Again click on launch. Then a pop-up will show up like this.

Ubuntu image

Here you will do

  • Create a new key pair and give it a name and download that keypair and put inside your project folder.

That ''' .pem ''' extension file will help us to interact with the server which is running on EC2 instance.

  • Now click on Launch Instance.

Now you need to download 3 software

  1. Putty - A ssh client to interact with the server.
    Download here

  2. Putty gen - For converting that ''' .pem ''' file into private key.
    Download here

  3. WinSCP - For sending your all-important files to the server.
    Download here

Now open putty gen

  • Load yours.pem file.
  • Generate your private key. (Convert .pem to .ppk file)
  • And save that on your project folder.

Putty gen

Now copy your hostname from EC2

  • Go to EC2.
  • Open Running Instances.
  • Right-click on Instance which we are working and click on connect.

hostname

  • And Copy that.

Now open your winSCP

  • Paste your Hostname in hostname column.

Winscp

  • Click on Advanced.
  • Now click on Authentication and browse your private key (.ppk) file.

Authentication

  • Now click on log-in and press yes on the pop-up.
  • Now drag your appropriate files from your local machine to Ubuntu server.

Now open Putty

  • Paste your Hostname.
  • Now click on ssh expand it and click on Auth.
  • Now browse the private key and save the session.

Putty

  • Now click on open.
  • Now type Ubuntu in login as.
  • Now you are in the ubuntu server panel.

Here now you have to update and install pip and appropriate libraries respectively to run your project.
run this command as you enter inside your server terminal.

sudo apt-get update && sudo apt-get install python3-pip

Now this command will manage your all issues regarding '''pip3 installer'''

Make sure in your

app.py or model.py

your host and port must be as is this.

host='0.0.0.0' and port=8080

Now in the Ubuntu server

pip install -r requirements.txt

As your installation of the libraries completed.
Now run

Python3 app.py or model.py

Now open the browser

  • Go to search bar.
  • Now paste your HostName followed by port 8080.

model

Like this.
Now enjoy, Your model is successfully deployed on EC2 Instance.

Extra tip, For running the program even after the terminal is closed or shut down the system.

nohup Python3 app.py or model.py &

Thank you so much for reading and visiting this.

Top comments (0)