Autoscale based on SQS queue size

2019-07-13 20:11发布

I have put together these commands to autoscale EC2 instances based on SQS queue size. I have run all commands and my queue is at 10 messages and a single instance hasn't been launched.

I am trying to figure out, what SQS queue my cloudwatch alarms are listening to? Also any help to indentify the issus is appreciated!

### Create Autoscaling Policy ###

aws autoscaling create-launch-configuration --launch-configuration-name my-lc --image-id ami-551c6d30 --instance-type m1.small

aws autoscaling create-auto-scaling-group --auto-scaling-group-name my-asg --launch-configuration-name my-lc --availability-zones "us-east-1a" "us-east-1c" --max-size 10 --min-size 0 --desired-capacity 0

# Scale up policy
aws autoscaling put-scaling-policy --policy-name my-sqs-scaleout-policy --auto-scaling-group-name my-asg --scaling-adjustment 1 --adjustment-type ChangeInCapacity

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

# Alarm to scale up
aws cloudwatch put-metric-alarm --alarm-name AddCapacityToProcessQueue --metric-name ApproximateNumberOfMessagesVisible --namespace "AWS/SQS" --statistic Average --period 120 --threshold 3 --comparison-operator GreaterThanOrEqualToThreshold --dimensions Name=QueueName,Value=my-queue --evaluation-periods 2 --alarm-actions arn:aws:autoscaling:us-east-1:850082592395:scalingPolicy:6408b62d-9363-4252-a88c-5ffab08a8cb5:autoScalingGroupName/my-asg:policyName/my-sqs-scaleout-policy

# Alarm to scale down
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:aws:autoscaling:us-east-1:850082592395:scalingPolicy:4771ea64-2ebf-45ef-9328-50e058dc68b7:autoScalingGroupName/my-asg:policyName/my-sqs-scalein-policy

# Verify cloudwatch alarms
aws cloudwatch describe-alarms --alarm-names AddCapacityToProcessQueue RemoveCapacityFromProcessQueue

# Verify scaling policy
aws autoscaling describe-policies --auto-scaling-group-name my-asg

# Verify instances autoscaled
aws autoscaling describe-auto-scaling-groups --auto-scaling-group-name my-asg

1条回答
【Aperson】
2楼-- · 2019-07-13 20:49

The AWS Documentation states that:

Amazon Simple Queue Service sends data to CloudWatch every 5 minutes.

Additionally, you have specified an Average metric over several periods of time. Therefore, it will require several 5-minute periods to send Amazon SQS metrics to Amazon CloudWatch.

It's possible that the metric period (120 seconds) is too short to receive multiple updates from SQS, therefore causing INSUFFICIENT_DATA errors.

Start by trying to get the Alarm to trigger with a Maximum setting and play with the time periods. Once the Alarm is successfully triggering, play with the thresholds to get the desired behaviour.

查看更多
登录 后发表回答