Search code examples
amazon-web-servicesamazon-ec2amazon-cloudwatchaws-auto-scaling

Pushing metric data to Cloudwatch for custom auto scaling


We would like to autoscale based on SQS size.

I was able to follow the below link and add custom autoscaling policy across the auto scaling group.

https://docs.aws.amazon.com/autoscaling/ec2/userguide/ec2-auto-scaling-target-tracking-metric-math.html

{
    "CustomizedMetricSpecification": {
        "Metrics": [
            {
                "Label": "Get the queue size (the number of messages waiting to be processed)",
                "Id": "m1",
                "MetricStat": {
                    "Metric": {
                        "MetricName": "ApproximateNumberOfMessagesVisible",
                        "Namespace": "AWS/SQS",
                        "Dimensions": [
                            {
                                "Name": "QueueName",
                                "Value": "my-queue"
                            }
                        ]
                    },
                    "Stat": "Sum"
                },
                "ReturnData": false
            },
            {
                "Label": "Get the group size (the number of InService instances)",
                "Id": "m2",
                "MetricStat": {
                    "Metric": {
                        "MetricName": "GroupInServiceInstances",
                        "Namespace": "AWS/AutoScaling",
                        "Dimensions": [
                            {
                                "Name": "AutoScalingGroupName",
                                "Value": "my-asg"
                            }
                        ]
                    },
                    "Stat": "Average"
                },
                "ReturnData": false
            },
            {
                "Label": "Calculate the backlog per instance",
                "Id": "e1",
                "Expression": "m1 / m2",
                "ReturnData": true
            }
        ]
    },
    "TargetValue": 100
}

After execution of this snippet, I see two alarms one for scale in and another for scale out.

Question: Will the above step also publish metric data to Cloudwatch? Because I don't see metric data populating which means no scaling is happening.


Solution

  • You don't need to send any SQS metrics to CloudWatch, they are sent by SQS by default. You can find the ApproximateNumberOfMessagesVisible metric in CloudWatch, you just need to send messages to my-queue and wait a bit (actually, it can take a couple of minutes).