Creating a hello world API with Python using FastAPI, bundle it up as a container image, and then deploy it to a Kubernetes cluster on Amazon Web Services (AWS)!
FastAPI is a modern, fast (high-performance), web framework for building APIs with Python 3.7+ based on standard Python type hints.
The key features are:
-
Fast: Very high performance, on par with NodeJS and Go (thanks to Starlette and Pydantic). One of the fastest Python frameworks available.
-
Fast to code: Increase the speed to develop features by about 200% to 300%. *
-
Fewer bugs: Reduce about 40% of human (developer) induced errors. *
-
Intuitive: Great editor support. Completion everywhere. Less time debugging.
-
Easy: Designed to be easy to use and learn. Less time reading docs.
-
Short: Minimize code duplication. Multiple features from each parameter declaration. Fewer bugs.
- First we create a virtual environment
python3 -m venv ./venv
The module used to create and manage virtual environments is called venv. venv will usually install the most recent version of Python that you have available. If you have multiple versions of Python on your system, you can select a specific Python version by running python3 or whichever version you want.
- Installing packages
pip install fastapi
pip install uvicorn
FastAPI is the framework you’ll use to build your API, and Uvicorn is the server that will use the API you build to serve requests.
- Make a basic FastAPI file
If you don't know how to make one, just copy the one from repository and just like that, you have a fully functional API application with some best practices like automatic documentation and serialization built in.
This code defines your application, but it won’t run on itself if you call it with python directly. To run it, you need a server program. In the steps above, you already installed Uvicorn. That will be your server.
- Create and build a simple Dockerfile
docker build -t {your preferred name} .
-
Create a repository at DockerHub and push your image there
-
Log in to your AWS account and via its EKS service, create a Kubernetes Cluster
-
Create a simple demployment and service file for the cluster and appply them using following command:
kubectl apply -f .
- Now to deploy your application on cluster
kubectl port-forward {any running pod name} 8080:80
The primary goal of this project is to help you learn how to deploy an application on Kubernetes cluster via a CSP