Django Elastic Beanstalk
Deploy Django apps to AWS easily
☁️ What is Elastic Beanstalk?
AWS Elastic Beanstalk is a platform that automatically handles deployment, scaling, and management of your Django applications. Simply upload your code and Beanstalk manages everything else.
# Install EB CLI
pip install awsebcli
# Initialize EB in your project
eb init -p python-3.9 my-django-app
# Create and deploy
eb create my-env
Elastic Beanstalk Features
Easy Deployment
Deploy with simple commands
Auto Scaling
Handles traffic automatically
Managed Platform
AWS handles infrastructure
Cost Effective
Pay only for resources used
🔹 Prerequisites
Before deploying to Elastic Beanstalk, ensure you have these requirements set up. You'll need an AWS account and the EB CLI tool installed on your computer.
What You Need:
- AWS Account: Sign up at aws.amazon.com
- EB CLI: Install with pip
- Django Project: Working Django application
- Git: Version control (optional but recommended)
# Install EB CLI
pip install awsebcli
# Verify installation
eb --version
🔹 Configure Django Settings
Update your Django settings to work with Elastic Beanstalk. These configurations ensure your app runs properly in the AWS environment with correct security and database settings.
# settings.py
import os
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = os.environ.get('SECRET_KEY', 'your-default-secret-key')
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = os.environ.get('DEBUG', 'False') == 'True'
ALLOWED_HOSTS = [
'localhost',
'127.0.0.1',
'.elasticbeanstalk.com', # Allow EB domains
os.environ.get('ALLOWED_HOST', '')
]
# Database configuration
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': os.environ.get('RDS_DB_NAME', 'mydatabase'),
'USER': os.environ.get('RDS_USERNAME', 'myuser'),
'PASSWORD': os.environ.get('RDS_PASSWORD', 'mypassword'),
'HOST': os.environ.get('RDS_HOSTNAME', 'localhost'),
'PORT': os.environ.get('RDS_PORT', '5432'),
}
}
# Static files
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATIC_URL = '/static/'
🔹 Initialize Elastic Beanstalk
Set up your project for Elastic Beanstalk deployment. This creates configuration files and connects your project to AWS.
# Navigate to your project directory
cd my-django-project
# Initialize EB (interactive setup)
eb init
# Or specify options directly
eb init -p python-3.9 my-django-app --region us-east-1
# This creates .elasticbeanstalk/config.yml
During eb init, you'll be asked:
- Select a region (e.g., us-east-1)
- Application name
- Python version
- SSH access (optional)
🔹 Create Environment
Create and launch your application environment on AWS. This sets up servers, load balancers, and all infrastructure needed to run your Django app.
# Create environment and deploy
eb create my-environment
# Or with specific options
eb create production-env --database.engine postgres --database.username myuser
# Check status
eb status
# Open in browser
eb open
Output:
Creating application version archive "app-231015_123456".
Uploading my-django-app/app-231015_123456.zip to S3...
Environment details for: production-env
Application name: my-django-app
Region: us-east-1
Platform: Python 3.9
Status: Ready
Health: Green
🔹 Deploy Updates
Deploy code changes to your running environment with a single command:
# Deploy current code
eb deploy
# Deploy with message
eb deploy -m "Added new feature"
# View logs
eb logs
# SSH into instance
eb ssh
🔹 Environment Variables
Set environment variables for your Django app securely through EB:
# Set environment variables
eb setenv SECRET_KEY=your-secret-key DEBUG=False
# Set multiple variables
eb setenv \
SECRET_KEY=your-secret-key \
DEBUG=False \
ALLOWED_HOST=myapp.com
# View current variables
eb printenv
🔹 Common EB Commands
Essential commands for managing your Elastic Beanstalk application:
Useful Commands:
- eb status: Check environment status
- eb health: View health information
- eb logs: View application logs
- eb open: Open app in browser
- eb terminate: Delete environment
- eb list: List all environments