Back to Curriculum

Auto Scaling and Load Balancing

📚 Lesson 7 of 12 ⏱️ 90 min

Auto Scaling and Load Balancing

90 min

Auto Scaling automatically adjusts the number of EC2 instances in your application based on demand, ensuring you have the right amount of compute capacity at all times. Auto Scaling groups define collections of EC2 instances that should be scaled together, with configurable minimum, maximum, and desired capacity. This automatic scaling enables your application to handle traffic spikes while minimizing costs during low-traffic periods.

Auto Scaling uses scaling policies to determine when and how to scale. Target tracking policies maintain a specific metric (like CPU utilization) at a target value. Step scaling policies adjust capacity based on the magnitude of alarm breaches. Simple scaling policies add or remove instances based on a single alarm. Scheduled scaling allows you to scale based on predictable load patterns, such as business hours or known traffic spikes.

Application Load Balancer (ALB) distributes incoming application traffic across multiple targets (EC2 instances, containers, IP addresses) in multiple Availability Zones. ALB operates at the application layer (Layer 7), enabling content-based routing, host-based routing, and path-based routing. ALB provides advanced request routing, SSL/TLS termination, and health checking, making it ideal for modern web applications.

ALB integrates seamlessly with Auto Scaling groups. When Auto Scaling adds or removes instances, ALB automatically registers or deregisters them as targets. Health checks ensure that traffic is only routed to healthy instances. If an instance fails health checks, ALB stops sending traffic to it, and Auto Scaling can replace it. This integration provides automatic recovery from instance failures.

Load balancers provide high availability by distributing traffic across multiple Availability Zones and instances. If one Availability Zone or instance fails, the load balancer routes traffic to healthy instances in other zones. This redundancy ensures your application remains available even during infrastructure failures. ALB also provides connection draining, allowing in-flight requests to complete before removing instances from service.

Together, Auto Scaling and Load Balancing provide a robust foundation for highly available, scalable applications. Auto Scaling ensures you have sufficient capacity, while Load Balancing distributes traffic efficiently and provides fault tolerance. Understanding how these services work together enables you to design applications that automatically adapt to changing demand while maintaining high availability.

Key Concepts

  • Auto Scaling automatically adjusts EC2 instance count based on demand.
  • Auto Scaling groups define collections of instances that scale together.
  • Application Load Balancer distributes traffic across multiple targets.
  • ALB operates at Layer 7, enabling advanced routing capabilities.
  • Auto Scaling and Load Balancing together provide high availability and scalability.

Learning Objectives

Master

  • Configuring Auto Scaling groups with scaling policies
  • Setting up Application Load Balancers for traffic distribution
  • Integrating Auto Scaling with Load Balancing
  • Implementing health checks and automatic recovery

Develop

  • Designing scalable, highly available architectures
  • Understanding automatic capacity management
  • Building fault-tolerant application infrastructure

Tips

  • Use target tracking policies for simple, effective auto scaling.
  • Configure health checks appropriately to avoid unnecessary instance replacements.
  • Distribute instances across multiple Availability Zones for high availability.
  • Use connection draining to gracefully remove instances from service.

Common Pitfalls

  • Setting scaling policies too aggressively, causing unnecessary scaling actions.
  • Not configuring health checks properly, routing traffic to unhealthy instances.
  • Not distributing across Availability Zones, risking availability during zone failures.
  • Not monitoring scaling activities, missing optimization opportunities.

Summary

  • Auto Scaling automatically adjusts capacity based on demand.
  • Application Load Balancer distributes traffic and provides high availability.
  • Together they provide scalable, fault-tolerant application infrastructure.
  • Proper configuration ensures applications adapt to changing workloads.

Exercise

Set up an Auto Scaling group with an Application Load Balancer.

# Create a launch template
cat > launch-template.json << 'EOF'
{
    "LaunchTemplateName": "my-launch-template",
    "LaunchTemplateData": {
        "ImageId": "ami-0c02fb55956c7d316",
        "InstanceType": "t2.micro",
        "SecurityGroupIds": ["sg-12345678"],
        "KeyName": "my-key-pair",
        "UserData": "IyEvYmluL2Jhc2gKeXVtIHVwZGF0ZSAteQp5dW0gaW5zdGFsbCAteSBodHRwZApzeXN0ZW1jdGwgc3RhcnQgaHR0cGQKc3lzdGVtY3RsIGVuYWJsZSBodHRwZApjZCAvdmFyL3d3dy9odG1sCmVjaG8gIjxoMT5IZWxsbyBmcm9tIEF1dG8gU2NhbGluZyBHcm91cCE8L2gxPiIgPiBpbmRleC5odG1s"
    }
}
EOF

aws ec2 create-launch-template --cli-input-json file://launch-template.json

# Create Application Load Balancer
aws elbv2 create-load-balancer \
    --name my-alb \
    --subnets subnet-12345678 subnet-87654321 \
    --security-groups sg-12345678

# Create target group
aws elbv2 create-target-group \
    --name my-target-group \
    --protocol HTTP \
    --port 80 \
    --vpc-id vpc-12345678 \
    --health-check-path / \
    --health-check-interval-seconds 30

# Create Auto Scaling group
aws autoscaling create-auto-scaling-group \
    --auto-scaling-group-name my-asg \
    --launch-template LaunchTemplateName=my-launch-template,Version=$Latest \
    --min-size 2 \
    --max-size 5 \
    --desired-capacity 2 \
    --vpc-zone-identifier "subnet-12345678,subnet-87654321" \
    --target-group-arns arn:aws:elasticloadbalancing:us-east-1:123456789012:targetgroup/my-target-group/1234567890123456

Exercise Tips

  • Configure scaling policies based on CloudWatch metrics for automatic adjustment.
  • Use launch templates for consistent instance configuration in Auto Scaling groups.
  • Set up lifecycle hooks to perform custom actions during instance launch/termination.
  • Monitor Auto Scaling activities in CloudWatch to understand scaling behavior.

Code Editor

Output