2 min read

AWS Step Scaling Policies: An Overview

AWS Step Scaling Policies: An Overview
Photo by Vitali Adutskevich / Unsplash

Auto Scaling is an AWS service that helps you ensure that you have the correct number of EC2 instances available to handle the load for your apps

Auto Scaling is an AWS service that helps you ensure that you have the correct number of Amazon EC2 instances available to handle the load for your applications. To achieve this, you can set up scaling policies that determine when and how to adjust the number of EC2 instances in your Auto Scaling group.

One type of scaling policy you can use is step scaling. Step scaling policies adjust the number of EC2 instances in your Auto Scaling group based on the value of a metric relative to specified thresholds. In this blog post, we'll go over the basics of step scaling policies and how to write them correctly in CloudFormation.

Step Scaling Policies

Step scaling policies allow you to define a series of steps that the Auto Scaling group should take when the metric value crosses specified thresholds. You can think of these steps as "if-then" statements. For example, if the metric value is below a certain threshold, you might want to add one EC2 instance. If the metric value is above another threshold, you might want to remove one EC2 instance.

Here's an example of how step-scaling policies work:

  1. You specify the metric to be used for scaling. This could be something like the average CPU utilization of your EC2 instances, or the number of requests per second.

  2. You specify the thresholds for the metric, which determine when the scaling policies should take effect.

  3. You specify the number of EC2 instances to add or remove based on the metric value relative to the thresholds.

Step scaling policies are flexible and can be fine-tuned to your specific needs. For example, you can specify different numbers of EC2 instances to add or remove based on the size of the metric value relative to the thresholds.

How to Write Step Scaling Policies in CloudFormation

Now that you understand the basics of step scaling policies, let's look at how to write them correctly in CloudFormation.

Here's a sample CloudFormation template for a step scaling policy:

{
  "Type": "AWS::AutoScaling::ScalingPolicy",
  "Properties": {
    "AdjustmentType": "ChangeInCapacity",
    "AutoScalingGroupName": {
      "Ref": "AutoScalingGroup"
    },
    "PolicyType": "StepScaling",
    "StepAdjustments": [
      {
        "MetricIntervalLowerBound": 0,
        "ScalingAdjustment": 2
      },
      {
        "MetricIntervalLowerBound": 10,
        "MetricIntervalUpperBound": 20,
        "ScalingAdjustment": 4
      },
      {
        "MetricIntervalLowerBound": 20,
        "ScalingAdjustment": 6
      }
    ],
    "MetricAggregationType": "Average"
  }
}

In this example, the step scaling policy increases the number of EC2 instances in the Auto Scaling group by 2 if the average CPU utilization is between 0 and 10, by 4 if the average CPU utilization is between 10 and 20, and by 6 if the average CPU utilization is greater than 20.

With scale in policies, you can decrease the number of EC2 instances in your group based on the same set of rules you define for step scaling policies. To create a scale in policy, you simply set the ScalingAdjustment property to a negative value, indicating that you want to decrease the number of EC2 instances in your group.

In summary, step scaling policies are a useful tool for automatically adjusting the number of EC2 instances in your Auto Scaling group based on conditions you define. By creating step scaling policies and scale-in policies, you can ensure that your Auto Scaling group is always running the optimal number of EC2 instances, which can help reduce costs and improve application performance.