FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

DevOps-Projects/DevOps-Project-04 at master · NotHarshhaa/DevOps-Projects · GitHub

Latest commit

 

History

History

README.md

Production-Ready Django Deployment on AWS: Complete ECS & ECR DevOps Pipeline

This comprehensive guide demonstrates how to deploy a Django-based production application onto AWS using ECS (Elastic Container Service) and ECR (Elastic Container Registry). We'll cover the complete DevOps pipeline from containerization to deployment, including security best practices, monitoring setup, and production optimization.

📋 Table of Contents

🎯 Overview

This project provides a complete DevOps pipeline for deploying Django applications on AWS cloud infrastructure. The solution includes:

  • Containerization: Docker-based application packaging
  • Registry Management: ECR for secure image storage
  • Orchestration: ECS for container management
  • Scalability: Auto-scaling and load balancing
  • Security: IAM roles, security groups, and secrets management
  • Monitoring: CloudWatch integration and health checks

📚 Prerequisites

Technical Requirements

  • Python 3.9+ installed locally
  • Docker Desktop or Docker Engine
  • AWS Account with appropriate permissions
  • AWS CLI configured with credentials
  • Django framework knowledge
  • Basic understanding of containers and cloud concepts

AWS Permissions Required

  • Amazon ECR: Full access
  • Amazon ECS: Full access
  • Amazon EC2: Full access
  • IAM: Basic permissions for role creation
  • CloudWatch: Read/Write permissions
  • VPC: Network configuration permissions

Development Environment Setup

# Install Django and create project
pip install django
django-admin startproject myproject
cd myproject

# Create requirements file
pip freeze > requirements.txt

# Test locally
python manage.py runserver

🏗️ Project Structure

myproject/
├── myproject/
│   ├── __init__.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
├── apps/
│   ├── __init__.py
│   └── [your-apps]
├── static/
├── media/
├── templates/
├── requirements.txt
├── Dockerfile
├── docker-compose.yml
├── .dockerignore
├── .env.example
└── README.md

🐍 Django Web Framework

Django is a high-level Python web framework that encourages rapid development and clean, pragmatic design. Built by experienced developers, it takes care of much of the hassle of web development, so you can focus on writing your app without needing to reinvent the wheel.

Key Features

  • Batteries-included: ORM, authentication, admin panel, forms
  • Security: Built-in protection against CSRF, XSS, SQL injection
  • Scalability: Designed for high-traffic applications
  • Documentation: Comprehensive docs and active community
  • MVT Architecture: Model-View-Template pattern

Production Considerations

  • Static files handling: Use AWS S3 + CloudFront
  • Database: Amazon RDS (PostgreSQL/MySQL)
  • Caching: Redis/ElastiCache
  • Session storage: Redis or database
  • Environment variables: AWS Secrets Manager

🐳 Docker & Containerization

What is Docker?

Docker is an open platform for developing, shipping, and running applications in containers. Containerization provides a lightweight, portable way to package applications with all their dependencies, ensuring consistency across different environments.

Benefits of Containerization

  • Portability: Run anywhere Docker is installed
  • Isolation: Applications don't interfere with each other
  • Scalability: Easy to scale horizontally
  • Version Control: Image versioning and rollbacks
  • Resource Efficiency: Shared OS kernel, lightweight

Docker Workflow

  1. Write Dockerfile: Define application environment
  2. Build Image: Create container image with dependencies
  3. Test Locally: Verify container works as expected
  4. Push to Registry: Store in ECR for deployment
  5. Deploy: Run containers in ECS

Dockerfile Best Practices

  • Use multi-stage builds for smaller images
  • Leverage layer caching effectively
  • Use specific base image versions
  • Minimize attack surface
  • Optimize for production performance

📦 AWS Elastic Container Registry (ECR)

Amazon Elastic Container Registry (Amazon ECR) is a fully managed container image registry service that makes it easy to store, manage, share, and deploy your container images. ECR eliminates the need to operate your own container repositories or worry about scaling the underlying infrastructure.

