Django Config File

Creating django.config for Elastic Beanstalk deployment

⚙️ What is django.config?

The django.config file tells AWS Elastic Beanstalk how to run your Django application. It specifies the WSGI application path, static files location, and other deployment configurations for proper server setup.


option_settings:
  aws:elasticbeanstalk:container:python:
    WSGIPath: myproject.wsgi:application
                                    

Config File Components

🌐

WSGI Path

Points to Django application

Required Entry point Core
📁

Static Files

Configure static file serving

CSS/JS Images Assets
🔧

Environment

Set environment variables

Secrets Config Settings
📜

Commands

Run deployment commands

Migrations Collectstatic Custom

🔹 Basic django.config Structure

Create a basic configuration file for deploying Django to Elastic Beanstalk. This file should be placed in the .ebextensions folder at your project root.

# .ebextensions/django.config
option_settings:
  aws:elasticbeanstalk:container:python:
    WSGIPath: myproject.wsgi:application
  aws:elasticbeanstalk:application:environment:
    DJANGO_SETTINGS_MODULE: myproject.settings
    PYTHONPATH: /var/app/current:$PYTHONPATH
  aws:elasticbeanstalk:container:python:staticfiles:
    /static/: staticfiles/

File Location:

  • Create folder: .ebextensions/ in project root
  • Create file: .ebextensions/django.config
  • Use YAML format (indentation matters!)

🔹 Complete django.config Example

A comprehensive configuration file that handles static files, runs migrations, collects static files, and sets up environment variables for a production-ready Django deployment.

# .ebextensions/django.config
option_settings:
  # WSGI Configuration
  aws:elasticbeanstalk:container:python:
    WSGIPath: myproject.wsgi:application
  
  # Environment Variables
  aws:elasticbeanstalk:application:environment:
    DJANGO_SETTINGS_MODULE: myproject.settings
    PYTHONPATH: /var/app/current:$PYTHONPATH
    DEBUG: False
  
  # Static Files Mapping
  aws:elasticbeanstalk:container:python:staticfiles:
    /static/: staticfiles/
    /media/: media/

# Container Commands (run after app deployment)
container_commands:
  01_migrate:
    command: "source /var/app/venv/*/bin/activate && python manage.py migrate --noinput"
    leader_only: true
  02_collectstatic:
    command: "source /var/app/venv/*/bin/activate && python manage.py collectstatic --noinput"
    leader_only: true
  03_createsu:
    command: "source /var/app/venv/*/bin/activate && python manage.py createsu"
    leader_only: true

🔹 Static Files Configuration

Configure how Elastic Beanstalk serves your static files (CSS, JavaScript, images). This maps URL paths to filesystem directories.

# Static files configuration
option_settings:
  aws:elasticbeanstalk:container:python:staticfiles:
    /static/: staticfiles/
    /media/: media/
    /static/admin/: staticfiles/admin/
    /static/rest_framework/: staticfiles/rest_framework/

Static Files Setup:

  • /static/: URL path users access
  • staticfiles/: Directory on server
  • Run collectstatic before deployment
  • Configure STATIC_ROOT in settings.py

🔹 Container Commands

Container commands run after your application is deployed but before it starts serving traffic. Use them for database migrations, static file collection, and other setup tasks.

container_commands:
  01_migrate:
    command: "source /var/app/venv/*/bin/activate && python manage.py migrate --noinput"
    leader_only: true
  02_collectstatic:
    command: "source /var/app/venv/*/bin/activate && python manage.py collectstatic --noinput"
    leader_only: true
  03_create_superuser:
    command: "source /var/app/venv/*/bin/activate && python manage.py shell -c \"from django.contrib.auth import get_user_model; User = get_user_model(); User.objects.filter(username='admin').exists() or User.objects.create_superuser('admin', '[email protected]', 'password')\""
    leader_only: true

Command Options:

  • leader_only: Run on one instance only
  • command: Shell command to execute
  • Numbers (01_, 02_): Execution order

🔹 Environment Variables

Set environment variables directly in the config file or use EB CLI for sensitive data:

# In django.config
option_settings:
  aws:elasticbeanstalk:application:environment:
    DJANGO_SETTINGS_MODULE: myproject.settings
    DEBUG: False
    ALLOWED_HOSTS: .elasticbeanstalk.com
    DATABASE_URL: postgres://user:pass@host:5432/db
# Or use EB CLI (recommended for secrets)
eb setenv SECRET_KEY=your-secret-key
eb setenv DEBUG=False
eb setenv DATABASE_URL=postgres://...

🔹 Database Configuration

Configure RDS database connection for your Django app:

# django.config
option_settings:
  aws:elasticbeanstalk:application:environment:
    RDS_DB_NAME: mydatabase
    RDS_USERNAME: myuser
    RDS_PASSWORD: mypassword
    RDS_HOSTNAME: mydb.xxxxx.us-east-1.rds.amazonaws.com
    RDS_PORT: 5432
# settings.py - Use these environment variables
import os

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': os.environ.get('RDS_DB_NAME'),
        'USER': os.environ.get('RDS_USERNAME'),
        'PASSWORD': os.environ.get('RDS_PASSWORD'),
        'HOST': os.environ.get('RDS_HOSTNAME'),
        'PORT': os.environ.get('RDS_PORT'),
    }
}

🔹 Complete Project Structure

Your project should have this structure for Elastic Beanstalk deployment:

myproject/
├── .ebextensions/
│   └── django.config
├── myproject/
│   ├── __init__.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
├── myapp/
│   ├── migrations/
│   ├── models.py
│   └── views.py
├── staticfiles/
├── media/
├── requirements.txt
├── manage.py
└── .gitignore

🔹 Common Issues & Solutions

Troubleshooting:

  • WSGI Path Error: Check WSGIPath matches your project structure
  • Static Files Not Loading: Verify STATIC_ROOT and run collectstatic
  • Migration Errors: Ensure database is accessible
  • YAML Syntax: Check indentation (use spaces, not tabs)
  • Commands Failing: Test commands locally first

🧠 Test Your Knowledge

Where should you place the django.config file?