How does an Amazon EC2 Auto Scaling group maintain and scale instances?
An Amazon EC2 Auto Scaling group (ASG) groups EC2 instances and either maintains a target capacity or changes instance count with demand. You define new-instance settings in a launch template and operate the group with minimum, maximum, and desired capacity, health checks, scaling policies, and Availability Zone placement. An ASG can maintain the instance fleet, but it does not automatically replicate application data, make scale-in safe, or guarantee zero-downtime deployments. ([AWS: Auto Scaling groups](https://docs.aws.amazon.com/autoscaling/ec2/userguide/auto-scaling-groups.html), [set capacity limits](https://docs.aws.amazon.com/autoscaling/ec2/userguide/asg-capacity-limits.html))
How does an Amazon EC2 Auto Scaling group maintain and scale instances?
An Amazon EC2 Auto Scaling group (ASG) groups EC2 instances and either maintains a target capacity or changes instance count with demand. You define new-instance settings in a launch template and operate the group with minimum, maximum, and desired capacity, health checks, scaling policies, and Availability Zone placement. An ASG can maintain the instance fleet, but it does not automatically replicate application data, make scale-in safe, or guarantee zero-downtime deployments. (AWS: Auto Scaling groups, set capacity limits)
What does an ASG use to manage instances?
The core is desired capacity, the number of instances the group tries to keep. Minimum capacity and maximum capacity bound how far it may scale. For example, with minimum 2, desired 4, and maximum 8, the group can run from 2 to 8 according to its policies and aims to keep 4 when no scaling condition changes the target. If an instance terminates or fails a health check, the group launches a replacement to restore desired capacity.
A launch template contains startup settings such as the AMI, instance type, network and security configuration, storage, and user data. Replacement instances use the group's current template settings. Do not assume that changing the template version by itself replaces instances that are already running. (AWS: Auto Scaling groups)
How do scaling policies change desired capacity?
Desired capacity can change manually, through scheduled actions, or through dynamic scaling policies. A dynamic policy adjusts the desired value in response to metrics such as CPU or a target utilization, and the group then launches or terminates instances to match. Minimum and maximum limits also apply to policies: a low maximum can prevent scale-out during a surge, while an unnecessarily high minimum keeps costs up when demand is low. (AWS: capacity limits)
Design scale-in around work in progress, connection draining, and cache warm-up. Scaling out adds capacity; scaling in terminates existing instances, so the application must hand off work or shut down safely. Spot interruptions and other unexpected terminations can also occur; the group attempts to replace instances to maintain desired capacity.
What do health checks and Availability Zones provide?
An ASG uses EC2 health checks and can also use checks from Elastic Load Balancing, VPC Lattice, or custom sources. When it marks an instance unhealthy, it can replace it. A load balancer check generally asks whether an instance can receive traffic; an EC2 check detects platform-level problems. Application-level checks can detect failures sooner, but an overly sensitive signal can replace instances during normal delays. (AWS: Auto Scaling health checks)
When multiple Availability Zones are configured, the group distributes capacity across them and tries to maintain balance during scaling. Whether it tries another zone when a launch fails, or insists on balancing, depends on the selected distribution strategy. Multi-zone placement can improve compute-tier availability, but it does not replace database replication or regional disaster recovery. (AWS: Availability Zone distribution)
How do you roll out a launch-template change?
Changing to a new launch-template version affects subsequently launched instances; existing instances may remain unchanged. To update the running fleet with a new AMI or user data, use Instance Refresh to replace instances in batches. Configure replacement percentages, warm-up time, checkpoints, and rollback behavior, and provide enough spare capacity and health monitoring. A small group can lose service capacity during refresh if the healthy-instance thresholds are set poorly. (AWS: use Instance Refresh, how Instance Refresh works)
An accepted request does not mean the update has completed. Verify that replacement instances launched, passed health checks, and serve the application normally, and that the refresh status is complete. If a problem appears, be ready to stop later batches or use a supported rollback to the prior template configuration.
When does an ASG fit, and what needs separate design?
An ASG is useful for web tiers or workers running multiple EC2 instances when capacity must follow demand, unhealthy instances should be replaced, and deployments should use a repeatable template. If a workload must remain on one instance or its state exists only on local storage, first assess whether safe replacement is possible. Even with an ASG, design load balancing, externalized state, data backups, deployment verification, least-privilege IAM, quotas, and cost alerts separately.
Official documentation
[Amazon EC2 Auto Scaling groups](https://docs.aws.amazon.com/autoscaling/ec2/userguide/auto-scaling-groups.html)
[Set desired, minimum, and maximum capacity](https://docs.aws.amazon.com/autoscaling/ec2/userguide/asg-capacity-limits.html)
[Auto Scaling health checks](https://docs.aws.amazon.com/autoscaling/ec2/userguide/ec2-auto-scaling-health-checks.html)
[Availability Zone distribution](https://docs.aws.amazon.com/autoscaling/ec2/userguide/ec2-auto-scaling-availability-zone-balanced.html)
[Use Instance Refresh](https://docs.aws.amazon.com/autoscaling/ec2/userguide/asg-instance-refresh.html)
[How Instance Refresh works](https://docs.aws.amazon.com/autoscaling/ec2/userguide/instance-refresh-overview.html)