Key Features

  • Fully Managed: No infrastructure to maintain
  • Secure: IAM integration and encryption
  • Scalable: Automatic scaling based on demand
  • Integrated: Works seamlessly with ECS and EKS
  • Cost-effective: Pay only for storage and data transfer
  • Vulnerability Scanning: Automated security scans

ECR Repository Setup

Step 1: Create Repository

# Using AWS CLI
aws ecr create-repository \
    --repository-name django-app \
    --image-scanning-configuration scanOnPush=true \
    --image-tag-mutability MUTABLE \
    --region us-east-1

Step 2: Configure Repository Policies

{
  "Version": "2008-10-17",
  "Statement": [
    {
      "Sid": "AllowPull",
      "Effect": "Allow",
      "Principal": {
        "AWS": "arn:aws:iam::account-id:user/ecs-user"
      },
      "Action": [
        "ecr:GetDownloadUrlForLayer",
        "ecr:BatchGetImage",
        "ecr:BatchCheckLayerAvailability"
      ]
    }
  ]
}

ECR Image Management

Build Docker Image

# Build the Docker image
docker build -t django-app:latest .

# Verify image creation
docker images | grep django-app

Authenticate with ECR

# Get ECR login password
aws ecr get-login-password --region us-east-1 | \
    docker login --username AWS --password-stdin \
    123456789012.dkr.ecr.us-east-1.amazonaws.com

Tag and Push Image

# Tag image for ECR
docker tag django-app:latest \
    123456789012.dkr.ecr.us-east-1.amazonaws.com/django-app:latest

# Push to ECR
docker push 123456789012.dkr.ecr.us-east-1.amazonaws.com/django-app:latest

Lifecycle Policies

# Create lifecycle policy to clean up old images
aws ecr put-lifecycle-policy \
    --repository-name django-app \
    --lifecycle-policy-text 'file://lifecycle-policy.json'

lifecycle-policy.json:

{
  "rules": [
    {
      "rulePriority": 1,
      "description": "Keep last 10 images",
      "selection": {
        "tagStatus": "tagged",
        "tagPrefixList": ["v"],
        "countType": "imageCountMoreThan",
        "countNumber": 10
      },
      "action": {
        "type": "expire"
      }
    }
  ]
}

🚀 AWS Elastic Container Service (ECS)

Amazon Elastic Container Service (ECS) is a highly scalable, high-performance container orchestration service that supports Docker containers and allows you to easily run applications on a managed cluster of Amazon EC2 instances or AWS Fargate.

ECS Launch Types

EC2 Launch Type

  • Full Control: Access to underlying EC2 instances
  • Cost Optimization: Better for sustained workloads
  • Customization: Can install additional software
  • Networking: Direct control over networking configuration

Fargate Launch Type

  • Serverless: No EC2 instances to manage
  • Pay-per-use: Billing based on vCPU and memory
  • Isolation: Each task gets its own isolated environment
  • Simplicity: Reduced operational overhead

Key ECS Components

1. ECS Cluster

# Create ECS cluster using AWS CLI
aws ecs create-cluster \
    --cluster-name django-cluster \
    --service-connect default \
    --region us-east-1

2. Task Definition

{
  "family": "django-task",
  "networkMode": "awsvpc",
  "requiresCompatibilities": ["FARGATE"],
  "cpu": "256",
  "memory": "512",
  "executionRoleArn": "arn:aws:iam::account:role/ecsTaskExecutionRole",
  "containerDefinitions": [
    {
      "name": "django-container",
      "image": "123456789012.dkr.ecr.us-east-1.amazonaws.com/django-app:latest",
      "portMappings": [
        {
          "containerPort": 8000,
          "protocol": "tcp"
        }
      ],
      "environment": [
        {
          "name": "DJANGO_SETTINGS_MODULE",
          "value": "myproject.settings.production"
        }
      ],
      "logConfiguration": {
        "logDriver": "awslogs",
        "options": {
          "awslogs-group": "/ecs/django-app",
          "awslogs-region": "us-east-1",
          "awslogs-stream-prefix": "ecs"
        }
      }
    }
  ]
}

