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.
- Overview
- Prerequisites
- Project Structure
- Django Web Framework
- Docker & Containerization
- AWS ECR Setup
- AWS ECS Deployment
- Security & Best Practices
- Monitoring & Logging
- Troubleshooting
- Cost Optimization
- Alternative Deployments
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
- 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
- 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
# 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
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 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.
- 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
- 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 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.
- 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
- Write Dockerfile: Define application environment
- Build Image: Create container image with dependencies
- Test Locally: Verify container works as expected
- Push to Registry: Store in ECR for deployment
- Deploy: Run containers in ECS
- Use multi-stage builds for smaller images
- Leverage layer caching effectively
- Use specific base image versions
- Minimize attack surface
- Optimize for production performance
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.
- 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
# Using AWS CLI
aws ecr create-repository \
--repository-name django-app \
--image-scanning-configuration scanOnPush=true \
--image-tag-mutability MUTABLE \
--region us-east-1{
"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"
]
}
]
}# Build the Docker image docker build -t django-app:latest . # Verify image creation docker images | grep django-app
# 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 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
# 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"
}
}
]
}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.
- Full Control: Access to underlying EC2 instances
- Cost Optimization: Better for sustained workloads
- Customization: Can install additional software
- Networking: Direct control over networking configuration
- 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
# Create ECS cluster using AWS CLI
aws ecs create-cluster \
--cluster-name django-cluster \
--service-connect default \
--region us-east-1{
"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"
}
}
}
]
}# 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
# 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
# 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/AmazonECSTaskExecutionRolePolicyaws ecs register-task-definition --cli-input-json file://task-definition.json
# 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
# 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
- Use non-root users in containers
- Scan images for vulnerabilities
- Implement resource limits to prevent DoS attacks
- Use secrets management instead of environment variables
- IAM Roles: Principle of least privilege
- Security Groups: Network-level security
- VPC: Isolated network environment
- Encryption: Data at rest and in transit
# 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
{
"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"
}
}
}# 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 }
- CPU and Memory utilization
- Request/response times
- Error rates
- Database connections
- Container restart counts
# 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
# 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
- Monitor CloudWatch metrics
- Check database connections
- Review container resource limits
- Analyze application logs
- 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
- Implement lifecycle policies to clean up old images
- Use image scanning only when needed
- Optimize image sizes with multi-stage builds
- Simplified deployment process
- Built-in load balancing and auto-scaling
- Managed platform updates
- Less configuration required
- More complex but highly flexible
- Portability across cloud providers
- Rich ecosystem and tooling
- Better for microservices architecture
- AWS Lambda for API endpoints
- Amplify for full-stack applications
- App Runner for containerized apps
Congratulations! You have successfully deployed your Django Application on AWS cloud using ECS and ECR with production-ready configurations.
- Check Load Balancer DNS in browser
- Verify SSL certificate is working
- Test application functionality
- Monitor CloudWatch metrics
- Review security configurations
- 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
- 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! ๐
This project is crafted by Harshhaa ๐ก.
Iโd love to hear your feedback! Feel free to share your thoughts.
๐ง Connect with me:
- GitHub: @NotHarshhaa
- Blog: ProDevOpsGuy
- Telegram Community: Join Here
- LinkedIn: Harshhaa Vardhan Reddy
If you found this helpful, consider starring โญ the repository and sharing it with your network! ๐