How to Deploy a Python API to AWS EC2

Deploy your ML model and your API using a modern tech stack.

Photo by Daniel Páscoa on Unsplash

This is my first Medium tutorial, I’ll try to do my best if you find any mistakes please let me know.

In this article I’ll walk you through deployment of CNN (Convolutional Neural Network) model and an API to AWS. This also includes how to create an API using FastAPI and to use AWS EC2.

AWS — Amazon Web Services

Amazon web services is a cloud computing provider that offers a wide range of products on it’s platform https://aws.amazon.com. We’ll be using EC2 Elastic Compute Cloud, is a service where you can rent virtual computers to run cloud applications. It also has a free tier version https://aws.amazon.com/free for 12 months with minimum system specifications.

You can create an account if you don’t have already and sign in to EC2 console https://console.aws.amazon.com/ec2 . To create a Linux based VM click on Launch Instances button available on top right hand side of the window. Choose an image for the machine is going to use, I’ll be using a Ubuntu 20.04 Server image. You can see Free tier eligible mark on some of the images. Select the one suitable for you, my suggestion would be to use a free tier if you use AWS for the first time.

AMI Details — Ubuntu Server 20.04

Free tier comes with t2.micro instances that gives 1 CPU and 1GiB computing power, which will be enough to run a trained CNN model. And next step is to edit/add security groups. Click edit security groups to add new security to our instance.

You will need a SSH access to that VM you can request SSH access by selecting My IP from source. But this access will be only for you. To access our web app from any browser/client we also require HTTP access rule. Hit add rule button and select HTTP from type and anywhere as a source. Our API will be running on port 8000 by default, hence don’t forget to add a new custom TCP rule for that.

Configure Security Groups

First click review and launch, then launch to create a key pair. You will need this key pair for connecting to VM over SSH. Download your key pair and keep it somewhere safe and try to not lose it. As a final step launch your instance.

Connecting to Server via SSH

Open running instance details under instance on EC2 console and click connect to see options to make connections to server. If AWS don’t introduce a new option you may see similar options as I do.

SSH client option gives you all the information how to connect to server via SSH. When you use SSH command be sure that you use the correct file name of the keypair. Hopefully you’ll logged into your instance.

How to connect via SSH

API development with FastAPI

FastAPI is a modern, high-performance, web framework for building APIs with Python 3.6+ based on standard Python type hints. It is hosted on Github https://github.com/tiangolo/fastapi.

It is very easy to use and your API comes with interactive docs available.

Requirements and Cloning API

API that I’m going to use is available on https://github.com/abdullahselek/plant-disease-classification-api, first get the repository in VM. Project based on PyTorch and FastAPI, all the dependencies are available on requirements.txt. Trained model is also available in models subfolder of the project. For more details about how to train model you check https://github.com/abdullahselek/plant-disease-classification-pytorch.

git clone https://github.com/abdullahselek/plant-disease-classification-api.git

It’s time to update the system, install Python3 with pip and venv.

sudo apt-get update
sudo apt-get install python3-pip
sudo apt-get install python3-venv
cd plant-disease-classification-api

Create a Python virtual environment to keep Python modules inside a folder in plant-disease-classification-api directory. This gives us a separation of modules used by different projects.

The venv module is the preferred way to create and manage virtual environments. First activate virtual environment then install module with dependencies. Double check if current file directory is plant-disease-classification-api.

python3 -m venv env
source ./env/bin/activate
pip3 install -e .

For some reason if installing of torch fails or stuck at some point you can try running the command below. Be sure that all dependencies installed by running pip3 install -e . again.

pip3 install torch==1.7.1+cpu torchvision==0.8.2+cpu -f https://download.pytorch.org/whl/torch_stable.html

Final step is to run webserver

uvicorn plant_disease_classification_api.main:app --host 0.0.0.0

Copy public IPv4 DNS from your instance details, append port number 8000 and try to open in a browser. You should see the welcome screen of the API. Click on documentation link to see endpoint and request details.

Welcome to Plant Disease Classification API

You can see request item details if you expand schemas as below. Classification endpoint supports multiple models but for now sample repository has only one trained model. Parameter data is Base64 encoded string value of a plant image.

Request schema

Download test image from https://github.com/abdullahselek/plant-disease-classification-api/blob/main/testdata/916fef78f494c6132246b40eac15f30e.jpg to your computer and write a short Python script to get Base64 encoded string of image.

import base64with open("916fef78f494c6132246b40eac15f30e.jpg", "rb") as f:
data = f.read()
image_data = base64.b64encode(data).decode("utf-8")
print(image_data)

Click try it out button to try endpoint with test parameter we have in hand. API has already one trained model named model_1.pt and we also have Base64 encoded data of the image we have downloaded. Fill the values in request body and hit execute.

Request body

We should get a result which is disease class of test image.

Response body

Improvements

This is a decent solution using modern tech stacks but there are still some missing parts to make it production ready. As you might realize API runs on http, you may want to use SSL certificates with in a subdomain as well as adding some scalability for high input traffic.

Conclusions

Creating a web application using Python is super easy and we have many options where to deploy and make it public to the world. There are also other Python web frameworks available as Flask, Tornado and many more. AWS has a big product range, offers limited free service on some of the products which we can use for learning and prototyping our ideas.

I would like to hear your thoughts, therefore any feedback and constructive criticism welcome.

I’m a polyglot developer and machine learning enthusiast working with multiple technologies.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store