Sub topic 3
Connecting an Auto Scaling Group to a Target Group
Assume you know EC2 instances, launch templates, VPC subnets, security groups, and basic AWS CLI commands. An Auto Scaling group (ASG) maintains a chosen number of instances. A target group is the load balancer’s list of destinations. Connecting them lets the ASG register new instances and deregister removed ones automatically.
Connect the group
Attach the target group when creating the ASG. Use ELB health checks so an instance that fails the application health check can be replaced.
aws autoscaling create-auto-scaling-group \
--auto-scaling-group-name web-asg \
--launch-template LaunchTemplateName=web-lt,Version='$Latest' \
--min-size 2 \
--max-size 6 \
--desired-capacity 2 \
--vpc-zone-identifier "subnet-aaaa1111,subnet-bbbb2222" \
--target-group-arns "$TG_ARN" \
--health-check-type ELB \
--health-check-grace-period 300The health-check grace period gives a new instance time to boot before health checks affect replacement decisions.
Metrics and scaling policies
CloudWatch collects metrics such as request count per target, CPU utilization, and latency. Alarms evaluate those metrics, while scaling policies change the ASG’s desired capacity.
- Target tracking maintains a metric near a target, such as 1,000 requests per target. It is usually the simplest starting point.
- Step scaling adds different amounts after thresholds are crossed. For example, a large CPU spike can add several instances.
- Scheduled scaling changes capacity at known times, such as before a daily traffic peak.
- Predictive scaling uses historical patterns to forecast capacity needs. Validate its forecasts before relying on it.
Instance warmup prevents a newly launched instance from influencing scaling decisions before it can serve traffic. Cooldown behavior helps avoid repeated reactions to one event, although target tracking manages much of this automatically.
Safe changes and other services
Lifecycle hooks pause launch or termination so automation can install software, drain connections, or export logs. Instance refresh replaces instances gradually after a launch-template change.
Application Auto Scaling applies similar policies to services such as ECS services, DynamoDB tables, and Aurora read replicas. A Gateway Load Balancer is different from an ALB: it distributes traffic to network appliances such as firewalls and intrusion detection systems.
Resilience, cost, and troubleshooting
Use at least two Availability Zones, keep minimum capacity above one, and select instance sizes that balance cost and headroom. Set deregistration delay long enough for requests to finish, but not so long that scale-in stalls.
For unhealthy targets, check the health path, response code, port, security groups, subnet routes, and application startup logs. If scaling does not trigger, verify the metric dimensions, alarm state, policy attachment, warmup period, and ASG limits.
Exercise
Why can an instance be running but still receive no traffic?
Show answer
The target group may mark it unhealthy because its health-check path, port, security group, route, or response code is incorrect.