How to Deploy the Machine Learning Model on AWS EC2 Instance?
Tags: webdev, productivity, machinelearning, aws
Published: 2020-03-21 23:20:27

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](https://dev-to-uploads.s3.amazonaws.com/i/vla69mpvh5up3ctdf52y.JPG) 

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](https://dev-to-uploads.s3.amazonaws.com/i/7l3mljspffnue1pujfqd.JPG)

#### 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](https://dev-to-uploads.s3.amazonaws.com/i/7kuol5d4c5jyhhojktt0.JPG)

#### 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](https://www.chiark.greenend.org.uk/~sgtatham/putty/latest.html)

2. Putty gen - For converting that ''' .pem ''' file into private key.
[Download here](https://puttygen.software.informer.com/0.6/)

3. WinSCP - For sending your all-important files to the server.
[Download here](https://filehippo.com/download_winscp/)

#### 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](https://dev-to-uploads.s3.amazonaws.com/i/vgumkx1nohr6pqv0p4ru.JPG)

#### 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](https://dev-to-uploads.s3.amazonaws.com/i/bzwvicc508lflj81wwkj.JPG)
+ And Copy that.


#### Now open your winSCP
+ Paste your Hostname in hostname column.

![Winscp](https://dev-to-uploads.s3.amazonaws.com/i/ri3em59bni4nq8c4hwes.JPG)

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

![Authentication](https://dev-to-uploads.s3.amazonaws.com/i/gvpyx5cfx16njpyoxq71.JPG)

+ 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](https://dev-to-uploads.s3.amazonaws.com/i/rvq344hj4yf636dk2rbt.JPG)

+ 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
``` python 
pip install -r requirements.txt
```
As your installation of the libraries completed.
Now run
``` python 
Python3 app.py or model.py
```

#### Now open the browser

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

![model](https://dev-to-uploads.s3.amazonaws.com/i/7b05d50js47f72qeewni.JPG)

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.

``` python 
nohup Python3 app.py or model.py &
```

Thank you so much for reading and visiting this.