Complete Django Deployment on AWS - Complete Guide

 

Deploy Django on AWS for Free (Complete Guide)

This document provides a complete, beginner-friendly guide to creating a Django project, containerizing it with Docker, connecting it to PostgreSQL on AWS RDS, and deploying it to AWS ECS Fargate using the AWS Free Tier.


1. Create a Simple Django Project

Create folder & virtual environment

mkdir myproject
cd myproject
python3 -m venv venv
source venv/bin/activate

Install dependencies

pip install django psycopg2-binary gunicorn

Start Django project

django-admin startproject core .

Update ALLOWED_HOSTS in core/settings.py

ALLOWED_HOSTS = ['*']

Test locally

python manage.py runserver

2. Containerize Django with Docker

Create Dockerfile

FROM python:3.11-slim

WORKDIR /app
COPY requirements.txt requirements.txt
RUN pip install --no-cache-dir -r requirements.txt

COPY . .
CMD ["gunicorn", "core.wsgi:application", "--bind", "0.0.0.0:8000"]

Create requirements.txt

django
gunicorn
psycopg2-binary

Build and test the image

docker build -t my-django-app .
docker run -p 8000:8000 my-django-app

3. Create PostgreSQL Database on AWS RDS (Free Tier)

  1. Go to AWS Console → RDS

  2. Choose DB Engine → PostgreSQL

  3. Select Template → Free Tier

  4. Choose db.t3.micro

  5. Storage: 20GB

  6. Public access → Yes (for initial setup)

  7. Add security group inbound rule:

    • Port 5432

    • Source → Your IP

Copy the generated DB endpoint.


4. Configure Django to Use AWS RDS

Update DATABASES in core/settings.py:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'yourdbname',
        'USER': 'yourdbuser',
        'PASSWORD': 'yourpassword',
        'HOST': 'your-rds-endpoint',
        'PORT': '5432',
    }
}

Run migrations:

python manage.py migrate

5. Push Docker Image to AWS ECR

Create ECR Repository

  • AWS Console → ECR → Create Repository

  • Name: django-app

Login to ECR

aws ecr get-login-password --region ap-south-1 \
| docker login --username AWS --password-stdin <account-id>.dkr.ecr.ap-south-1.amazonaws.com

Tag and Push the Image

docker tag my-django-app:latest <repo-url>:latest
docker push <repo-url>:latest

6. Deploy Django on AWS ECS Fargate (Free Tier)

Create ECS Cluster

  • AWS Console → ECS → Create Cluster

  • Choose Fargate

Create Task Definition

  • Launch type: Fargate

  • Container image: ECR URL

  • Port: 8000

  • Memory: 0.5 GB

  • CPU: 0.25 vCPU

Create Service

  • Desired tasks: 1

  • Network: Public IP enabled

  • Security Group: allow port 8000 (or 80)

Once running, ECS provides a public IP.

Open in browser:

http://<ecs-public-ip>:8000

7. Cost Management (Free Tier Safe)

To stay free:

  • Use 1 ECS task only

  • Use db.t3.micro RDS under Free Tier

  • Avoid Load Balancers as they are NOT free

This setup stays fully free for the first 12 months.


8. Optional: docker-compose.yml (Local Dev Setup)

version: '3'
services:
  web:
    build: .
    ports:
      - "8000:8000"
    depends_on:
      - db
  db:
    image: postgres:15
    environment:
      POSTGRES_DB: mydb
      POSTGRES_USER: user
      POSTGRES_PASSWORD: pass
    ports:
      - "5432:5432"

9. Optional: Terraform Template for Infra (IaC)

(You can extend this later)

provider "aws" {
  region = "ap-south-1"
}

10. Optional: CI/CD with GitHub Actions

name: Deploy to AWS

on:
  push:
    branches: [ "main" ]

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v3

    - name: Build Docker image
      run: docker build -t my-django-app .

    - name: Push to ECR
      run: echo "Configure AWS & Push"

(This can be expanded as needed.)


Conclusion

You have a complete, production-style deployment pipeline using AWS Free Tier:

  • Django

  • Docker

  • PostgreSQL RDS

  • ECS Fargate

  • ECR

Comments

Popular posts from this blog

Complete Guide: Deploy Django + Docker + PostgreSQL for FREE using Render + Neon.tech