AWS & Modern Cloud Architecture · Chapter 5 · ~20 min · Intermediate
EC2, Auto Scaling & Application Load Balancers
Design resilient, fault-tolerant, horizontally scalable compute architectures in AWS. Combine Layer 7 Application Load Balancers (ALBs) with EC2 Auto Scaling Groups (ASGs) to absorb massive traffic spikes and self-heal automatically.
- Configure Application Load Balancers with path-based routing and target groups
- Implement deep health checks and connection draining (deregistration delay)
- Define EC2 Launch Templates with user data boot scripts and AMI snapshots
- Configure dynamic Auto Scaling policies based on CloudWatch CPU and request count alarms
EC2, Auto Scaling & Application Load Balancers: learn it visually
Deploy highly available compute clusters: EC2 Launch Templates, target groups with deep /healthz checks, Application Load Balancers, connection draining, and cross-AZ Auto Scaling Groups.
1The Core Role: Application Load Balancers (ALB)
In a cloud architecture, you never point DNS records directly to the IP addresses of individual EC2 servers. Instances can be terminated, replaced, or scaled up dynamically at any moment.
Instead, you place an Application Load Balancer (ALB) in front of your compute fleet. The ALB acts as a single, highly available public entry point across multiple Availability Zones:
Layer 7 Intelligence
Inspects HTTP headers, cookies, and paths. Routes /api/* to one target group and /images/* to another.
TLS/SSL Termination
Terminates HTTPS encryption at the load balancer using free ACM certificates, relieving EC2 instances of cryptography overhead.
Health Check Probing
Continuously probes backend instances. If an instance crashes or fails /healthz, traffic is rerouted instantly.
Wrong idea: Resizing a single EC2 server when traffic spikes (Vertical Scaling)
Stopping your web server on Black Friday to upgrade from a t3.medium to an m5.4xlarge, incurring 15 minutes of downtime and creating a single point of failure.
Real system: Horizontal auto scaling across isolated Availability Zones
Deploying a fleet of identical stateless instances behind an Application Load Balancer. EC2 Auto Scaling adds and removes virtual machines dynamically based on live load.
What is the primary architectural difference between an AWS Application Load Balancer (ALB) and a Network Load Balancer (NLB)?
2EC2 Launch Templates: The Scaling Blueprint
To scale automatically, an Auto Scaling Group must know how to launch an identical replacement server without human intervention. In AWS, this blueprint is defined in an EC2 Launch Template:
{
"LaunchTemplateData": {
"ImageId": "ami-0c7217cdde317cfec",
"InstanceType": "t3.micro",
"IamInstanceProfile": { "Arn": "arn:aws:iam::123456789012:instance-profile/WebTierRole" },
"SecurityGroupIds": ["sg-01234567webservers"],
"UserData": "IyEvYmluL2Jhc2gKeXVtIHVwZGF0ZSAteQp5dW0gaW5zdGFsbCAteSBodHRwZApzeXN0ZW1jdGwgc3RhcnQgaHR0cGQK"
}
}User Data: A base64-encoded shell script that runs as root the first time the machine boots up. It installs runtime packages (Node.js, Docker, Python), clones configuration files, and starts the web process.
3Dynamic Auto Scaling Policies & Connection Draining
Auto Scaling Groups can scale using three distinct mechanisms:
- Target Tracking Scaling: You set a metric target (e.g. "Maintain average CPU utilization at 60%" or "Maintain 1,000 requests per target"). AWS automatically adds or removes instances to match the target curve.
- Step Scaling: Adds distinct numbers of servers based on alarm thresholds (e.g. If CPU > 70%, add 1 server; if CPU > 90%, add 3 servers).
- Scheduled Scaling: Scales out proactively ahead of predictable traffic spikes (e.g. 8:00 AM every Monday).
Connection Draining (Deregistration Delay):
When an instance is marked for termination during scale-in or health check failure, the ALB places it into the draining state. The ALB immediately stops sending new requests to that instance, while giving existing active HTTP transactions up to 300 seconds to finish.
Predict first
What happens if you set ALB Deregistration Delay Timeout to 0 seconds on a high-traffic production API?
What is the primary function of ALB Connection Draining (Deregistration Delay)?
4Hands-On: Simulate Traffic Spikes & Multi-AZ Auto Scaling
Use the slider below to send simulated traffic spikes to an Application Load Balancer. Watch the CloudWatch CPU metric alarm trigger dynamic scale-out across AZ-1a and AZ-1b, or inject failures to test automatic self-healing:
Multi-AZ Auto Scaling & ALB Traffic Distributer
Test self-healing when a server dies
5Deep Health Checks vs. Shallow Ping Checks
A shallow health check only confirms that the EC2 operating system is listening on TCP port 80. However, modern applications can experience hidden failures:
- Database connection pool exhaustion.
- Out of Memory (OOM) memory leaks in the V8 garbage collector.
- Deadlocked background worker threads.
The production pattern is to create a dedicated /healthz endpoint that performs lightweight dependency assertions:
{
"HealthCheckEndpoint": "/healthz",
"ResponseStatus": 200,
"ResponseBody": {
"status": "healthy",
"dbConnected": true,
"redisConnected": true,
"uptimeSeconds": 84210
}
}Why should production EC2 Auto Scaling Groups use Launch Templates rather than legacy Launch Configurations?
6Bug Hunt: Fix the ALB Target Group Health Check & Draining
A newly deployed target group is dropping customer connections during deployments, and healthy instances are intermittently marked unhealthy. Find and fix the three health check endpoint, expected status code, and draining timeout flaws below:
🎯 Find and fix the bugs in the code above, then press Run & verify to test your solution.
Need a hint?
- Use dedicated health check endpoint: change
/admin/dashboardto/healthz. - Healthy instances return HTTP success: change expected status code from
404to200. - Never use 0-second draining: set
DeregistrationDelayTimeoutSecondsto300.
7Chapter Knowledge Challenge
Validate your knowledge of load balancing, target tracking, health checks, and auto-scaling fleets:
You want your web server fleet to dynamically adjust instance count to maintain an average CPU utilization of 60%. Which Auto Scaling policy should you choose?
8Recap
- Application Load Balancers (ALB) provide Layer 7 intelligent routing, SSL termination, and health check isolation across multi-AZ targets.
- EC2 Launch Templates define the immutable instance configuration: AMI, compute size, security groups, and automated User Data boot scripts.
- Auto Scaling Groups (ASG) dynamically provision and terminate virtual machines across Availability Zones to match traffic demand.
- Connection Draining (Deregistration Delay) gives active in-flight user requests a grace period (e.g. 300s) to finish before instance shutdown.
- Deep Health Checks: Probe dedicated
/healthzendpoints to detect application-level deadlocks beyond simple TCP port responses.
Ready for Chapter 6 (Object Storage with S3 & CloudFront CDN)? Mark as complete to proceed: