Deployment with FastAPI & Streamlit


This tutorial teaches how to deploy AI-powered applications using FastAPI, Streamlit, Docker, and Cloud Platforms like AWS, Azure, and GCP. Perfect for developers looking to deploy scalable applications for the web.

1. Introduction

Deploying your application is a key part of turning a prototype into a usable product. This tutorial will show you how to deploy applications created with FastAPI and Streamlit, containerize them with Docker, and deploy them on popular cloud platforms like AWS, Azure, or Google Cloud Platform (GCP).

2. Tools & Technologies

  1. FastAPI: A modern, fast (high-performance) web framework for building APIs with Python.
  2. Streamlit: A framework to build interactive web apps for machine learning and data science projects.
  3. Docker: A platform to package applications and dependencies into a container, ensuring consistency across environments.
  4. Cloud Platforms: Services like AWS, Azure, and GCP for hosting and scaling your app.

3. Project Steps

3.1 Step 1: Deploying with FastAPI

What is FastAPI?

FastAPI is an asynchronous web framework for building APIs with Python, known for its speed and ease of use. It’s perfect for building REST APIs for your application.

  1. Install FastAPI and Uvicorn (ASGI server):

pip install fastapi uvicorn
  1. Basic FastAPI App Example:

from fastapi import FastAPI

app = FastAPI()

@app.get("/")
def read_root():
return {"message": "Hello, FastAPI!"}

# To run: uvicorn main:app --reload
  1. Running the FastAPI App Locally:

uvicorn main:app --reload

Once you run the above command, the FastAPI app will be live at http://127.0.0.1:8000.

Deploying FastAPI on Cloud:

You can deploy your FastAPI app using cloud services like AWS, Azure, or Google Cloud.

  1. Deploying on AWS EC2:
  2. Set up an EC2 instance.
  3. SSH into the instance, install dependencies (Python, FastAPI, Uvicorn).
  4. Run your FastAPI app using Uvicorn.
  5. Deploying on Azure:
  6. Create a Web App on Azure, and upload your FastAPI application.
  7. Use Azure App Services to run your app with a web server.
  8. Deploying on Google Cloud (GCP):
  9. Use Google Cloud Compute Engine to set up a VM and deploy FastAPI.
  10. Alternatively, use Google App Engine to automatically manage the deployment.

3.2 Step 2: Deploying with Streamlit

What is Streamlit?

Streamlit is a Python library that turns scripts into shareable web apps in minutes. It's ideal for creating interactive dashboards and applications, especially for data science and machine learning models.

  1. Install Streamlit:

pip install streamlit
  1. Basic Streamlit App Example:

import streamlit as st

st.title("Hello, Streamlit!")
st.write("Welcome to the Streamlit App.")
  1. Running the Streamlit App Locally:

streamlit run app.py

The app will be accessible on http://localhost:8501.

Deploying Streamlit on Cloud:

  1. Streamlit Sharing (Free and easy):
  2. Sign up on Streamlit Sharing and directly deploy your app by connecting it to a GitHub repository.
  3. Deploying on AWS (EC2 or Lambda):
  4. Host your Streamlit app on AWS EC2.
  5. Use AWS Lambda for serverless deployment with API Gateway.
  6. Deploying on Heroku:
  7. Use Heroku CLI to deploy Streamlit applications.
  8. Create a Procfile with the following content:

web: streamlit run app.py
  1. Then, push to Heroku from your Git repository.

3.3 Step 3: Dockerizing Your Application

What is Docker?

Docker is a tool that allows you to package your app along with its dependencies into a container. This makes deployment easier by ensuring that the app runs the same way in every environment.

  1. Install Docker:
  2. Download and install Docker from here.
  3. Create a Dockerfile:

# Use an official Python runtime as a parent image
FROM python:3.9

# Set the working directory
WORKDIR /app

# Install dependencies
COPY requirements.txt .
RUN pip install -r requirements.txt

# Copy the current directory contents into the container
COPY . .

# Expose the necessary port
EXPOSE 8501

# Command to run the app
CMD ["streamlit", "run", "app.py"]
  1. Build and Run the Docker Container:

# Build the image
docker build -t streamlit-app .

# Run the container
docker run -p 8501:8501 streamlit-app
  1. Push Docker Image to Docker Hub (Optional):
  2. Create a Docker Hub account.
  3. Push your Docker image for easy access from any machine:

docker tag streamlit-app your-dockerhub-username/streamlit-app
docker push your-dockerhub-username/streamlit-app

3.4 Step 4: Cloud Deployment (AWS / Azure / GCP)

Deploying Docker on Cloud Platforms:

  1. AWS (Elastic Beanstalk or ECS):
  2. Use AWS Elastic Beanstalk for simplified deployment.
  3. Alternatively, use AWS ECS (Elastic Container Service) for deploying Docker containers.
  4. For Elastic Beanstalk:
  5. Install the Elastic Beanstalk CLI.
  6. Create an Elastic Beanstalk environment and deploy the Docker image.
  7. For ECS:
  8. Create a Docker image and push it to Amazon ECR.
  9. Set up a Fargate service to run the container in AWS.
  10. Azure:
  11. Use Azure App Services to deploy containers.
  12. Alternatively, use Azure Kubernetes Service (AKS) to scale containerized applications.
  13. Create a Docker container image and deploy it using Azure Container Instances.
  14. Google Cloud (GCP):
  15. Use Google Kubernetes Engine (GKE) to deploy Docker containers.
  16. Alternatively, use Google Cloud Run for serverless deployment of containerized apps.
  17. GCP Steps:
  18. Build the Docker image and push it to Google Container Registry (GCR).
  19. Use Google Cloud Run to deploy it as a fully managed container.

4. Features & Enhancements

  1. Auto-scaling: Configure cloud platforms to auto-scale your app based on usage (e.g., AWS Auto Scaling).
  2. Continuous Deployment: Set up CI/CD pipelines with GitHub Actions or GitLab CI for automated deployments.
  3. SSL Certificates: Secure your app with SSL by using Let's Encrypt for free certificates.
  4. Monitoring & Logging: Integrate CloudWatch (AWS), Azure Monitor, or Google Stackdriver for real-time monitoring and logging.

5. Best Practices

  1. Optimize Dockerfiles: Use multi-stage builds to reduce the size of Docker images and make deployments faster.
  2. CI/CD Pipelines: Set up CI/CD pipelines to automatically deploy your app when changes are pushed to the repository.
  3. Environment Variables: Store sensitive data like API keys or passwords in environment variables instead of hard-coding them into your app.
  4. Backup & Recovery: Use cloud-based backup solutions for your app’s data and configurations.

6. Outcome

By completing this Deployment tutorial, you will be able to:

  1. Containerize your FastAPI and Streamlit apps with Docker.
  2. Deploy your application on AWS, Azure, or GCP cloud platforms.
  3. Automate cloud deployments and scale your applications to handle production traffic.
  4. Use Docker and cloud services to ensure that your app runs consistently and reliably across environments.