Auto Scaling Groups (ASG) in AWS
An Auto Scaling Group (ASG) automatically manages a fleet of Amazon EC2 instances to maintain availability and match capacity to demand. You define how many instances you want, where to place them, and how to scale. ASG then launches, terminates, replaces, and scales instances according to health checks and policies.
Core concepts
- Group size: You set minimum, maximum, and desired capacity. ASG keeps the desired number running and never goes below min or above max.
- Launch configuration: You attach either a Launch Template (recommended) or an older Launch Configuration to define AMI, instance type, security groups, user data, IAM role, EBS settings, and tags.
- Subnets/AZs: You choose one or more subnets across Availability Zones. ASG spreads instances to improve resilience.
- Health checks: ASG considers EC2 status checks and, if attached to a load balancer target group, ELB health checks. Unhealthy instances are terminated and replaced automatically.
- Scaling policies: Rules that change desired capacity based on metrics or schedules.
Scaling options
- Target tracking: Easiest. You specify a target metric (for example, average CPU at 50% or ALB request count per target). ASG adjusts capacity automatically to maintain the target.
- Step scaling: Add or remove a specific number/percentage of instances when a CloudWatch alarm breaches thresholds with steps (for example, +2 instances if CPU > 70% for 5 minutes).
- Scheduled scaling: Scale for known patterns (for example, increase capacity every weekday 9:00–18:00).
- Predictive scaling: Uses machine learning to forecast demand and provision capacity ahead of time. Works best with regular, daily/weekly patterns and with Application Load Balancers metrics.
Integration with other services
- Elastic Load Balancing (ALB/NLB): Register ASG instances with target groups for traffic distribution and use ELB health checks.
- EC2 Spot: Use Mixed Instances policy to combine On-Demand and Spot across instance types for cost optimization and resilience.
- Amazon CloudWatch: Drive scaling with metrics like CPUUtilization, RequestCountPerTarget, TargetResponseTime, custom metrics.
- AWS Systems Manager: Patch and manage instances at scale; use Parameter Store/Secrets Manager from user data.
- Lifecycle hooks: Run custom actions during instance launch or termination (for example, configuration, deregister from service registry, drain connections).
Best practices
- Use Launch Templates with versioning; reference SSM parameters for “latest approved AMI”.
- Distribute across at least two AZs and enable ELB health checks for application-aware replacement.
- Keep instances stateless; externalize sessions to ElastiCache or DynamoDB for seamless replace/scale.
- Set appropriate warm-up time in scaling policies so metrics reflect steady-state before further scaling.
- Use termination policies (oldest instance, closest to next billing hour, oldest launch template) to control how ASG scales in.
- For cost control, combine On-Demand, Reserved/Savings Plans, and Spot with instance diversification.
- Protect critical instances temporarily with instance protection during deployments or maintenance.
- Use instance refresh or blue/green deployments to roll out new AMIs or configuration safely.
Steps to create an Auto Scaling Group (ASG) on AWS
- Go to the EC2 Dashboard.
- In the navigation pane, under Auto Scaling, choose Auto Scaling Groups.
- Click Create Auto Scaling group.
- Choose launch template or configuration:
- Configure settings:
- Select "Launch template" and choose the template you created (e.g.,
my-web-app-lt). - Click Next.
- Select "Launch template" and choose the template you created (e.g.,
- Configure group size and scaling policies:
- Auto Scaling group name: Provide a name (e.g.,
my-web-app-asg). - Network:
- VPC: Select your VPC.
- Availability Zones and subnets: Select at least two subnets in different AZs.
- Load balancing (optional but recommended):
- Choose "Attach to an existing load balancer".
- Select "Choose from your load balancer target groups" and pick your target group (e.g.,
my-alb-tg).
- Health checks:
- Health check type: Choose
EC2(default) orELB(if using a load balancer).ELBis recommended for application-level health. - Health check grace period: Time (in seconds) for instances to warm up before health checks begin (e.g., 300 seconds).
- Health check type: Choose
- Instance refresh (optional): Keep default for now.
- Click Next.
- Auto Scaling group name: Provide a name (e.g.,
- Add notifications (optional): Configure SNS topics for scaling events. Click Next.
- Add tags (optional): Add tags for organization and cost allocation. Click Next.
- Review: Review all settings. Click Create Auto Scaling group.
- Group size:
- Desired capacity: The number of instances ASG tries to maintain (e.g.,
2). - Minimum capacity: The smallest number of instances (e.g.,
2). - Maximum capacity: The largest number of instances (e.g.,
10).
- Desired capacity: The number of instances ASG tries to maintain (e.g.,
- Scaling policies:
- None: No automatic scaling (only manual changes or health checks).
- Target tracking scaling policy (Recommended):
- Policy name: (e.g.,
cpu-target-50). - Metric type: Choose a predefined metric (e.g.,
Average CPU utilization). - Target value: Set the target (e.g.,
50). ASG will scale to keep CPU around 50%. - Instances need: Set a warm-up period (e.g.,
300seconds).
- Policy name: (e.g.,
- Step scaling policies: More advanced, based on CloudWatch alarms.
- Scheduled actions: For predictable traffic patterns.
- Click Next.
- Group size:
