AI-Powered Chat Backend with AWS Bedrock Integration
A high-performance FastAPI backend service that integrates with AWS Bedrock to provide AI-powered security analysis capabilities.
- AWS Deployment Guide - Complete guide for deploying to AWS with Docker, CloudFormation, and GitHub Actions
- Deployment Checklist - Step-by-step checklist for deployment
- Architecture Documentation - System architecture and design decisions Features full CRUD functionality for chat history management, PostgreSQL database integration, and ChatGPT-style conversation experience.
- Model: Anthropic Claude 3 Haiku (
anthropic.claude-3-haiku-20240307-v1:0) - Real-time AI response generation
- Configurable temperature and token limits
- Automatic retry and error handling
- β Create: Store new chat messages and responses
- β Read: Retrieve chat history by user, chat session, or message ID
- β Update: Modify existing conversations (if needed)
- β Delete: Remove specific messages or entire chat sessions
- Hierarchical ID structure:
chat_id: Groups messages in a conversationmessage_uid: Unique identifier for each messageresponse_session_id: Links user input to AI response
- Load complete chat history on frontend initialization
- Efficient pagination and filtering
- PostgreSQL with async support (SQLModel + psycopg)
- Alembic migrations for schema management
- JSONB storage for flexible metadata
- Indexed columns for fast queries
- Async/await throughout the application
- Database connection pooling
- Caching strategy for frequently accessed data
- Rate limiting per user
- No authentication required (open for testing)
- Anonymous user support
- CORS configured for frontend access
βββββββββββββββββββββββββββββββββββββββββββββββββββ
β Frontend (React + Vite) β
β - ChatGPT-style UI β
β - Load chat history on init β
β - Real-time message streaming β
ββββββββββββββββββββ¬βββββββββββββββββββββββββββββββ
β HTTP/REST API
βΌ
βββββββββββββββββββββββββββββββββββββββββββββββββββ
β FastAPI Backend β
β ββ /api/v1/chats (POST) - Send messages β
β ββ /api/v1/history (GET) - Get chat history β
β ββ /api/v1/conversations - CRUD operations β
β ββ /health - Health check β
ββββββββββββββββββββ¬βββββββββββββββββββββββββββββββ
β
ββββββββββββ΄ββββββββββββ
βΌ βΌ
ββββββββββββββββββββ βββββββββββββββββββββββ
β AWS Bedrock β β PostgreSQL DB β
β - Claude 3 β β - Chats table β
β - AI responses β β - Users table β
ββββββββββββββββββββ βββββββββββββββββββββββ
CREATE TABLE chats (
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
user_id VARCHAR(255) NOT NULL,
chat_id UUID NOT NULL,
user_input TEXT NOT NULL,
bedrock_response TEXT,
chat_metadata JSONB,
created_at TIMESTAMP NOT NULL DEFAULT NOW(),
updated_at TIMESTAMP NOT NULL DEFAULT NOW()
);
CREATE INDEX idx_chats_user_id ON chats(user_id);
CREATE INDEX idx_chats_chat_id ON chats(chat_id);
CREATE INDEX idx_chats_created_at ON chats(created_at);{
"question": "Original user query",
"bedrock_processed": true,
"chat_id": "uuid",
"message_uid": "uuid",
"response_session_id": "uuid",
"user_id": "anonymous",
"timestamp": "2025-11-04T00:00:00Z"
}GET /health
GET /api/v1/healthPOST /api/v1/chats
Content-Type: application/json
{
"message": "What are the top security vulnerabilities?",
"chat_id": "optional-uuid",
"message_uid": "optional-uuid"
}Response:
{
"response": "AI-generated response text...",
"chat_id": "abc123",
"message_uid": "def456",
"response_session_id": "ghi789",
"metadata": {
"question": "What are the top security vulnerabilities?",
"bedrock_processed": true,
"timestamp": "2025-11-04T10:30:00Z"
},
"status": "success"
}# Get chat history by chat_id
GET /api/v1/my-history?chat_id=abc123&message_id=def456
# Get all conversations
GET /api/v1/conversations?limit=50&offset=0
# Get specific chat conversations
GET /api/v1/conversations/{chat_id}| Component | Technology | Version |
|---|---|---|
| Framework | FastAPI | 0.119.1 |
| Language | Python | 3.11+ |
| Database | PostgreSQL | 15+ |
| ORM | SQLModel + SQLAlchemy | 2.x |
| Migrations | Alembic | 1.17.1 |
| AI Service | AWS Bedrock | Claude 3 Haiku |
| Async Driver | psycopg3 (async) | 3.x |
| Authentication | Optional (currently disabled) | - |
Recommended for production environments
β
Complete automated deployment to AWS
β
Docker containerized for consistency
β
Auto-scaling with load balancer
β
CI/CD with GitHub Actions
π Follow AWS Deployment Guide
π Use Deployment Checklist
Quick deploy command:
# After configuring parameters.json
.\deploy.ps1For development and testing
- Python 3.11+
- PostgreSQL 15+
- AWS Account with Bedrock access
- AWS Access Keys with Bedrock permissionsgit clone <repository-url>
cd bedrock-backendpython -m venv venv
# Windows
venv\Scripts\activate
# Linux/Mac
source venv/bin/activatepip install -r requirements.txtCreate .env file:
# CORS Configuration
ALLOWED_ORIGINS=["http://localhost:5173", "https://your-cloudfront-url.com"]
# AWS Bedrock
BEDROCK_MODEL_ID=anthropic.claude-3-haiku-20240307-v1:0
BEDROCK_MAX_TOKENS=4000
BEDROCK_TEMPERATURE=0.1
AWS_ACCESS_KEY_ID=your-access-key
AWS_SECRET_ACCESS_KEY=your-secret-key
AWS_DEFAULT_REGION=us-east-1
# Database
DATABASE_URL=postgresql+psycopg_async://user:password@host:port/dbname
# Optional: Authentication (currently disabled)
JWT_SECRET=your-secret-key
JWT_ALGORITHM=HS256
# Optional: Redis (for caching)
REDIS_HOST=localhost
REDIS_PORT=6379# Initialize Alembic (if not already done)
alembic upgrade head# Using uvicorn directly
uvicorn main:app --reload --host 0.0.0.0 --port 8000
# Or using FastAPI CLI
fastapi dev main.pyServer will be available at: http://localhost:8000
API Documentation: http://localhost:8000/docs
-
Enable Bedrock in AWS Console
- Go to AWS Bedrock console
- Enable model access for Claude 3 Haiku
- Region:
us-east-1(or your preferred region)
-
Create IAM User with Permissions
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"bedrock:InvokeModel",
"bedrock:InvokeModelWithResponseStream"
],
"Resource": "arn:aws:bedrock:*::foundation-model/*"
}
]
}- Generate Access Keys
- IAM β Users β Security credentials
- Create access key
- Copy
AWS_ACCESS_KEY_IDandAWS_SECRET_ACCESS_KEY
PostgreSQL (Local):
# Install PostgreSQL
# Create database
createdb security_agents_db
# Create user
psql -c "CREATE USER dbuser WITH PASSWORD 'your-password';"
psql -c "GRANT ALL PRIVILEGES ON DATABASE security_agents_db TO dbuser;"PostgreSQL (Cloud - Neon, RDS, etc.):
- Use provided connection string
- Ensure SSL is enabled for production
- Configure connection pooling
alembic revision --autogenerate -m "Add new feature"alembic upgrade headalembic downgrade -1alembic historyCurrent implementation:
- 10 requests per minute per user
- 60-second window
- Anonymous users: No limit (or separate limit)
Configurable in chats/service.py:
self.rate_limit = 10
self.rate_limit_window = 60# Health check
curl http://localhost:8000/health
# Send chat message
curl -X POST http://localhost:8000/api/v1/chats \
-H "Content-Type: application/json" \
-d '{
"message": "What are SQL injection attacks?",
"chat_id": "test-chat-123"
}'
# Get chat history
curl "http://localhost:8000/api/v1/my-history?chat_id=test-chat-123"# Run tests (if implemented)
pytest tests/
# Run with coverage
pytest --cov=. tests/- Deploy with AWS SAM or Serverless Framework
- Use API Gateway for HTTP endpoints
- Integrate with RDS for database
- Cost-effective for variable traffic
- Deploy on Ubuntu/Amazon Linux
- Use systemd for process management
- Nginx as reverse proxy
- Suitable for consistent traffic
- Package as Docker container
- Deploy to ECS Fargate
- Auto-scaling capabilities
- Production-grade setup
- Simple deployment process
- Automatic scaling
- Managed environment
- Good for quick deployment
β οΈ No authentication - Open for testingβ οΈ No authorization - All endpoints accessibleβ οΈ Rate limiting - Basic protection
-
Enable Authentication
- JWT-based authentication
- Okta/Auth0 integration
- API key authentication
-
Add Authorization
- Role-based access control (RBAC)
- User-specific chat history
- Admin-only endpoints
-
Secure Secrets
- Use AWS Secrets Manager
- Environment-based configuration
- Rotate credentials regularly
-
API Security
- HTTPS only (TLS 1.2+)
- CORS whitelist
- Request size limits
- Input validation
-
Database Security
- SSL connections
- Encrypted at rest
- Regular backups
- Access control
1. Bedrock Connection Error
β Failed to initialize Bedrock client
Solution: Check AWS credentials, region, and Bedrock model access
2. Database Connection Error
sqlalchemy.exc.OperationalError: could not connect to server
Solution: Verify DATABASE_URL, check PostgreSQL is running
3. CORS Error in Frontend
Access to fetch blocked by CORS policy
Solution: Add frontend URL to ALLOWED_ORIGINS in .env
4. Rate Limit Exceeded
429 Too Many Requests
Solution: Wait 60 seconds or adjust rate limits
| Variable | Description | Required | Default |
|---|---|---|---|
ALLOWED_ORIGINS |
CORS allowed origins (JSON array) | Yes | - |
DATABASE_URL |
PostgreSQL connection string | Yes | - |
BEDROCK_MODEL_ID |
AWS Bedrock model identifier | Yes | claude-3-haiku |
BEDROCK_MAX_TOKENS |
Max response tokens | No | 4000 |
BEDROCK_TEMPERATURE |
AI temperature (0-1) | No | 0.1 |
AWS_ACCESS_KEY_ID |
AWS access key | Yes | - |
AWS_SECRET_ACCESS_KEY |
AWS secret key | Yes | - |
AWS_DEFAULT_REGION |
AWS region | Yes | us-east-1 |
JWT_SECRET |
JWT signing secret | No | - |
REDIS_HOST |
Redis host for caching | No | localhost |
- Location:
logs/directory - Format: Structured JSON logs
- Rotation: Daily rotation with 7-day retention
- Request latency (target: < 2s)
- Bedrock API calls (cost optimization)
- Database query performance
- Error rates
- Rate limit hits
- Add authentication (JWT/Okta)
- Implement Redis caching
- Add WebSocket support for streaming responses
- Implement message editing/deletion
- Add conversation sharing
- Export chat history (PDF/JSON)
- Multi-model support (Claude, GPT-4, etc.)
- Vector database integration for RAG
- Conversation summarization
- Cost tracking per user
Proprietary - Internal Use Only
For issues, questions, or contributions:
- Create an issue in the repository
- Contact the development team
- Check documentation in
/docs
Built with β€οΈ using FastAPI, AWS Bedrock, and PostgreSQL