CS Fundamentals Trainer · Guides
AWS for a React and Django application: the services, how they fit together and the usual mistakes
A map of the AWS services a typical React frontend and Django API use and why: S3 and CloudFront for the static app, a load balancer with ECS Fargate (or Elastic Beanstalk or EC2) for Django, RDS PostgreSQL, ElastiCache, SQS, S3 uploads with presigned URLs, VPC networking and security groups, IAM roles, secrets, CloudWatch, a deployment pipeline and the cost and security traps that catch teams out.
7 min read
AWS has hundreds of services, but a web product built with React and Django uses a small, predictable set. This guide walks through that set from the user's browser inward, says what each piece is for, and lists the decisions and mistakes that matter in practice. Exact limits and prices change, so check the current documentation before relying on a number.
The shape of the system
Browser --> Route 53 (DNS) --> CloudFront (CDN, TLS)
|-- / --> S3 bucket with the built React app
|-- /api/* --> Application Load Balancer --> Django containers (ECS Fargate)
|--> RDS PostgreSQL
|--> ElastiCache Redis (cache, sessions)
|--> SQS or Redis --> Celery workers
|--> S3 (user uploads)
The frontend: S3 and CloudFront
A React single-page app is a set of static files after npm run build. Store them in an S3 bucket that is not public, and serve them through CloudFront, AWS's content delivery network, which caches them at edge locations near users and terminates HTTPS with a free certificate from ACM (AWS Certificate Manager; certificates for CloudFront must be in us-east-1). An origin access control lets only CloudFront read the bucket.
Points that matter:
- Cache headers: built files with a content hash in their names (
app.3f9c1a.js) can be cached for a year;index.htmlmust not be cached for long, or users keep loading the old app after a deploy. - Client-side routing: a request for
/rooms/42has no file in the bucket. Configure CloudFront to returnindex.htmlfor those paths (a custom error response or a small function), so the React router can handle them. - One domain for app and API (CloudFront routes
/api/*to the load balancer) avoids CORS and lets Django's session cookie and CSRF protection work as on a single site. - Deploys upload the new files, then invalidate
index.htmlin CloudFront.
The backend: where Django runs
Options, from most managed to least:
| Option | What you manage | Good for |
|---|---|---|
| App Runner or Elastic Beanstalk | the application and configuration | small teams, quick start |
| ECS on Fargate | container images and task definitions; no servers | most production APIs |
| EC2 instances | the operating system, patching, scaling | special needs, or very cost-sensitive steady load |
| EKS (Kubernetes) | a cluster | organisations already standardised on Kubernetes |
| Lambda | functions | event handlers, cron jobs; a full Django app fits awkwardly |
A common choice is ECS Fargate: build a Docker image with Gunicorn (or Uvicorn for ASGI), push it to ECR (the container registry), and run it as an ECS service with several tasks behind an Application Load Balancer (ALB). The ALB checks a health endpoint, spreads requests, and drains tasks during deploys. Auto scaling adds tasks on CPU or request count.
Run database migrations as a separate one-off task before the new version takes traffic, not at container start, so that ten tasks do not race to migrate. Keep migrations compatible with the version still running (add columns first, remove later).
Celery workers run as another ECS service from the same image with a different command. A scheduled job (celery beat, or EventBridge Scheduler starting a task) handles periodic work.
Data: RDS, ElastiCache and S3
- RDS for PostgreSQL (or Aurora PostgreSQL) is managed Postgres: automated backups with point-in-time recovery, patching, and Multi-AZ, a standby in another availability zone that takes over on failure. Read replicas add read capacity; Django can route reads to them with a database router, but replicas lag, so read-after-write paths must use the primary. Watch the connection count: each Gunicorn worker holds connections, so use
CONN_MAX_AGEcarefully or RDS Proxy/PgBouncer. - ElastiCache (Redis or Valkey) holds the Django cache, sessions if you choose, rate-limit counters and possibly the Celery broker.
- S3 stores user uploads (photos, documents) through django-storages. Let the browser upload directly to S3 with a presigned URL the API creates, instead of streaming files through Django; then store the object key in the database. Keep the bucket private and serve files through CloudFront or short-lived presigned download URLs.
- SQS is a fully managed queue. Celery supports it as a broker; SQS has at-least-once delivery and a visibility timeout, so tasks must be idempotent and the timeout longer than the slowest task.
Networking: VPC, subnets and security groups
Everything runs in a VPC (your private network in AWS), spread across at least two availability zones:
- Public subnets hold only the load balancer (and NAT gateways).
- Private subnets hold the Django tasks, workers, RDS and ElastiCache. They have no public IP addresses.
- Security groups are stateful firewalls attached to resources. Allow traffic by source security group, not by IP ranges: the ALB group may reach the app on its port; the app group may reach RDS on 5432 and Redis on 6379; nothing else.
Private resources reach the internet (to call an email API, say) through a NAT gateway, which is charged per hour and per gigabyte. VPC endpoints for S3, ECR and other AWS services keep that traffic inside AWS and often cut the NAT bill noticeably.
Identity and secrets
- IAM roles, not keys. The ECS task gets a task role with exactly the permissions it needs (read and write one S3 prefix, send to one queue). No access keys in code, images or environment files. Humans sign in through IAM Identity Center with MFA; the root account is locked away.
- Least privilege: start narrow and add permissions when something fails, rather than starting with
*. - Secrets (the database password, Django
SECRET_KEY, third-party API keys) live in Secrets Manager or SSM Parameter Store and are injected into the task at start. Rotate them. - CI deploys with short-lived credentials via OIDC (for example GitHub Actions assuming a role), not long-lived keys stored in the CI system.
Observability
- CloudWatch Logs collects container output; log JSON with a request id so you can search it with Logs Insights.
- CloudWatch metrics and alarms: ALB 5xx rate and latency (p95/p99), ECS CPU and memory, RDS CPU, free storage, connections and replica lag, queue age for SQS. Alarms notify through SNS to email, chat or a paging tool.
- Tracing with X-Ray or OpenTelemetry shows which part of a slow request took the time.
- Error tracking (Sentry or similar) for both the React app and Django.
Deployment pipeline and infrastructure as code
A typical pipeline: run tests and linters, build the React app and upload it to S3, build the Django image and push it to ECR, run migrations as a one-off task, update the ECS service (a rolling or blue/green deploy that the ALB health checks protect), then invalidate CloudFront.
Describe the infrastructure as code with Terraform, AWS CDK or CloudFormation, so environments are reproducible, changes are reviewed, and a staging environment really matches production.
Security checklist
- No public S3 buckets unless intended; Block Public Access on at account level.
- RDS and Redis in private subnets, reachable only from the app's security group; encryption at rest on.
- HTTPS everywhere (ACM certificates), HTTP redirected; HSTS from Django or CloudFront.
- AWS WAF on CloudFront or the ALB for rate limiting and common attack patterns.
- CloudTrail on, and alerts on root sign-in and IAM changes.
- Django
ALLOWED_HOSTS,SECURE_PROXY_SSL_HEADER(the ALB terminates TLS) and secure cookie settings set for the real domain.
Cost traps
- NAT gateway data processing for traffic that could use VPC endpoints.
- Oversized RDS instances, or Multi-AZ and replicas in development environments that do not need them.
- Forgotten resources: idle load balancers, unattached volumes, old snapshots, test environments left running. Tag everything with an owner and environment, and set AWS Budgets alerts.
- Data transfer out of AWS and between availability zones.
- CloudWatch Logs kept forever: set retention.
Sources
- AWS Well-Architected Framework.
- Amazon ECS on AWS Fargate and Application Load Balancer.
- Amazon RDS for PostgreSQL.
- Amazon S3 presigned URLs and CloudFront origin access control.
- IAM best practices.
- django-storages: Amazon S3.
Practise this topic: DevOps & Cloud DevOps & Cloud › Cloud