How to Deploy a Python RAG Pipeline on AWS EC2 (Production Guide)
Building a Retrieval-Augmented Generation (RAG) chatbot locally with Python, LangChain, and ChromaDB is straightforward. Moving it from localhost to a reliable cloud server that stays online 24/7 is where many developers hit configuration walls.
In this guide, you will deploy a FastAPI-powered Python RAG backend onto an AWS EC2 Ubuntu instance, keep it running continuously with systemd, and expose it cleanly through an Nginx reverse proxy.
1. Launch and Configure Your AWS EC2 Instance
Start by launching an Ubuntu Server 24.04 LTS instance in the AWS Console. For lightweight RAG prototypes using OpenAI embeddings and a small ChromaDB store, a t3.small (2 GB RAM) or t3.medium instance is recommended—a 1 GB Free Tier instance often runs out of memory when installing ML dependencies.
- Region: Choose
us-east-1(N. Virginia) for lowest latency to OpenAI APIs and US users. - Security Group Inbound Rules: Allow SSH (port 22) from your IP, HTTP (port 80), and HTTPS (port 443) from anywhere.
2. Install Python and Clone Your RAG Project
Connect to your server via SSH, update packages, and create an isolated virtual environment:
ssh -i your-key.pem ubuntu@YOUR_EC2_PUBLIC_IP
sudo apt update && sudo apt upgrade -y
sudo apt install -y python3-pip python3-venv nginx git
git clone https://github.com/your-username/your-rag-app.git
cd your-rag-app
python3 -m venv venv
source venv/bin/activate
pip install --upgrade pip
pip install -r requirements.txt
pip install gets killed on a low-RAM server, add the --no-cache-dir flag or configure a 2 GB swap file before installing packages like chromadb or langchain.
3. Store API Keys Securely with Environment Variables
Never hardcode your OpenAI or Pinecone keys inside Python files. Create a .env file in your project root and restrict its permissions:
cat <<EOF > .env
OPENAI_API_KEY=sk-your-actual-key-here
CHROMA_PERSIST_DIR=./chroma_db
EOF
chmod 600 .env
4. Run FastAPI in the Background with systemd
If you launch uvicorn manually in your terminal, the server stops the moment you close SSH. Instead, create a systemd service so Ubuntu automatically restarts your RAG API on crashes or reboots:
sudo nano /etc/systemd/system/rag-api.service
Paste the following configuration (update paths to match your folder name):
[Unit]
Description=FastAPI Python RAG Service
After=network.target
[Service]
User=ubuntu
WorkingDirectory=/home/ubuntu/your-rag-app
EnvironmentFile=/home/ubuntu/your-rag-app/.env
ExecStart=/home/ubuntu/your-rag-app/venv/bin/uvicorn main:app --host 127.0.0.1 --port 8000
Restart=always
[Install]
WantedBy=multi-user.target
Enable and start the service:
sudo systemctl daemon-reload
sudo systemctl enable rag-api
sudo systemctl start rag-api
sudo systemctl status rag-api
5. Configure Nginx as a Reverse Proxy
Bind Nginx to port 80 and forward traffic to your local Uvicorn process on port 8000:
sudo nano /etc/nginx/sites-available/rag-api
server {
listen 80;
server_name yourdomain.com;
location / {
proxy_pass http://127.0.0.1:8000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_read_timeout 120s;
}
}
Notice proxy_read_timeout 120s;—LLM completions often take longer than standard web requests, and raising this timeout prevents frustrating 504 Gateway Timeout errors.
sudo ln -s /etc/nginx/sites-available/rag-api /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl restart nginx
6. Add Free HTTPS with Certbot
Once your domain's DNS A record points to your EC2 public IP, enable SSL in one command:
sudo apt install -y certbot python3-certbot-nginx
sudo certbot --nginx -d yourdomain.com
Next Steps for Production
Your Python RAG pipeline is now live over HTTPS. Before scaling to heavy traffic, consider setting up AWS CloudWatch alarms for CPU/RAM spikes, backing up your ChromaDB vector directory to Amazon S3, and adding rate limiting in Nginx to protect your OpenAI API budget.