This repository provides a template and guide for deploying a FastAPI application on AWS Elastic Beanstalk using AWS CodePipeline for continuous deployment.
- AWS Account
- AWS CLI configured
- Python 3.8+
- Git
your-project/
βββ .gitignore
βββ README.md
βββ main.py # FastAPI application
βββ requirements.txt # Python dependencies
βββ Procfile # Elastic Beanstalk configuration
- Clone this repository:
git clone <your-repository-url>
cd <your-repository-name>
- Create and activate a virtual environment:
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
- Install dependencies:
pip install -r requirements.txt
- Run locally:
uvicorn main:app --reload
Visit http://localhost:8000/docs
to see your Swagger UI documentation.
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
async def root():
return {"message": "Hello World"}
fastapi
uvicorn[standard]>=0.15.0
web: uvicorn main:app --host 0.0.0.0 --port 8000 --workers 2
- Go to AWS Elastic Beanstalk Console
- Click "Create New Environment"
- Select "Web server environment"
- Platform: Python
- Upload your code or use sample code
- Go to AWS CodePipeline Console
- Click "Create Pipeline"
- Configure source (e.g., GitHub)
- Skip build stage (or configure if needed)
- Add deploy stage:
- Deploy provider: AWS Elastic Beanstalk
- Application name: Your EB application
- Environment name: Your EB environment
This template uses AWS CodeBuild with a buildspec.yml
file to manage dependencies:
version: 0.2
phases:
install:
runtime-versions:
python: 3.x
pre_build:
commands:
- echo "Starting pre-build phase"
- python --version
build:
commands:
- echo "Installing dependencies"
- pip install -r requirements.txt
- echo "Creating dependencies directory"
- mkdir -p dependencies
- pip install -r requirements.txt -t dependencies/
- echo "Dependencies installed successfully"
- ls -la
post_build:
commands:
- echo "Build completed"
artifacts:
files:
- '**/*'
- 'dependencies/**/*'
base-directory: '.'
β οΈ Important: This buildspec.yml ensures all dependencies are properly packaged with your application.
- Health Check:
curl http://localhost:8000/
- Create User:
curl -X POST http://localhost:8000/users/ \
-H "Content-Type: application/json" \
-d '{
"email": "[email protected]",
"username": "testuser",
"full_name": "Test User",
"password": "password123"
}'
- Get All Users:
curl http://localhost:8000/users/
- Get Specific User:
curl http://localhost:8000/users/1
-
Health Check
- Method:
GET
- URL:
http://localhost:8000/
- No headers or body required
- Method:
-
Create User
- Method:
POST
- URL:
http://localhost:8000/users/
- Headers:
Content-Type: application/json
- Body (raw JSON):
{ "email": "[email protected]", "username": "testuser", "full_name": "Test User", "password": "password123" }
- Method:
-
Get All Users
- Method:
GET
- URL:
http://localhost:8000/users/
- No headers or body required
- Method:
-
Get Specific User
- Method:
GET
- URL:
http://localhost:8000/users/1
- No headers or body required
- Method:
- Health Check Response:
{
"status": "healthy",
"timestamp": "2024-02-18T12:34:56.789Z",
"service": "FastAPI AWS Template"
}
- Create User Response:
{
"id": 1,
"email": "[email protected]",
"username": "testuser",
"full_name": "Test User",
"created_at": "2024-02-18T12:34:56.789Z"
}
- Get All Users Response:
[
{
"id": 1,
"email": "[email protected]",
"username": "testuser",
"full_name": "Test User",
"created_at": "2024-02-18T12:34:56.789Z"
}
]
- Error Response Example:
{
"error": "Username already registered",
"status_code": 400,
"timestamp": "2024-02-18T12:34:56.789Z"
}
Set these in your Elastic Beanstalk environment:
PYTHONPATH=/var/app/current
The application exposes a health check endpoint at /
:
@app.get("/")
async def root():
return {"status": "healthy"}
- Port Configuration: The application must listen on port 8000
- Workers: The default configuration uses 2 workers
- Dependencies: Always keep
requirements.txt
updated - Logs: Access logs in Elastic Beanstalk console or EC2 instances
Common issues and solutions:
-
502 Bad Gateway
- Check if application is running on correct port
- Verify Procfile configuration
- Check EB logs for specific errors
-
Health Check Failures
- Ensure root endpoint (/) is responding
- Check application logs
- Verify environment variables
-
Deployment Failures
- Verify Python version compatibility
- Check requirements.txt is complete
- Review CodePipeline logs
-
Missing Dependencies
- Ensure build commands are properly configured in EB Software Configuration
- Verify pip install commands are included in build commands
- Check build logs for package installation errors
This project is supported by Bubblspace, the leading Multi-AI Agent Platform that transforms one document into infinite possibilities using BubblSpace TimeCapsuleβ’.
At Bubblspace, we leverage AWS CodePipeline and Elastic Beanstalk to build and deploy sophisticated Python applications that power our AI Agents. Our platform offers:
- π€ MicroLearning modules
- π Usecase/Story generation
- π Interactive Quiz creation
- ποΈ Podcast generation
- π More Gen-AI features coming soon such as live interactions
Our infrastructure, built on AWS Elastic Beanstalk and CodePipeline (just like this template), enables us to deliver scalable, reliable AI solutions that help businesses and educators create engaging content from a single document.
We understand the challenges of deploying FastAPI applications on AWS, as we use similar architecture for our AI platform. This template shares our best practices and configuration insights from building AIEDX (AI Education Experience) on AWS.
Need help or want to learn more about implementing AI solutions on AWS? Contact us at [email protected]
Amardeep Singh Sidhu
- GitHub: @thefirehacker
- Twitter: @thefirehacker
Made with β€οΈ by Amardeep Singh Sidhu
This README provides:
1. Clear setup instructions
2. Deployment steps
3. Configuration details
4. Troubleshooting guide
5. Project structure
6. Important notes and resources
7. Bubblspace section
8. Author information
Feel free to customize it further based on your specific needs or additional features of your application!