3. Service Definition

# Create ECS service
aws ecs create-service \
    --cluster django-cluster \
    --service-name django-service \
    --task-definition django-task \
    --desired-count 2 \
    --launch-type FARGATE \
    --network-configuration "awsvpcConfiguration={subnets=[subnet-12345,subnet-67890],securityGroups=[sg-12345],assignPublicIp=ENABLED}" \
    --deployment-configuration "maximumPercent=200,minimumHealthyPercent=100" \
    --health-check-grace-period-seconds 30

ECS Deployment Steps

Step 1: Create VPC and Networking

# Create VPC
aws ec2 create-vpc --cidr-block 10.0.0.0/16 --tag-specifications 'ResourceType=vpc,Tags=[{Key=Name,Value=django-vpc}]'

# Create subnets
aws ec2 create-subnet --vpc-id vpc-12345 --cidr-block 10.0.1.0/24 --availability-zone us-east-1a
aws ec2 create-subnet --vpc-id vpc-12345 --cidr-block 10.0.2.0/24 --availability-zone us-east-1b

# Create security groups
aws ec2 create-security-group --group-name django-sg --description "Security group for Django app" --vpc-id vpc-12345

Step 2: Create IAM Roles

# Create task execution role
aws iam create-role --role-name ecsTaskExecutionRole --assume-role-policy-document file://trust-policy.json
aws iam attach-role-policy --role-name ecsTaskExecutionRole --policy-arn arn:aws:iam::aws:policy/service-role/AmazonECSTaskExecutionRolePolicy

Step 3: Register Task Definition

aws ecs register-task-definition --cli-input-json file://task-definition.json

Step 4: Create Service with Load Balancer

# Create Application Load Balancer
aws elbv2 create-load-balancer \
    --name django-alb \
    --subnets subnet-12345 subnet-67890 \
    --security-groups sg-12345

# Create target group
aws elbv2 create-target-group \
    --name django-tg \
    --protocol HTTP \
    --port 8000 \
    --vpc-id vpc-12345 \
    --target-type ip

# Create service with load balancer
aws ecs create-service \
    --cluster django-cluster \
    --service-name django-service \
    --task-definition django-task \
    --desired-count 2 \
    --launch-type FARGATE \
    --load-balancers targetGroupArn=arn:aws:elasticloadbalancing:region:account:targetgroup/django-tg,containerName=django-container,containerPort=8000

Step 5: Configure Auto Scaling

# Create auto scaling target
aws application-autoscaling register-scalable-target \
    --service-namespace ecs \
    --resource-id service/django-cluster/django-service \
    --scalable-dimension ecs:service:DesiredCount \
    --min-capacity 1 \
    --max-capacity 10

# Create scaling policy
aws application-autoscaling put-scaling-policy \
    --service-namespace ecs \
    --resource-id service/django-cluster/django-service \
    --scalable-dimension ecs:service:DesiredCount \
    --policy-name django-scale-out \
    --policy-type TargetTrackingScaling \
    --target-tracking-scaling-policy-configuration file://scaling-policy.json

🔒 Security & Best Practices

Container Security

  • Use non-root users in containers
  • Scan images for vulnerabilities
  • Implement resource limits to prevent DoS attacks
  • Use secrets management instead of environment variables

AWS Security

  • IAM Roles: Principle of least privilege
  • Security Groups: Network-level security
  • VPC: Isolated network environment
  • Encryption: Data at rest and in transit

Django Security Settings

