A comprehensive web chat application with OpenAI integration, Redis caching for message history, deployed on Azure Kubernetes Service (AKS) using Terraform infrastructure as code.
The application uses a secure internal communication pattern with API proxy routes for optimal security and performance.
- Next.js with Tailwind CSS and TypeScript
- Responsive chat interface with modern UI
- Session management with localStorage
- Internal API Routes:
/api/chat,/api/sessions/*(acts as proxy to backend) - External LoadBalancer: Only component exposed to internet
- Chat Service (Python/FastAPI): Handles OpenAI communication
- Session Service (Python/FastAPI): Manages Redis operations and message persistence
- ClusterIP Services: Internal-only communication within Kubernetes cluster
- Azure Kubernetes Service (AKS): Container orchestration
- Azure OpenAI: GPT-4o-mini for chat completions
- Azure Cache for Redis: Message history storage (last 20 messages per session)
- Azure Container Registry: Docker image storage
- Terraform: Infrastructure as Code
Internet β Frontend (LoadBalancer) β Next.js API Routes β Backend Services (ClusterIP) β Redis/OpenAI
Key Security Benefits:
- β No External Backend Exposure: Chat and Session services are ClusterIP only
- β API Proxy Pattern: Frontend routes proxy requests to internal services
- β CORS Eliminated: No cross-origin requests from browser
- β
Kubernetes Service Discovery: Uses internal DNS names (
chat-service:8000) - β Network Isolation: Backend services only accessible within cluster
- Azure CLI installed and configured
- Terraform installed
- Docker installed
- kubectl installed
- Node.js (for local frontend development)
- Python 3.11+ (for local backend development)
# Navigate to infrastructure directory
cd infrastructure
# Initialize Terraform
terraform init
# Plan the deployment
terraform plan
# Apply the infrastructure
terraform apply- Follow the GitHub Actions Setup Guide
- Push code to your repository
- Automatic deployment will trigger
.\deploy.ps1chmod +x deploy.sh
./deploy.shAfter deployment, get the frontend URL:
kubectl get svc frontend -n azure-chat-appAccess your chat application at http://<EXTERNAL-IP>:3000
cd backend/chat-service
pip install -r requirements.txt
# Set environment variables
export OPENAI_ENDPOINT="your-openai-endpoint"
export OPENAI_API_KEY="your-openai-key"
export SESSION_SERVICE_URL="http://localhost:8001"
# Run the service
uvicorn main:app --host 0.0.0.0 --port 8000 --reloadcd backend/session-service
pip install -r requirements.txt
# Set environment variables
export REDIS_HOST="your-redis-host"
export REDIS_PASSWORD="your-redis-password"
export REDIS_PORT="6380"
export REDIS_SSL="true"
# Run the service
uvicorn main:app --host 0.0.0.0 --port 8001 --reloadcd frontend
npm install
# For local development, no external service URLs needed
# API routes will communicate with local backend services
# Run development server
npm run devNote: In production, the frontend uses internal API routes (/api/chat, /api/sessions/*) that proxy requests to backend services. No external service configuration needed!
azure-chat-app/
βββ infrastructure/ # Terraform configuration
β βββ main.tf # Main infrastructure resources
β βββ variables.tf # Input variables
β βββ outputs.tf # Output values
βββ backend/
β βββ chat-service/ # OpenAI communication service
β β βββ main.py # FastAPI application
β β βββ requirements.txt
β β βββ Dockerfile
β βββ session-service/ # Redis session management
β βββ main.py # FastAPI application
β βββ requirements.txt
β βββ Dockerfile
βββ frontend/ # Next.js frontend
β βββ app/
β β βββ api/ # Internal API routes (proxy to backend)
β β β βββ chat/ # Chat API proxy route
β β β βββ sessions/ # Session API proxy routes
β β βββ page.tsx # Main chat interface
β β βββ layout.tsx # App layout
β β βββ globals.css # Global styles
β βββ package.json
β βββ next.config.js
β βββ tailwind.config.js
β βββ Dockerfile
βββ k8s/ # Kubernetes manifests
β βββ namespace.yaml
β βββ configmap.yaml
β βββ chat-service.yaml
β βββ session-service.yaml
β βββ frontend.yaml
βββ deploy.sh # Bash deployment script
βββ deploy.ps1 # PowerShell deployment script
βββ README.md
POST /api/chat- Send message and get AI response (proxies to chat-service)GET /api/sessions/{session_id}/messages- Get conversation history (proxies to session-service)DELETE /api/sessions/{session_id}- Delete session (proxies to session-service)
GET /health- Health checkPOST /chat- Send message and get AI response
GET /health- Health checkGET /sessions/{session_id}/messages- Get conversation historyPOST /sessions/{session_id}/messages- Add message to sessionGET /sessions/{session_id}/info- Get session informationDELETE /sessions/{session_id}- Delete sessionGET /sessions- List active sessions
Note: Backend services are only accessible from within the Kubernetes cluster via internal service names.
OPENAI_ENDPOINT: Azure OpenAI endpoint URLOPENAI_API_KEY: Azure OpenAI access keyOPENAI_DEPLOYMENT: Model deployment name (default: gpt-4o-mini)SESSION_SERVICE_URL: Internal service URL (http://session-service:8001)
REDIS_HOST: Redis cache hostnameREDIS_PASSWORD: Redis access keyREDIS_PORT: Redis SSL port (6380)REDIS_SSL: Use SSL connection (true)
CHAT_SERVICE_URL: Internal service URL (http://chat-service:8000) - used by API routesSESSION_SERVICE_URL: Internal service URL (http://session-service:8001) - used by API routes
Note: No NEXT_PUBLIC_* variables needed in production - frontend uses internal API routes!
- Max Messages: 20 messages per session
- Session Expiry: 24 hours
- SSL: Enabled by default for Azure Cache for Redis
- SSL/TLS encryption for Redis connections
- Kubernetes secrets for sensitive data
- CORS configuration for API security
- Resource limits and health checks for all services
- Azure managed identities for service authentication
# Chat service logs
kubectl logs -l app=chat-service -n azure-chat-app
# Session service logs
kubectl logs -l app=session-service -n azure-chat-app
# Frontend logs
kubectl logs -l app=frontend -n azure-chat-app
# All services
kubectl logs -l app -n azure-chat-app# Scale frontend
kubectl scale deployment/frontend --replicas=3 -n azure-chat-app
# Scale backend services
kubectl scale deployment/chat-service --replicas=3 -n azure-chat-app
kubectl scale deployment/session-service --replicas=3 -n azure-chat-app-
LoadBalancer IP not assigned
kubectl get svc frontend -n azure-chat-app -w
-
Pod not starting
kubectl describe pod -l app=<service-name> -n azure-chat-app
-
Check service connectivity
kubectl port-forward svc/chat-service 8000:8000 -n azure-chat-app kubectl port-forward svc/session-service 8001:8001 -n azure-chat-app
-
Redis connection issues
- Verify Redis credentials in Kubernetes secrets
- Check Azure Cache for Redis firewall rules
- Ensure SSL is properly configured
# Get all resources
kubectl get all -n azure-chat-app
# Check secrets
kubectl get secrets -n azure-chat-app
# Execute into a pod
kubectl exec -it <pod-name> -n azure-chat-app -- /bin/bash
# Check resource usage
kubectl top pods -n azure-chat-appTo destroy all resources:
# Delete Kubernetes resources
kubectl delete namespace azure-chat-app
# Destroy infrastructure
cd infrastructure
terraform destroyThis project is licensed under the MIT License.
- Fork the repository
- Create a feature branch
- Make your changes
- Test thoroughly
- Submit a pull request
β Implemented
- Real-time chat with Azure OpenAI
- Message history persistence (last 20 messages)
- Session management
- Responsive UI with Tailwind CSS
- Containerized microservices
- Infrastructure as Code with Terraform
- Kubernetes deployment
- Health checks and monitoring
- Auto-scaling capabilities
π§ Future Enhancements
- User authentication
- Multiple chat rooms
- File upload support
- Message encryption
- Prometheus metrics
- Database integration for persistent storage
This project includes comprehensive GitHub Actions workflows for automated deployment:
- Deploy: Automatic deployment on push to main/develop
- Test: Run tests and validation on pull requests
- Destroy: Manual infrastructure cleanup
- Follow the GitHub Actions Setup Guide
- Configure Azure authentication with OIDC (no secrets required)
- Push code to trigger automatic deployment
- Secure: Uses OIDC authentication (no stored secrets)
- Automated: Deploy on every push
- Reliable: Includes testing and validation
- Scalable: Environment-specific deployments