Deploy to AWS
7. Deploy to AWS - Complete Production Deployment Guide
Section titled “7. Deploy to AWS - Complete Production Deployment Guide”Step 1: AWS Account Setup and Prerequisites
Section titled “Step 1: AWS Account Setup and Prerequisites”Create AWS Account
Section titled “Create AWS Account”- Go to aws.amazon.com
- Create account or sign in
- Set up billing alerts in AWS Budgets
Install AWS CLI
Section titled “Install AWS CLI”# On macOSbrew install awscli
# On Ubuntusudo apt updatesudo apt install awscli
# On Windows (using Chocolatey)choco install awscli
# Verify installationaws --versionConfigure AWS CLI
Section titled “Configure AWS CLI”aws configure# Enter:# AWS Access Key ID: [Your Access Key]# AWS Secret Access Key: [Your Secret Key]# Default region: us-east-1# Default output format: jsonStep 2: Prepare Django Application for Production
Section titled “Step 2: Prepare Django Application for Production”Create Production Requirements
Section titled “Create Production Requirements”Create requirements/production.txt
Section titled “Create requirements/production.txt”mkdir requirementstouch requirements/{base.txt,production.txt,development.txt}requirements/base.txt
Section titled “requirements/base.txt”Django==4.2.7djangorestframework==3.14.0django-cors-headers==4.3.1django-filter==23.3djangorestframework-simplejwt==5.3.0celery==5.3.4redis==5.0.1flower==1.2.0psycopg2-binary==2.9.7Pillow==10.0.1gunicorn==21.2.0whitenoise==6.6.0drf-spectacular==0.26.5python-dotenv==1.0.0boto3==1.34.0django-storages==1.14.2requirements/production.txt
Section titled “requirements/production.txt”-r base.txt# Production specific packagessentry-sdk==1.40.0psutil==5.9.6defusedxml==0.7.1python-memcached==1.59Update Production Settings
Section titled “Update Production Settings”Create config/settings/production.py
Section titled “Create config/settings/production.py”"""Production settings for Django REST API"""import osimport sentry_sdkfrom sentry_sdk.integrations.django import DjangoIntegrationfrom pathlib import Pathfrom .base import *
# SECURITY WARNING: don't run with debug turned on in production!DEBUG = False
# SECURITY WARNING: keep the secret key used in production secret!SECRET_KEY = os.getenv('SECRET_KEY')
# Allowed hostsALLOWED_HOSTS = os.getenv('ALLOWED_HOSTS', '').split(',')
# DatabaseDATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql', 'NAME': os.getenv('DB_NAME'), 'USER': os.getenv('DB_USER'), 'PASSWORD': os.getenv('DB_PASSWORD'), 'HOST': os.getenv('DB_HOST'), 'PORT': os.getenv('DB_PORT', '5432'), 'CONN_MAX_AGE': 600, # 10 minutes connection persistence }}
# Security settingsSECURE_SSL_REDIRECT = TrueSESSION_COOKIE_SECURE = TrueCSRF_COOKIE_SECURE = TrueSECURE_BROWSER_XSS_FILTER = TrueSECURE_CONTENT_TYPE_NOSNIFF = TrueSECURE_HSTS_SECONDS = 31536000 # 1 yearSECURE_HSTS_INCLUDE_SUBDOMAINS = TrueSECURE_HSTS_PRELOAD = TrueX_FRAME_OPTIONS = 'DENY'
# Static filesSTATIC_URL = '/static/'STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
# Media files (if using S3)DEFAULT_FILE_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'
# AWS S3 ConfigurationAWS_ACCESS_KEY_ID = os.getenv('AWS_ACCESS_KEY_ID')AWS_SECRET_ACCESS_KEY = os.getenv('AWS_SECRET_ACCESS_KEY')AWS_STORAGE_BUCKET_NAME = os.getenv('AWS_STORAGE_BUCKET_NAME')AWS_S3_REGION_NAME = os.getenv('AWS_S3_REGION_NAME', 'us-east-1')AWS_S3_CUSTOM_DOMAIN = f'{AWS_STORAGE_BUCKET_NAME}.s3.amazonaws.com'AWS_S3_OBJECT_PARAMETERS = { 'CacheControl': 'max-age=86400',}AWS_DEFAULT_ACL = 'public-read'AWS_QUERYSTRING_AUTH = False
# Cache configurationCACHES = { 'default': { 'BACKEND': 'django_redis.cache.RedisCache', 'LOCATION': os.getenv('REDIS_URL', 'redis://localhost:6379/1'), 'OPTIONS': { 'CLIENT_CLASS': 'django_redis.client.DefaultClient', 'CONNECTION_POOL_KWARGS': { 'max_connections': 100, 'retry_on_timeout': True } }, 'KEY_PREFIX': 'django_prod' }}
# Celery ConfigurationCELERY_BROKER_URL = os.getenv('CELERY_BROKER_URL', 'redis://localhost:6379/0')CELERY_RESULT_BACKEND = os.getenv('CELERY_RESULT_BACKEND', 'redis://localhost:6379/0')
# Email configuration (Amazon SES)EMAIL_BACKEND = 'django