| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Name | Name | Last commit date | ||
|---|---|---|---|---|
parent directory.. | ||||
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.
This project provides a complete DevOps pipeline for deploying Django applications on AWS cloud infrastructure. The solution includes:
# 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 runservermyproject/ ├── 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.
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.
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.
# 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.
# 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# 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
}# 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" \
--interactiveCongratulations! You have successfully deployed your Django Application on AWS cloud using ECS and ECR with production-ready configurations.
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:
If you found this helpful, consider starring ⭐ the repository and sharing it with your network! 🚀
| Back | FazBrowse Home | New Git URL |