Deploying a Django Application to AWS Fargate: A Step-by-Step Guide
AWS Fargate is a serverless compute engine for containers, offering an excellent platform for deploying Django applications without managing underlying servers. This guide outlines the step-by-step process for deploying a Django application to AWS Fargate.
Prerequisites
- Django Application: A working Django application.
- AWS Account: An active AWS account.
- Docker: Installed and configured.
- AWS CLI: Installed and configured with access keys.
- ECS CLI: Installed (optional but helpful).
- PostgreSQL: A database available (e.g., Amazon RDS).
Step 1: Prepare Your Django Application
1. Configure Settings:
- Set ALLOWED_HOSTS to include your domain or * for testing purposes.
- Configure static files and media files to use AWS S3 or another cloud storage.
- Update the database settings to use your external database.
2. Collect Static Files:
python manage.py collectstatic
3. Install Required Libraries:
Ensure your requirements.txt includes:
gunicorn
psycopg2-binary
Step 2: Containerize the Application
1. Create a Dockerfile:
FROM python:3.9-slim
WORKDIR /app
COPY requirements.txt requirements.txt
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
RUN python manage.py collectstatic --noinput
CMD ["gunicorn", "myproject.wsgi:application", "--bind", "0.0.0.0:8000"]
2. Create a .dockerignore File:
__pycache__
*.pyc
*.pyo
.env
staticfiles/
3. Build and Tag the Docker Image:
docker build -t my-django-app .
4. Test Locally:
docker run -p 8000:8000 my-django-app
Step 3: Push Docker Image to Amazon ECR
1. Create an ECR Repository:
aws ecr create-repository --repository-name my-django-app
2. Authenticate Docker with ECR:
aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin <ECR_URI>
3. Push the Image:
docker tag my-django-app:latest <ECR_URI>/my-django-app:latest
docker push <ECR_URI>/my-django-app:latest
Step 4: Configure AWS ECS and Fargate
1. Create a Cluster:
aws ecs create-cluster --cluster-name my-django-cluster
2. Create a Task Definition:
{
"family": "my-django-task",
"containerDefinitions": [
{
"name": "my-django-container",
"image": "<ECR_URI>/my-django-app:latest",
"memory": 512,
"cpu": 256,
"essential": true,
"portMappings": [
{
"containerPort": 8000,
"hostPort": 8000,
"protocol": "tcp"
}
]
}
],
"networkMode": "awsvpc",
"requiresCompatibilities": ["FARGATE"],
"cpu": "256",
"memory": "512"
}
Save this JSON and register it:
aws ecs register-task-definition --cli-input-json file://task-definition.json
3. Create a Service:
aws ecs create-service \
--cluster my-django-cluster \
--service-name my-django-service \
--task-definition my-django-task \
--desired-count 1 \
--launch-type FARGATE \
--network-configuration "awsvpcConfiguration={subnets=[subnet-12345],securityGroups=[sg-12345],assignPublicIp=ENABLED}"
Step 5: Set Up a Load Balancer
1. Create an Application Load Balancer (ALB):
- Navigate to the EC2 Console > Load Balancers > Create Load Balancer.
- Configure the ALB with public subnets.
2. Register Target Group:
- Add the ECS service as a target for the ALB.
3. Update ECS Service:
aws ecs update-service --cluster my-django-cluster --service my-django-service --force-new-deployment
Step 6: Test the Deployment
Access your Django application via the DNS of the ALB. Verify that everything works as expected.
Step 7: Automate Deployment (Optional)
Use AWS CodePipeline or GitHub Actions to automate future deployments. Configure the pipeline to rebuild and deploy the Docker image on code change.