FastAPI + Next.js + LLM (Optional) + AWS ECS
A production-ready template for building full-stack applications with:
- Backend: FastAPI (Python 3.13) with async PostgreSQL
- Frontend: Next.js 16 (TypeScript)
- LLM: Optional Anthropic Claude integration
- Infrastructure: Docker + AWS ECS (Terraform)
- Docker and Docker Compose
- (Optional) Anthropic API key for LLM features
-
Start all services (no configuration needed!):
docker-compose up -d
-
Access the application:
- Frontend: http://localhost:3000
- API: http://localhost:8000
- API Docs: http://localhost:8000/docs
- Health Check: http://localhost:8000/api/v1/health
-
(Optional) Enable LLM features:
# Create .env file cp .env.example .env # Add your Anthropic API key to .env # APP_ANTHROPIC_API_KEY=sk-ant-your-key-here # Restart API service docker-compose restart api
That's it! The application runs perfectly fine without an API key - LLM features are completely optional.
Once Docker is running, test your application with any of these methods:
Quick Test (30 seconds):
# Check health
curl http://localhost:8000/api/v1/health
# View interactive docs
open http://localhost:8000/docs # macOS
# or visit http://localhost:8000/docs in your browserAutomated Testing:
# Run complete test suite
./scripts/test-api.shFor comprehensive testing guide: See TESTING.md for:
- Interactive Swagger UI testing (no coding!)
- curl command examples
- Postman collection
- Automated test scripts
- Troubleshooting guide
| Service | Port | URL | Description |
|---|---|---|---|
| Frontend | 3000 | http://localhost:3000 | Next.js web application |
| API | 8000 | http://localhost:8000 | FastAPI backend |
| Docs | 8000 | http://localhost:8000/docs | Interactive API documentation |
| PostgreSQL | 5432 | localhost:5432 | Database |
| Redis | 6379 | localhost:6379 | Cache |
- ✅ User authentication (register, login, logout)
- ✅ Session management with secure password hashing (Argon2)
- ✅ Database migrations with Alembic
- ✅ Health check endpoints
- ✅ Optional LLM integration (works without API key!)
- ✅ Type-safe API contracts (Pydantic)
- ✅ Async database operations (SQLModel + asyncpg)
- ✅ CORS configuration
- ✅ Docker containerization
- ✅ Production-ready Terraform infrastructure
When you add an Anthropic API key, you get:
- Text completion endpoint (
/api/v1/llm/complete) - Streaming responses (
/api/v1/llm/stream) - Conversation history tracking
- Configurable models and parameters
Without an API key, the LLM endpoints return a clear 503 error explaining how to enable them.
# Start services
docker-compose up -d
# View logs
docker-compose logs -f
# Stop services
docker-compose down
# Rebuild after changes
docker-compose buildBackend:
# Install dependencies (requires Python 3.13+)
cd packages/api
pip install uv
uv pip install -e .
# Run development server
uvicorn app_api.main:app --reload --host 0.0.0.0 --port 8000Frontend:
# Install dependencies (requires Node.js 20+)
cd frontend
npm install
# Run development server
npm run devDatabase:
# Make sure PostgreSQL is running on localhost:5432
# Or use Docker:
docker-compose up -d postgres redis# Start all services
docker-compose up -d
# Stop all services
docker-compose down
# Restart a specific service
docker-compose restart api
# View service status
docker-compose ps
# View logs
docker-compose logs -f api
docker-compose logs -f frontend# Create a new migration
docker-compose exec api app-migrate revision "add users table" --autogenerate
# Run migrations
docker-compose exec api app-migrate upgrade head
# Rollback last migration
docker-compose exec api app-migrate downgrade -1
# For production ECS migrations, see MIGRATIONS.md# Health check
curl http://localhost:8000/api/v1/health
# Register a user
curl -X POST http://localhost:8000/api/v1/auth/register \
-H "Content-Type: application/json" \
-d '{"email": "user@example.com", "password": "secure-password-123"}'
# LLM completion (requires API key)
curl -X POST http://localhost:8000/api/v1/llm/complete \
-H "Content-Type: application/json" \
-d '{"prompt": "Hello, world!"}'template-app-ecs/
├── packages/ # Python backend packages (monorepo)
│ ├── schemas/ # Shared Pydantic models and settings
│ ├── database/ # Database layer (SQLModel + Alembic)
│ ├── auth/ # Authentication (Argon2 hashing, sessions)
│ ├── llm/ # LLM integration (Anthropic Claude)
│ └── api/ # FastAPI application
│ ├── routers/ # API endpoints (health, auth, llm)
│ └── Dockerfile # API container
├── frontend/ # Next.js application
│ ├── src/
│ │ ├── app/ # App Router pages
│ │ └── lib/ # API client, utilities
│ ├── scripts/ # Type generation from OpenAPI
│ └── Dockerfile # Frontend container
├── infrastructure/ # Terraform for AWS ECS
│ ├── modules/ # Reusable Terraform modules
│ └── environments/ # Environment configs (dev, staging, prod)
├── docker-compose.yml # Local development environment
├── .env.example # Environment variables template
├── README.md # This file
└── SETUP_SUMMARY.md # Detailed setup guide
The application uses environment variables for configuration. See .env.example for all available options.
Required (have defaults):
APP_SECRET_KEY- Session encryption key (default: dev-secret-change-in-production)APP_DB_HOST- Database host (default: localhost)APP_DB_NAME- Database name (default: app_db)APP_DB_USER- Database user (default: app_user)APP_DB_PASSWORD- Database password (default: dev_password)
Optional:
APP_ANTHROPIC_API_KEY- Anthropic API key for LLM featuresAPP_DEFAULT_MODEL- Claude model to use (default: claude-opus-4-8)APP_LOG_LEVEL- Logging level (default: INFO)APP_ALLOWED_ORIGINS- CORS origins (default: ["http://localhost:3000"])
This template includes complete Terraform configuration for AWS ECS deployment:
-
Configure AWS credentials:
aws configure
-
Update Terraform variables:
cd infrastructure/environments/prod cp terraform.tfvars.example terraform.tfvars # Edit terraform.tfvars with your settings
-
Deploy infrastructure:
terraform init terraform plan terraform apply
-
Build and push Docker images:
# See infrastructure/README.md for detailed instructions
See infrastructure/ directory and sample-claude-app.md for detailed deployment documentation.
Backend:
- FastAPI 0.136+ (async Python web framework)
- SQLModel + SQLAlchemy 2.0 (async ORM)
- PostgreSQL 16+ (database)
- Redis 7 (caching)
- Alembic (database migrations)
- Anthropic Claude SDK 0.40+ (LLM integration)
- Argon2 (password hashing)
- Pydantic 2.13+ (data validation)
- uvicorn (ASGI server)
Frontend:
- Next.js 16+ (React framework with App Router)
- TypeScript 5.9+
- Tailwind CSS 4.0+
- npm (package manager)
Infrastructure:
- Docker + Docker Compose (containerization)
- AWS ECS Fargate (serverless containers)
- AWS RDS (managed PostgreSQL)
- AWS ElastiCache (managed Redis)
- Terraform (infrastructure as code)
- GitHub Actions (CI/CD)
# Check service status
docker-compose ps
# View logs for errors
docker-compose logs
# Common fix: remove old containers and volumes
docker-compose down -v
docker-compose up -dIf you see errors about ports 3000, 8000, 5432, or 6379 being in use:
# Find process using the port
lsof -i :3000
# Kill the process or stop conflicting services# Ensure PostgreSQL is healthy
docker-compose ps postgres
# Restart database
docker-compose restart postgres
# Check database logs
docker-compose logs postgres- Without API key: This is expected! LLM endpoints return 503 with instructions
- With API key: Check logs for errors:
docker-compose logs api - Verify API key is set correctly in
.env
- Setup Guide: See
SETUP_SUMMARY.mdfor detailed setup instructions - Migrations: See
MIGRATIONS.mdfor database migration guide (local + ECS) - Architecture: See
sample-claude-app.mdfor complete architecture documentation - API Docs: Visit http://localhost:8000/docs when running
- Infrastructure: See
infrastructure/directory for AWS deployment docs
- Visit https://console.anthropic.com
- Sign up or log in
- Navigate to API Keys section
- Create a new API key
- Add it to your
.envfile:APP_ANTHROPIC_API_KEY=sk-ant-your-key-here - Restart the API:
docker-compose restart api
See LICENSE file for details.
Contributions are welcome! Please feel free to submit a Pull Request.