Deploy your Deep Learning model with Flask, on AWS EC2 Instance
Tags: aws, tutorial, python, deeplearning
Published: 2021-12-18 05:08:45

Amazon Elastic Compute Cloud (Amazon EC2) is a web service that provides secure, resizable compute capacity in the cloud. It is designed to make web-scale cloud computing easier for developers. AWS Free Tier provides 750 hours per month of Linux, RHEL, SLES t2.micro or t3.micro instances and 750 hours per month of Windows t2.micro or t3.micro instances. 

Any individual can Sign Up and use these services for free for 12 months. Your services will be activated after 24 hours of signing up.

In this blog, I have demonstrated deployment of trained deep learning models with your Flask app, on AWS EC2 instances. 

I deployed a trained TensorFlow Deep Learning Flask API step by step, from scratch, along with setting up all the necessary resources and packages. 

The model is based on Transformer Neural Networks and was trained in Google Colab’s GPU runtime.

So let’s get started!

## Step 1: Setting Up your AWS EC2 instance

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/krjc4kwr1yrco25e8ml0.png)

In the services dropdown, click “Compute”. This opens another right panel, as shown in the above pic. Click on the EC2 option.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6h1o7s4ihy8qumgqjtnk.png)

You will see all your ec2 type instances running. Click “Launch Instances” to launch a new instance.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/53ayhnzd1yz10bbo72kg.png)

Choose your Ubuntu machine, here I have chosen Ubuntu Server 20.04 LTS, SSD Volume Type ,64-bit (x86).

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h31pxww8e6pz9vvsqmws.png)

Choose your instance type as t2.micro. Obviously, this is the only choice available for Free Tier. We’ll be modifying the memory partitions according to us, and increasing the internal storage.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s2tx98mg15955y8h2rmy.png)

“Review and Launch” the instance.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kohpxbny9zzcafwnuk5r.png)

“Create a new key pair” to connect to your instance securely and remotely with SSH protocol. Name it and download it to the same folder with your Flask app.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8loh9wix04lnqj5r4kd9.png)

You’ll see your instance up and running. You can rename it as you want. I renamed it as “test_deploy”.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6oaavdcv370wkl8tw2bm.png)

Now, we’ll be creating a security group for our instance.

A security group acts as a virtual firewall for your instance to control inbound and outbound traffic. Here, in the instances, scroll right and you will see the current default security groups for your instance.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vuuzr99i4snoikggeqcw.png)

Go to the Security Groups from the left panel, you see all the currently available Security Groups. 

“Create security group”. Give it a name, a description and click on “Add rule” to add your inbound rule.

In my instance, I have set the type to “All Traffic” to allow access to all the available ports, and Source to “Anywhere with IPv4” which sets the Classless IP as 0.0.0.0/0 to allow any address. You can set these according to your requirements and what ports you want to expose.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w817uro9ygpnzxx9p93d.png)

Going back to the instances, 
right-click on your instance → Security → Change Security Groups.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i38p3vxukmj0o7gd78nq.png)

Click on the search bar, you would see your created security group. Click on your security group, click on “Add Security Group”, and finally click “Save”.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j2q20lveskvk4nzy4qcy.png)

Again, going back to the Instances, you will see your security group applied to your instance.

## Step 2: Installing necessary software for SSH remote connection

We’ll not be typing each command in the terminal, instead we will be using light software like PuTTY, PuTTYgen (for PEM to PPK conversion), and WinSCP to establish SSH (Secure Shell) remote control.

Download Links: 
PuTTYgen: https://the.earth.li/~sgtatham/putty/0.76/w64/puttygen.exe
PuTTY: https://the.earth.li/~sgtatham/putty/0.76/w64/putty.exe
WinSCP: https://winscp.net/download/WinSCP-5.19.5-Setup.exe

Start the puttygen.exe and load the .pem file from your directory. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pzpqrjxxthj5nbl2vddd.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cotmnnb8tt1coylprs67.png)

Provide the generated .ppk (putty private key) file with a name and save it in the same directory.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r7y0agdbjgwiueu3i5wz.png)

Now, start WinSCP to establish SSH remote control.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ysww6tob2izwec5t6y90.png)

Copy the Public IP address for your instance and paste it in the “Host Name” field. Click the “Advanced” option, select Authentication beneath SSH and load the previously generated .ppk file.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rlpbiafoj3piph635e7j.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3htm6p3nebx9xqjim09o.png)

Enter User Name as “ubuntu”, click save, and then click login.

Congo!! Now you have successfully remote controlled your AWS EC2 instance. 

The left panel now shows your local directory, and the right panel shows the ubuntu remote directory. 
Transfer your program files just by dragging and dropping them into the right panel.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dlc4m76lgemm5fx7csnz.png)

Now, it’s time to start the remote machine’s terminal. Start putty.exe.
Paste your instance public IP in the “Host Name” field.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/imx4qafsqksapnaslp2f.png)

