A comprehensive Django-based web application for booking and managing AWS EC2 instances with JupyterHub automation for educational workshops and hackathons.
- User-friendly Booking Interface: Simple form for users to schedule EC2 resources
- Automated EC2 Provisioning: On-demand creation of JupyterHub instances
- Credential Management: Automatic generation and delivery of user credentials
- Email Notifications: Booking confirmations and instance access details
- Scheduled Tasks: Time-based instance provisioning and shutdown
- AWS Integration: Complete EC2 lifecycle management
- Security: Proper security group configuration and credential handling
- System Architecture
- Installation
- Configuration
- Usage
- Development
- Testing
- Deployment
- Docker
- Contributing
- License
The system is built using a modular architecture with the following components:
βββββββββββββββββββ βββββββββββββββββββ βββββββββββββββββββ
β β β β β β
β Web Frontend ββββββΆβ Django Backend ββββββΆβ AWS EC2 β
β β β β β β
βββββββββββββββββββ ββββββββββ¬βββββββββ βββββββββββββββββββ
β
βΌ
βββββββββββββββββββ
β β
β Celery Tasks β
β β
ββββββββββ¬βββββββββ
β
βΌ
βββββββββββββββββββ
β β
β Email Service β
β β
βββββββββββββββββββ
- Web Frontend: User interface for registration and booking management
- Django Backend: Core application logic and data management
- Celery Tasks: Background jobs for scheduled provisioning and cleanup
- AWS EC2 Integration: Management of EC2 instances and security groups
- Email Service: Communication with users about bookings and resources
- Python 3.12+
- PostgreSQL 16+
- Redis 7+ (for Celery task queue)
- AWS Account with appropriate permissions
- Docker and Docker Compose (optional)
-
Clone the repository:
git clone <repository-url> cd ec2-booking-system
-
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
-
Set up environment variables:
cp .env.example .env # Edit .env with your configuration
-
Run database migrations:
python manage.py migrate
-
Start the development server:
python manage.py runserver
-
In a separate terminal, start Celery worker:
celery -A booking worker -l info
Create a .env
file in the project root with the following variables:
# Django settings
SECRET_KEY=your_secret_key
DEBUG=True
ALLOWED_HOSTS=localhost,127.0.0.1
# Database settings
DB_NAME=your_db_name
DB_USER=your_db_user
DB_PASSWORD=your_db_password
DB_HOST=your_db_host
DB_PORT=5432
# AWS settings
AWS_ACCESS_KEY_ID=your_aws_access_key_id
AWS_SECRET_ACCESS_KEY=your_aws_secret_access_key
AWS_DEFAULT_REGION=ap-southeast-2
# EC2 configuration
AWS_INSTANCE_TYPE=t3.micro
AWS_AMI_ID=ami-0892a9c01908fafd1
AWS_KEY_NAME=aws_00
# JupyterHub settings
JUPYTER_REQUIREMENTS_URL=https://raw.githubusercontent.com/PawseySC/quantum-computing-hackathon/main/python/requirements.txt
JUPYTER_ADMIN_USERNAME=pawsey
JUPYTER_USERS_PER_INSTANCE=2
# Email settings
EMAIL_HOST=smtp.example.com
EMAIL_PORT=587
EMAIL_USE_TLS=True
EMAIL_HOST_USER=[email protected]
EMAIL_HOST_PASSWORD=your_email_password
# Celery settings
CELERY_BROKER_URL=redis://localhost:6379/0
CELERY_RESULT_BACKEND=redis://localhost:6379/0
TIME_ZONE=Australia/Perth
The AWS user associated with the access key needs the following permissions:
ec2:RunInstances
ec2:DescribeInstances
ec2:TerminateInstances
ec2:StopInstances
ec2:CreateSecurityGroup
ec2:AuthorizeSecurityGroupIngress
ec2:DescribeSecurityGroups
events:PutRule
events:PutTargets
lambda:AddPermission
lambda:InvokeFunction
- Access the booking registration form at
http://localhost:8000/booking/register/
- Enter your email address, desired booking time, and the number of users
- Submit the form to receive an email with user credentials
- At the scheduled time, instances will be provisioned automatically
- You'll receive a second email with instance access details once provisioning is complete
Access the Django admin interface at http://localhost:8000/admin/
to:
- View and manage bookings
- Check instance statuses
- Generate reports
ec2-booking-system/
βββ aws_ec2/ # Main application
β βββ ec2_utils/ # AWS EC2 utilities
β βββ services/ # Service layer
β βββ templates/ # HTML templates
β βββ lambda_functions/ # AWS Lambda handlers
β βββ management/ # Django management commands
β βββ migrations/ # Database migrations
β βββ tests/ # Test suite
βββ booking/ # Project configuration
βββ logs/ # Log files
βββ .env # Environment variables
βββ docker-compose.yml # Docker configuration
βββ requirements.txt # Python dependencies
-
Create a feature branch:
git checkout -b feature/your-feature-name
-
Make changes and run tests:
python manage.py test
-
Submit a pull request
The project includes a comprehensive test suite covering all major components:
- Service Layer Tests: Booking, email, and logging services
- EC2 Utilities Tests: Instance management, security, user data generation
- Django Component Tests: Models, forms, views, and tasks
- AWS Lambda Function Tests: Instance stopping and termination
To run the entire test suite:
python manage.py test aws_ec2
For pytest with coverage report:
pytest --cov=aws_ec2
To run specific tests:
python manage.py test aws_ec2.tests.test_booking_service
For production deployment, adjust the following settings:
- Set
DEBUG=False
in.env
- Configure proper
ALLOWED_HOSTS
- Set up a production-grade database
- Use a reverse proxy (Nginx/Apache)
- Configure proper SSL certificates
-
Install Gunicorn:
pip install gunicorn
-
Run with Gunicorn:
gunicorn booking.wsgi:application --bind 0.0.0.0:8000
-
Set up Supervisor to manage processes:
[program:ec2booking] command=/path/to/venv/bin/gunicorn booking.wsgi:application --bind 0.0.0.0:8000 directory=/path/to/project user=www-data autostart=true autorestart=true redirect_stderr=true stdout_logfile=/var/log/ec2booking.log
The application can be run using Docker and Docker Compose for easier deployment.
docker-compose up --build
This will start the following services:
- Web application (Django)
- PostgreSQL database
- Redis for Celery
- Nginx as a reverse proxy
- Web interface:
http://localhost:80/booking/register/
- Admin interface:
http://localhost:80/admin/
Contributions are welcome! Please follow these steps:
- Fork the repository
- Create a feature branch (
git checkout -b feature/your-feature
) - Make your changes
- Run tests (
python manage.py test
) - Commit changes (
git commit -m 'Add some feature'
) - Push to the branch (
git push origin feature/your-feature
) - Create a Pull Request
Please make sure your code passes all tests and follows the project's coding style.
This project is licensed under the MIT License - see the LICENSE file for details.