Launch Templates and ASG Configuration
Launch Templates define the configuration for ASG instances: AMI, instance type, security groups, key pair, storage, and user data. They support versioning—create new versions when updating your base configuration. Launch Templates also enable mixed instance policies for cost optimization.
Auto Scaling Groups manage a collection of EC2 instances as a single logical unit. Key parameters include: Min size (baseline capacity), Max size (upper limit), Desired capacity (target count), and VPC zone distribution for high availability across AZs.
ASG automatically replaces unhealthy instances by monitoring EC2 status checks and ELB health checks. For production, always span at least 2 AZs and configure health check grace period to give instances time to pass health checks before being marked unhealthy.
Scaling Policies
Target Tracking maintains a metric at a target value—simplest approach. Set target CPU utilization at 50% and ASG adds/removes instances to maintain that target. Works well for predictable workloads.
Step Scaling uses CloudWatch alarms with custom thresholds. Create alarms for CPU > 70% (scale out +2) and CPU < 30% (scale in -1). Offers granular control but more complex to set up.
Simple Scaling triggers a single adjustment based on one alarm—useful for basic scenarios but less responsive than step scaling.
Scheduled Scaling anticipates known traffic patterns. Scale out before business hours (8 AM) and scale in at night (10 PM). Combine with Predictive Scaling which uses ML to forecast traffic and pre-provision capacity. For event-driven scaling, use Scheduled Actions with cron expressions for maximum predictability.
Lifecycle Hooks and Instance Refresh
Lifecycle Hooks pause instances during state transitions for custom actions. When an instance launches, a lifecycle hook puts it in Pending:Wait state—run user data scripts, register with load balancers, or pull secrets—then signal Continue to move to InService. This prevents unready instances from receiving traffic.
Instance Refresh enables zero-downtime deployments. When you update the launch template, trigger an instance refresh that gradually replaces old instances with new ones. Configure Min Healthy Percentage (e.g., 90%) to ensure capacity during rollout and Instance Warmup to wait for instances to be ready before replacing the next batch.
Warm Pools keep stopped instances ready to scale faster. Instead of launching new instances (2-5 minutes), warm pools start stopped instances in seconds. Configure pool size and instance reuse policy. Ideal for latency-sensitive applications that need rapid scaling.
ASG Best Practices and Cost Optimization
Set Health Check Type to ELB (not EC2) so ASG considers application health, not just instance health. Configure Grace Period to match your application startup time—too short causes premature termination.
Use Mixed Instance Policies to combine On-Demand and Spot instances in a single ASG. Configure Spot allocation strategies (capacity-optimized preferred) to minimize interruptions. Set instance type overrides for fallback when Spot capacity is unavailable.
Termination Policies control which instance gets removed first. Default is OldestInstance—use OldestLaunchTemplate for cleaner rollouts or ClosestToNextInstanceHour for Spot cost optimization.
Monitor ASG with CloudWatch metrics: GroupDesiredCapacity, GroupInServiceInstances, GroupPendingInstances. Set alarms on GroupTotalInstances approaching max capacity to prevent scaling failures. Enable Capacity Rebalancing to proactively replace Spot instances at risk of interruption.
Quiz
1. Which scaling policy is simplest to configure for maintaining consistent CPU utilization?
2. What does an Instance Refresh do?
3. What is the purpose of a Warm Pool in an Auto Scaling Group?
Flashcards
Question
What is an Auto Scaling Group (ASG)?
Click to reveal answer
Answer
A collection of EC2 instances managed as a single logical unit that automatically adjusts capacity to meet demand.
Question
What is Target Tracking scaling?
Click to reveal answer
Answer
A scaling policy that automatically adjusts instance count to keep a specified metric (like CPU) at a target value.
Question
What are Lifecycle Hooks?
Click to reveal answer
Answer
Mechanisms that pause instances during state transitions for custom initialization before they receive traffic.
Question
What is the recommended ASG configuration for production?
Click to reveal answer
Answer
Span at least 2 AZs, set min=2, use ELB health checks, and configure a health check grace period matching app startup time.
Revision Notes
Key Takeaways
- 1. Launch Templates version your instance configuration; ASGs manage groups of instances
- 2. Target Tracking is the simplest scaling policy for steady-state; use Scheduled for predictable patterns
- 3. Lifecycle Hooks pause instances for custom init; Instance Refresh enables zero-downtime deployments
- 4. Warm Pools reduce scaling latency by keeping stopped instances ready
Interview Tips
- • Explain how ASG replaces unhealthy instances automatically
- • Compare Target Tracking vs Step Scaling and when to use each
- • Describe the purpose of lifecycle hooks in deployment workflows
- • Discuss mixed instance policies for cost optimization with Spot
Cheat Sheet
ASG = groups of EC2 instances with auto-scaling. Config: min/desired/max, AZs, health checks. Scaling: Target Tracking (simplest), Step Scaling (granular), Scheduled (predictable). Instance Refresh = zero-downtime deployments. Warm Pools = stopped instances for fast scaling. Lifecycle Hooks = pause for custom init.