# production.py
SECURE_SSL_REDIRECT = True
SECURE_HSTS_SECONDS = 31536000
SECURE_HSTS_INCLUDE_SUBDOMAINS = True
SECURE_HSTS_PRELOAD = True
SECURE_CONTENT_TYPE_NOSNIFF = True
SECURE_BROWSER_XSS_FILTER = True
X_FRAME_OPTIONS = 'DENY'
CSRF_COOKIE_SECURE = True
SESSION_COOKIE_SECURE = True

📊 Monitoring & Logging

CloudWatch Integration

{
  "logConfiguration": {
    "logDriver": "awslogs",
    "options": {
      "awslogs-group": "/ecs/django-app",
      "awslogs-region": "us-east-1",
      "awslogs-stream-prefix": "ecs",
      "awslogs-datetime-format": "%Y-%m-%dT%H:%M:%S.%fZ"
    }
  }
}

Health Checks

# Configure health checks in task definition
"healthCheck": {
  "command": ["CMD-SHELL", "curl -f http://localhost:8000/health/ || exit 1"],
  "interval": 30,
  "timeout": 5,
  "retries": 3,
  "startPeriod": 60
}

Metrics to Monitor

  • CPU and Memory utilization
  • Request/response times
  • Error rates
  • Database connections
  • Container restart counts

🛠️ Troubleshooting

Common Issues

Container Won't Start

# Check task logs
aws logs get-log-events \
    --log-group-name /ecs/django-app \
    --log-stream-name ecs/django-container/abcdef123456

# Describe task failure
aws ecs describe-tasks --cluster django-cluster --tasks abcdef123456

Network Issues

# Check security group rules
aws ec2 describe-security-groups --group-ids sg-12345

# Test connectivity
aws ecs execute-command \
    --cluster django-cluster \
    --task abcdef123456 \
    --container django-container \
    --command "curl -I http://localhost:8000" \
    --interactive

Performance Issues

  • Monitor CloudWatch metrics
  • Check database connections
  • Review container resource limits
  • Analyze application logs

💰 Cost Optimization

ECS Cost Saving Tips

  • Use Fargate Spot for non-critical workloads
  • Implement auto-scaling to match demand
  • Right-size containers based on actual usage
  • Schedule scaling for predictable patterns

ECR Cost Management

  • Implement lifecycle policies to clean up old images
  • Use image scanning only when needed
  • Optimize image sizes with multi-stage builds

🔄 Alternative Deployments

AWS Elastic Beanstalk

  • Simplified deployment process
  • Built-in load balancing and auto-scaling
  • Managed platform updates
  • Less configuration required

Kubernetes (EKS)

  • More complex but highly flexible
  • Portability across cloud providers
  • Rich ecosystem and tooling
  • Better for microservices architecture

Serverless Options

  • AWS Lambda for API endpoints
  • Amplify for full-stack applications
  • App Runner for containerized apps

🎉 Success! 🎉

Congratulations! You have successfully deployed your Django Application on AWS cloud using ECS and ECR with production-ready configurations.

Verification Steps

  1. Check Load Balancer DNS in browser
  2. Verify SSL certificate is working
  3. Test application functionality
  4. Monitor CloudWatch metrics
  5. Review security configurations

Post-Deployment Checklist

  • SSL/TLS certificates configured
  • Database backups enabled
  • Monitoring alerts set up
  • Log rotation configured
  • Security groups reviewed
  • Auto-scaling policies tested
  • Disaster recovery plan documented

Next Steps

  • Implement CI/CD pipeline with AWS CodePipeline
  • Add comprehensive testing suite
  • Set up staging environment
  • Implement blue-green deployments
  • Add performance monitoring with APM tools

Happy Learning and Happy Deploying! 🚀

🛠️ Author & Community

This project is crafted by Harshhaa 💡.
I’d love to hear your feedback! Feel free to share your thoughts.

📧 Connect with me:


⭐ Support the Project

If you found this helpful, consider starring ⭐ the repository and sharing it with your network! 🚀

📢 Stay Connected


Back | FazBrowse Home | New Git URL