Menu
Auto Scaling
User Guide

Scaling Based on Amazon SQS

Amazon Simple Queue Service (Amazon SQS) is a scalable message queuing system that stores messages as they travel between various components of your application architecture. Amazon SQS enables web service applications to quickly and reliably queue messages that are generated by one component and consumed by another component. A queue is a temporary repository for messages that are awaiting processing. For more information, see the Amazon Simple Queue Service Developer Guide.

For example, suppose that you have a web app that receives orders from customers. The app runs on EC2 instances in an Auto Scaling group that is configured to handle a typical number of orders. The app places the orders in an Amazon SQS queue until they are picked up for processing, processes the orders, and then sends the processed orders back to the customer. The following diagram illustrates the architecture of this example.


                    Auto Scaling using queues architectural diagram

This architecture works well if your order levels remain the same at all times. What happens if your order levels change? You would need to launch additional EC2 instances when the orders increase and terminate the extra EC2 instances when the orders decrease. If your orders increase and decrease on a predictable schedule, you can specify the time and date to perform scaling activities. For more information, see Scheduled Scaling. Otherwise, you can scale based on criteria, such as the number of messages in your SQS queue. For more information, see Dynamic Scaling.

Queues provide a convenient mechanism to determine the load on an application. You can use the length of the queue (number of messages available for retrieval from the queue) to determine the load. Because each message in the queue represents a request from a user, measuring the length of the queue is a fair approximation of the load on the application. CloudWatch integrates with Amazon SQS to collect, view, and analyze metrics from SQS queues. You can use the metrics sent by Amazon SQS to determine the length of the SQS queue at any point in time. For a list of all the metrics that Amazon SQS sends to CloudWatch, see Amazon SQS Metrics in the Amazon Simple Queue Service Developer Guide.

The following examples create Auto Scaling policies that configure your Auto Scaling group to scale based on the number of messages in your SQS queue.

Scaling with Amazon SQS Using the AWS CLI

The following example shows you how to create policies for scaling in and scaling out, plus create, verify, and validate CloudWatch alarms for your scaling policies. It assumes that you already have an SQS queue, an Auto Scaling group, and EC2 instances running the application that uses the SQS queue.

Create the Scaling Policies

You can create scaling policies that tell the Auto Scaling group what to do when the specified conditions change.

To create scaling policies

  1. Use the following put-scaling-policy command to create a scale-out policy to increase the Auto Scaling group by one EC2 instance:

    Copy
    aws autoscaling put-scaling-policy --policy-name my-sqs-scaleout-policy -–auto-scaling-group-name my-asg --scaling-adjustment 1 --adjustment-type ChangeInCapacity

    Auto Scaling returns the Amazon Resource Name (ARN) for the new policy. Store the ARN in a safe place. You need it when you create the CloudWatch alarms.

  2. Use the following put-scaling-policy command to create a scale-in policy to decrease the Auto Scaling group by one EC2 instance:

    Copy
    aws autoscaling put-scaling-policy --policy-name my-sqs-scalein-policy --auto-scaling-group-name my-asg --scaling-adjustment -1 --adjustment-type ChangeInCapacity

    Auto Scaling returns the ARN for the new policy. Store the ARN in a safe place. You need it when you create the CloudWatch alarms.

Create the CloudWatch Alarms

Next, you create alarms by identifying the metrics to watch, defining the conditions for scaling, and then associating the alarms with the scaling policies that you created in the previous task.

Note

All active SQS queues send metrics to CloudWatch every five minutes. We recommend that you set the alarm Period to at least 300 seconds. Setting the alarm Period to less than 300 seconds results in the alarm going to the INSUFFICIENT_DATA state while waiting for the metrics.

To create CloudWatch alarms

  1. Use the following put-metric-alarm command to create an alarm that increases the size of the Auto Scaling group when the number of messages in the queue available for processing (ApproximateNumberOfMessagesVisible) increases to three and remains at three or greater for at least five minutes.

    Copy
    aws cloudwatch put-metric-alarm --alarm-name AddCapacityToProcessQueue --metric-name ApproximateNumberOfMessagesVisible --namespace "AWS/SQS" --statistic Average --period 300 --threshold 3 --comparison-operator GreaterThanOrEqualToThreshold --dimensions Name=QueueName,Value=my-queue --evaluation-periods 2 --alarm-actions arn
  2. Use the following put-metric-alarm command to create an alarm that decreases the size of the Auto Scaling group when the number of messages in the queue available for processing (ApproximateNumberOfMessagesVisible) decreases to one and the length remains at one or fewer for at least five minutes.

    Copy
    aws cloudwatch put-metric-alarm --alarm-name RemoveCapacityFromProcessQueue --metric-name ApproximateNumberOfMessagesVisible --namespace "AWS/SQS" --statistic Average --period 300 --threshold 1 --comparison-operator LessThanOrEqualToThreshold --dimensions Name=QueueName,Value=my-queue --evaluation-periods 2 --alarm-actions arn

Verify Your Scaling Policies and CloudWatch Alarms

You can verify that your CloudWatch alarms and scaling policies were created.

To verify your CloudWatch alarms

Use the following describe-alarms command:

Copy
aws cloudwatch describe-alarms --alarm-names AddCapacityToProcessQueue RemoveCapacityFromProcessQueue

To verify your scaling policies

Use the following describe-policies command:

Copy
aws autoscaling describe-policies --auto-scaling-group-name my-asg

Test Your Scale Out and Scale In Policies

You can test your scale-out policy by increasing the number of messages in your SQS queue and then verifying that your Auto Scaling group has launched an additional EC2 instance. Similarly, you can test your scale-in policy by decreasing the number of messages in your SQS queue and then verifying that the Auto Scaling group has terminated an EC2 instance.

To test the scale-out policy

  1. Follow the steps in Getting Started with Amazon SQS to add messages to your SQS queue. Make sure that you have at least three messages in the queue.

    It takes a few minutes for the SQS queue metric ApproximateNumberOfmessagesVisible to invoke the CloudWatch alarm. After the CloudWatch alarm is invoked, it notifies the Auto Scaling policy to launch one EC2 instance.

  2. Use the following describe-auto-scaling-groups command to verify that the group has launched an instance:

    Copy
    aws autoscaling describe-auto-scaling-groups --auto-scaling-group-name my-asg

To test the scale-in policy

  1. Follow the steps in Getting Started with Amazon SQS to remove messages from the SQS queue. Make sure that you have no more than one message in the queue.

    It takes a few minutes for the SQS queue metric ApproximateNumberOfmessagesVisible to invoke the CloudWatch alarm. After the CloudWatch alarm is invoked, it notifies the Auto Scaling policy to terminate one EC2 instance.

  2. Use the following describe-auto-scaling-groups command to verify that the group has terminated an instance:

    Copy
    aws autoscaling describe-auto-scaling-groups --auto-scaling-group-name my-asg