Auto-Scaling Web Apps with AWS Elastic Beanstalk

Submitted by leopathu on Sun, 06/23/2019 - 16:11

As an AWS Solution Architect, I'll walk you through building a resilient, auto-scaling web application to handle unpredictable traffic spikes (e.g., breaking news, product launches).


Problem Scenario

A news portal experiences traffic surges (10x normal load) during elections, causing 5-minute outages that cost $20k/hour in lost revenue. Their legacy servers cannot scale dynamically.


Solution Overview

AWS Services Used:

  • Elastic Beanstalk: Automated deployment & scaling
  • RDS: Managed PostgreSQL database
  • CloudWatch: Monitoring & alerts
  • S3: Static asset storage
  • CloudFront: Global content caching

Architecture:

Users → CloudFront (CDN) → Elastic Beanstalk (Auto-Scaling EC2) → RDS (Multi-AZ)  
Static Assets (S3) ↗

Step-by-Step Implementation

Step 1: Package Your Application

Create a Dockerfile or ZIP deployment package:

FROM nginx:alpine  
COPY /news-app /usr/share/nginx/html  
EXPOSE 80

Step 2: Create Elastic Beanstalk Environment

AWS Console → Elastic Beanstalk → "Create Application":

  • Name: news-portal-prod
  • Platform: Docker (or Node.js/Python)
  • Upload code: Upload your ZIP/Dockerfile

Configure RDS Database:

  • Under "Database" settings:
    • Engine: PostgreSQL
    • Multi-AZ: Enabled (for failover)

Step 3: Configure Auto-Scaling

In ConfigurationCapacity:

  • Environment type: Load balanced, auto-scaling
  • Instances:
    • Min: 4
    • Max: 20
  • Scaling triggers:
    • Metric: CPUUtilization
    • Upper threshold: 60%
    • Lower threshold: 30%
    • Scale-out increment: 2 instances
    • Scale-in increment: 1 instance

Advanced Scaling (via .ebextensions/scaling.config):

aws:autoscaling:trigger
MetricName: NetworkOut
Statistic: Sum
Unit: Bytes
LowerThreshold: 1000000
UpperThreshold: 5000000
ScaleOutIncrement: 2
ScaleInIncrement: 1

Step 4: Optimize Static Assets

  1. Upload CSS/JS/images to S3:
aws s3 sync ./assets s3://news-portal-assets
  1. Integrate with CloudFront:
    • Create distribution pointing to S3 bucket + Elastic Beanstalk origin.

Step 5: Set Up Alerts

  1. CloudWatch Alarms:
    • Trigger SMS/email when:
      • CPU > 70% for 5 minutes
      • Healthy hosts < 4
aws cloudwatch put-metric-alarm \
--alarm-name "High-CPU-NewsPortal" \
--metric-name CPUUtilization \
--threshold 70 \
--comparison-operator GreaterThanThreshold \
--evaluation-periods 2

Key Benefits Achieved

Cost Efficiency:

  • Scale from 4 to 20 instances within 5 minutes during traffic spikes.
  • Pay only for resources used (vs. over-provisioned legacy servers).

Zero Downtime:

  • Rolling deployments: Updates deploy to instances incrementally.
  • Multi-AZ RDS ensures database failover in <60s.

Performance:

  • CloudFront caches static content at 225+ global edge locations.
  • Auto-scaling maintains <1s response time at 100k RPM.

Operational Simplicity:

  • Beanstalk handles OS patching, load balancer config, and SSL/TLS.
  • One-command rollbacks: eb deploy --version previous.

Real-World Results

After implementation:
 

MetricBeforeAfter
Outage Duration 5 min/hour0
Cost/Month%18k$6.2k
Max RPM Handled50k450k

Architect’s Checklist

  1. Security:
    • Enable IAM instance roles (not hardcoded keys).
    • Encrypt RDS at rest using AWS KMS.
  2. Cost Controls:
    • Use Reserved Instances for baseline capacity.
    • Set Scale-In Protection to avoid thrashing.
  3. Disaster Recovery:
    • Daily RDS snapshots + cross-region replication.

"Elastic Beanstalk transforms scaling from a crisis into a non-event. It’s the fastest way to achieve production resilience without DevOps overhead."