Go to SSH → Auth in the left panel, and load the .ppk file again.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m4s0yayhjgfd9m5hp9ek.png)

Click the “Session” option in the left panel to come back, enter a name for your session and click “Save”.
I entered the name as “test_deploy”

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3554jl7vzgachn2colxz.png)

Double-click the name or click “Open” to start the terminal.
Login as “ubuntu”

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ft2nqyxmh4ticav56ph1.png)

## Step 3: Terminal

To view the files, and directories present in the root directory

```
ls
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pb6ih8ycyfnjpypupx4c.png)

There are no dependencies already installed in the instance. Hence, we’ll be getting all the necessary updates followed by installation of python3.

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

A prompt asks for “Do you want to continue? (Y/n)”, press ‘y’ and continue.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r6x1wcckw85gch8v2r7c.png)

I had prepared a requirements.txt file with all the dependencies and packages my application required. 
To install those, simply run:

```
sudo pip3 install -r requirements.txt
```

After the installation of various libraries, we might not be left with enough space, needed for those heavy deep-learning models, or any other files. 
We go back to our AWS Management Console,
 click checkbox near your instance → Storage tab → Click on volume id

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yeplp3sbkcfw01wl4r2a.png)

Right click on volume → Modify Volume

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bm1w85cg53ck586k7qp2.png)

Change the volume as per your requirements. Here, I changed it to 20GB, maximum being 30GB. 
Click “Modify”.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ppl71u3j4x5tks0rlr0d.png)

Congratulations! 
But wait, the partition hasn’t been extended. Let’s get back to the terminal…

To check whether the volume has a partition that must be extended, use the lsblk command.

```
lsblk
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/308v62ojrxp8fwdgwglz.png)

Here, notice that dsk space is 20GB but our instance is still utilizing the 8GB partition.
Check Space usage:

```
df -h
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/56u68dp0y3wwn7z4vgan.png)

To extend the partition  : 

```
sudo growpart /dev/xvda 1
```

Check again using lsblk

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ap5hbea29agpa2fcv9sb.png)

Now the memory storage has been increased successfully!

I had to install Tensorflow, but the “grpcio” package was causing some issues during Tensorflow's installation, so I installed “grpcio” independently.

First update pip3:

```
sudo pip3 install -U pip
```

Installing grpcio==1.36.1

```
sudo pip3 install grpcio==1.36.1
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z2pee0s670zg44c7rn82.png)

Now installing Tensorflow:

```
sudo pip3 install tensorflow
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w2ncb84oigk7114d7p12.png)

Ohh crap! Process killed!
Possible reasons: might have to disable cache due to less space on the hard drive. Still?!!

Thanks to StackOverflow! Execute:

```
sudo pip3 install tensorflow --no-cache-dir
```

## Step 4: Running the Flask App

In your app, modify the app.run() as follows.

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

Here, host=’0.0.0.0’ means, any address would work, no such specification. 
And, port=80 (HTTP), anything works, 22 (SSH), 443 (HTTPS)...
Remember how we modified the Inbound Rules for “All Traffic” while creating a security group. This is how it works!

Run your app:

```
sudo python3 app.py
```

Now, you have your Standalone server running your API

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3ehnp41tzsx3qcqidwp0.png)

Visit the Instance IP appended with the port number:
Eg: 3.134.92.137:80 
My instance IP

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uzj9hu3y0uzhcdjto73g.png)

## Step 4: Session

Now, we’ll implement terminal multiplexing, in simple words, to keep the current session running even after we close our terminal.

Stop your currently running app: Ctrl+C
Again, execute:

```
tmux
sudo python3 app.py
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n53k5cxxgacp1jkehz1u.png)

All done! Close your terminal, let your PC rest, and still flex your deployed Flask API from any device, any location.

Remember this is a development server for testing/demonstration, and not a production deployment. You can use gunicorn for the same. Gunicorn is a Python WSGI HTTP server that many developers use to deploy their Python applications. This WSGI (Web Server Gateway Interface) is necessary because traditional web servers do not understand how to run Python applications.

To end the terminal multiplexing, come back to your saved machine, using PuTTY.
Run:

```
tmux attach
```

Once inside the session:
Stop the app and run:

```
tmux detach
```

Terminal multiplexing has ended. Now you will not be able to run your app after closing the current session.

## Wrapping Up

_Kudos!! A Deep Learning Flask API deployed on your AWS EC2 instance!_
_That’s all for this lengthy blog._
_I hope you found this insightful!_

_Inputs, thoughts, improvements, suggestions are most welcomed!_
.
.
.
.
.
.
.
Cover Image: https://www.google.co.in/url?sa=i&url=https%3A%2F%2Fwww.brainvire.com%2Famazon-web-service%2F&psig=AOvVaw2xnyUGPMUMrb_mWlSXQzn5&ust=1639846510264000&source=images&cd=vfe&ved=0CAsQjRxqFwoTCICUsamm6_QCFQAAAAAdAAAAABAV