Deploy Django on AWS with GitHub Actions

🎸 Django ☁️ Amazon Web Services ⚙️ GitHub Actions

Configuration Files

2 files
Production-ready configuration files with detailed comments and best practices. Each file works together as a complete deployment solution.
Production-ready Django Dockerfile
dockerfile
FROM python:3.11-slim

ENV PYTHONUNBUFFERED=1 \
    PYTHONDONTWRITEBYTECODE=1

WORKDIR /app

# Install system dependencies
RUN apt-get update && apt-get install -y \
    postgresql-client \
    && rm -rf /var/lib/apt/lists/*

# Install Python dependencies
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

# Copy project
COPY . .

# Collect static files
RUN python manage.py collectstatic --noinput

# Run as non-root
RUN useradd -m django && chown -R django:django /app
USER django

EXPOSE 8000

CMD ["gunicorn", "config.wsgi:application", "--bind", "0.0.0.0:8000", "--workers", "4"]
Django production dependencies
text
Django==5.0
gunicorn==21.2.0
psycopg2-binary==2.9.9
whitenoise==6.6.0
django-environ==0.11.2

Prerequisites

  • AWS account with ECS, ECR, RDS access
  • PostgreSQL database configured
  • S3 bucket for static/media files
  • GitHub repository

Deployment Steps

  • Set up RDS PostgreSQL database
  • Create S3 bucket for static files
  • Create ECR repository and ECS cluster
  • Configure environment variables in ECS task definition
  • Add AWS credentials to GitHub Secrets
  • Push to trigger deployment

Additional Notes

  • Use WhiteNoise for serving static files
  • Configure ALLOWED_HOSTS in production
  • Use django-environ for environment variables
  • Set DEBUG=False in production