By Selman Karaosmanoglu
2 July 2024
This project demonstrates how to deploy a PyTorch model using FastAPI on AWS App Runner. It provides a scalable and efficient way to serve machine learning models. The project includes detailed instructions for building a Docker image on AWS Cloud9 and deploying it to AWS App Runner via AWS Elastic Container Registry (ECR).
The PyTorch project was converted from the official PyTorch tutorial using Flask by Noah Gift.
Tasks:
- Run the fastapi app:
python app.py
- Upload an image to classify, say a cat using the swagger docs url: /docs
- Deploy to AWS and the AWS App Runner service
flowchart TD;
A[AWS Cloud9] -->|Docker push| B[Amazon Elastic Container Registry];
B -->|Deploy| C[AWS App Runner];
D[FastAPI Docs Swagger UI] -->|Request| C;
docker build .
Note use: docker images
to find (replace id with the image id):
docker run -p 127.0.0.1:8080:8080 54a55841624f
git clone ..
python3 -m venv ~/.venv
source ~/.venv/bin/activate`
Go to the settings of the EC2 instance of Cloud9 and increase the volume size
sudo reboot
- Check volume size and build docker image
df --human-readable
docker build -t pytorch .
docker images
docker run -p 127.0.0.1:8080:8080 <docker image id>
Click Preview button of AWS Cloud9 and go to /docs page of the FastAPI app in preview pane as shown in the screenshot below
Login
aws ecr get-login-password --region eu-west-2 | docker login --username AWS --password-stdin 176839071578.dkr.ecr.eu-west-2.amazonaws.com
Docker Tag
docker tag pytorch:latest 176839071578.dkr.ecr.eu-west-2.amazonaws.com/pytorch:latest
Docker Push
docker push 176839071578.dkr.ecr.eu-west-2.amazonaws.com/pytorch:latest
Go to /docs url
-
Duke University - Virtualization, Docker, Kubernetes Data Engineering Program