Deploy with Elastic Beanstalk

Deploy your Django application to AWS cloud

☁️ What is AWS Elastic Beanstalk?

AWS Elastic Beanstalk is a cloud platform that automatically handles deployment, scaling, and management of your Django applications. Simply upload your code and Elastic Beanstalk handles everything else.


# Install EB CLI
pip install awsebcli

# Initialize your project
eb init -p python-3.9 my-django-app
                                    

Deployment Steps

📦

Install EB CLI

Set up AWS command line tools

pip install awsebcli
⚙️

Configure Project

Initialize Elastic Beanstalk settings

eb init
🚀

Create Environment

Set up your deployment environment

eb create
📤

Deploy App

Upload and launch your application

eb deploy

🔹 Create Requirements File

Elastic Beanstalk needs a requirements.txt file listing all your Django dependencies. This file tells AWS which Python packages to install for your application to run properly.

# Generate requirements.txt
pip freeze > requirements.txt

Example requirements.txt:

Django==4.2.0
gunicorn==20.1.0
psycopg2-binary==2.9.5
python-decouple==3.8

🔹 Configure Django Settings

Update your Django settings.py to work with Elastic Beanstalk. You need to configure allowed hosts and static files properly for production deployment.

# settings.py
import os

# Allow EB domain
ALLOWED_HOSTS = [
    'your-app.elasticbeanstalk.com',
    'localhost'
]

# Static files configuration
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATIC_URL = '/static/'

# Security settings
DEBUG = False
SECRET_KEY = os.environ.get('SECRET_KEY')

🔹 Create .ebextensions Config

The .ebextensions folder contains configuration files that customize your Elastic Beanstalk environment. Create a django.config file to specify your WSGI application path.

# .ebextensions/django.config
option_settings:
  aws:elasticbeanstalk:container:python:
    WSGIPath: myproject.wsgi:application
  aws:elasticbeanstalk:environment:proxy:staticfiles:
    /static: staticfiles

Folder Structure:

myproject/
├── .ebextensions/
│   └── django.config
├── myproject/
│   ├── settings.py
│   └── wsgi.py
├── manage.py
└── requirements.txt

🔹 Deploy Your Application

Follow these commands to initialize, create, and deploy your Django application to AWS Elastic Beanstalk. The process takes a few minutes to complete.

# Initialize EB in your project
eb init -p python-3.9 my-django-app --region us-east-1

# Create environment and deploy
eb create django-env

# Open your deployed app
eb open

# Deploy updates
eb deploy

Expected Output:

✓ Environment created successfully

✓ Application deployed

URL: http://django-env.elasticbeanstalk.com

🔹 Useful EB Commands

Common Elastic Beanstalk CLI commands for managing your deployed Django application:

  • eb status: Check environment status
  • eb logs: View application logs
  • eb ssh: Connect to your instance
  • eb terminate: Delete environment
  • eb setenv KEY=value: Set environment variables
# Check deployment status
eb status

# View recent logs
eb logs

# Set environment variable
eb setenv DEBUG=False SECRET_KEY=your-secret-key

🧠 Test Your Knowledge

Which file lists all Python dependencies for deployment?