Skip to content

Lab 4 · EC2 Auto Scaling + ELB

Resilient · Est. time: 40–50 min · Cost: ⚠️ the Application Load Balancer is NOT Free Tier (bills hourly + per LCU); t2.micro/ t3.micro instances are Free-Tier eligible. Tear everything down at the end.

Goal. Build the exam's canonical resilient web tier — ALB → Auto Scaling group across 2 AZs — and watch it self-heal when you kill an instance (Topic 4).

Prereq

Use the lab-vpc from Lab 1, or the default VPC. You need two subnets in different AZs.


Step 1 — Launch template

  1. EC2 → Launch templates → Create launch template. Name lab-lt.
  2. AMI: Amazon Linux 2023. Instance type: t2.micro/t3.micro (Free Tier).
  3. User data (installs a web page that shows the instance ID):
    #!/bin/bash
    dnf install -y httpd
    systemctl enable --now httpd
    echo "Hello from $(hostname -f)" > /var/www/html/index.html
    
  4. Security group: allow HTTP 80 from anywhere (lab only). Create the template.
# (create a security group allowing port 80 first; then:)
aws ec2 create-launch-template --launch-template-name lab-lt \
  --launch-template-data '{"ImageId":"ami-xxxxxxxx","InstanceType":"t3.micro","UserData":"<base64-user-data>"}'

Step 2 — Application Load Balancer + target group

  1. EC2 → Load Balancers → Create → Application Load Balancer. Name lab-alb, internet-facing.
  2. Select two subnets in different AZs.
  3. Security group: allow HTTP 80.
  4. Create a target group lab-tg (type Instances, protocol HTTP:80, health check path /).
  5. Finish creating the ALB pointing at lab-tg.

Billing starts now

The ALB bills from creation. Note the time; delete it in teardown.


Step 3 — Auto Scaling group across 2 AZs

  1. EC2 → Auto Scaling groups → Create. Name lab-asg, launch template lab-lt.
  2. Select the two AZs/subnets.
  3. Attach to an existing load balancer → target group lab-tg.
  4. Health check type: ELB (not just EC2 — the Topic 4 gotcha). Health check grace ~90s.
  5. Desired 2, Min 2, Max 4. Create.
aws autoscaling create-auto-scaling-group --auto-scaling-group-name lab-asg \
  --launch-template LaunchTemplateName=lab-lt --min-size 2 --max-size 4 --desired-capacity 2 \
  --vpc-zone-identifier "subnet-aaa,subnet-bbb" \
  --target-group-arns <lab-tg-arn> --health-check-type ELB --health-check-grace-period 90

Step 4 — Test resilience and scaling

  1. Browse to the ALB DNS name (EC2 → Load Balancers → lab-alb → DNS name). Refresh — you'll see responses from different instance IDs across AZs (load balancing).
  2. Kill an instance: EC2 → Instances → terminate one. Watch the ASG detect the failure (ELB health check) and launch a replacement to return to desired = 2. That's self-healing.
  3. (Optional) Add a target-tracking policy (keep average CPU at 50%) to see scaling config — generating real CPU load is optional.

Teardown

  1. Delete the Auto Scaling group lab-asg (this terminates its instances).
  2. Delete the ALB lab-albstops the hourly bill.
  3. Delete the target group lab-tg and the launch template lab-lt.
  4. Delete the lab security groups if unused.
aws autoscaling delete-auto-scaling-group --auto-scaling-group-name lab-asg --force-delete
aws elbv2 delete-load-balancer --load-balancer-arn <lab-alb-arn>
aws elbv2 delete-target-group --target-group-arn <lab-tg-arn>
aws ec2 delete-launch-template --launch-template-name lab-lt

✅ What you should understand now

  • ALB + ASG across ≥ 2 AZs is the resilient web-tier default — survives an AZ failure and self-heals failed instances.
  • Setting the ASG health check type to ELB (not the default EC2) is what makes it replace instances the load balancer considers unhealthy.
  • The ALB spreads traffic across healthy targets in multiple AZs (you saw different instance IDs).
  • An ALB is not Free Tier — always delete it after labs.
  • Target tracking is the go-to scaling policy; scheduled/predictive are for known/forecastable patterns.

Related: Learn — HA & Fault Tolerance