Phase 2: Core Cloud Services

Auto Scaling groups, scaling policies & health checks

Intermediate ~3 min read
Think of it this way A friendly analogy. Read this if the technical version feels dense. Show Hide

Imagine you're running a super popular pizza restaurant, and you've got chefs in the kitchen making all the delicious pizzas. This is a lot like how a computer program works – it needs "chefs" (which are like mini-computers called EC2 instances in the cloud) to do its job, like handling website visitors or running games. You want to make sure your pizza kitchen always has enough chefs to keep making pizzas, even if one chef gets tired or too many customers suddenly show up!

That's where your "Auto Scaling Group" comes in – it's like a super-smart kitchen manager for your restaurant. You tell this manager: "Always have at least two chefs, never more than ten, and right now, let's aim for about five chefs working." If one chef needs a break or accidentally burns a pizza, the kitchen manager instantly calls in a new chef to replace them so pizza-making never stops. And when a new chef arrives, they already know exactly what uniform to wear and which recipes to follow, so they can start cooking right away.

Now, how does your kitchen manager know when to hire more chefs or send some home? That's where "scaling policies" come in, which are like the rules for your manager. For example, you might tell them: "If all the chefs are working really hard, like they're cooking at 80% of their top speed, then it's time to hire more chefs until everyone is a bit more relaxed, maybe cooking at 60% speed." Or, "If we get more than 100 pizza orders per hour, bring in more help!" These rules help your kitchen manager automatically adjust how many chefs are working based on how busy the restaurant is.

So, when you build a website or a game, you don't have to constantly watch it to see if it's getting too busy or if one of its parts breaks down. Your Auto Scaling Group and its policies will take care of it, making sure your customers always get their pizzas, no matter what! This means your games and apps can handle huge numbers of players or visitors without slowing down, and you only pay for the "chefs" you actually need.

An Auto Scaling Group (ASG) is a fundamental AWS service that helps you maintain application availability and allows you to automatically adjust your EC2 capacity. Think of it as a logical grouping of EC2 instances managed as a single unit. Your ASG's primary goal is to ensure you always have a specified number of instances running, automatically launching new ones to replace unhealthy or terminated instances, and scaling out or in to match demand. You define the minimum, maximum, and desired capacity for your group, along with a launch template or configuration specifying how new instances should be provisioned (AMI, instance type, security groups, user data, etc.). This ensures consistent deployment and immediate replacement of any failing components.

While an ASG ensures a baseline number of instances, scaling policies are the brains that tell your ASG when and how to dynamically adjust its capacity. These policies react to changing application load or schedules. The most common and recommended type is Target Tracking Scaling, where you pick a metric (like average CPU utilization, network I/O, or an ALB request count) and a target value (e.g., "keep average CPU at 60%"). The ASG then automatically adds or removes instances to try and maintain that target. Other policies include Step Scaling (adds/removes instances in steps based on alarm breaches) and Scheduled Scaling (for predictable load changes, like increasing capacity every Monday morning). Choosing the right policy ensures your application performs optimally without over-provisioning and incurring unnecessary costs.

To maintain application availability, ASGs continuously monitor the health of instances within the group using health checks. There are two primary types: EC2 Status Checks and ELB Health Checks. EC2 Status Checks monitor the underlying AWS infrastructure (system status) and the instance's operating system (instance status) to ensure the VM itself is healthy. ELB Health Checks, often used when an ASG is behind a Load Balancer, go a step further by checking if your application running on the instance is actually responding to requests on a specific port and path (e.g., an HTTP 200 response from /health). If an instance fails a health check, the ASG marks it as unhealthy, takes it out of service, and automatically launches a replacement, significantly improving the fault tolerance and resilience of your architecture.

Key Takeaways

  • Auto Scaling Groups (ASGs) automate the management of EC2 instances, ensuring desired capacity and high availability.
  • Scaling policies dictate when and how ASGs adjust capacity in response to load or schedule.
  • Health checks automatically detect and replace unhealthy instances, boosting application resilience.
  • Combining ASGs, scaling policies, and health checks is crucial for building robust, cost-effective, and scalable cloud architectures.

Code Example

yaml
# Simplified CloudFormation for an ASG with Target Tracking Policy
Resources:
  MyAutoScalingGroup:
    Type: AWS::AutoScaling::AutoScalingGroup
    Properties:
      VPCZoneIdentifier: ["subnet-0abcdef1234567890"] # Your Subnet ID
      LaunchTemplate:
        LaunchTemplateId: lt-0abcdef1234567890 # Your Launch Template ID
        Version: '$LATEST'
      MinSize: '1'
      MaxSize: '5'
      DesiredCapacity: '2'
      HealthCheckType: EC2

  MyCPUScalingPolicy:
    Type: AWS::AutoScaling::ScalingPolicy
    Properties:
      AutoScalingGroupName: !Ref MyAutoScalingGroup
      PolicyType: TargetTracking
      TargetTrackingConfiguration:
        PredefinedMetricSpecification:
          PredefinedMetricType: ASGCPUUtilization
        TargetValue: 60.0

How this code works

This code defines an Auto Scaling Group (ASG) and a scaling policy, enabling an application to automatically adjust its EC2 instance count to match demand. The MyAutoScalingGroup resource sets up the core group, specifying where instances launch with VPCZoneIdentifier and their configuration using a LaunchTemplate. It defines the boundaries for the group with MinSize: '1' and MaxSize: '5', and an initial instance count of DesiredCapacity: '2'. A HealthCheckType: EC2 ensures that the ASG replaces any instances detected as unhealthy.

The MyCPUScalingPolicy then adds intelligent scaling to the ASG by referencing !Ref MyAutoScalingGroup. It uses a PolicyType: TargetTracking, which is excellent for maintaining a consistent performance level. Specifically, it monitors PredefinedMetricType: ASGCPUUtilization and aims to keep the average CPU utilization across the group at a TargetValue: 60.0. A subtle but crucial point for beginners is that while DesiredCapacity sets the initial instance count, once this TargetTracking policy is active, it dynamically overrides and manages the actual number of instances to achieve the 60% CPU target, scaling out or in as needed, rather than strictly adhering to the initial DesiredCapacity.