3
votes

My goal is to test AutoScaling based on memory used on my EC2 instances.

To monitor my EC2 instance memory, I installed CloudWatch agent on my EC2 Instance and created and configured my CloudWatch agent configuration file, as explained here: https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/Install-CloudWatch-Agent.html https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/create-cloudwatch-agent-configuration-file.html

{
        "metrics": {
                "append_dimensions": {
                        "AutoScalingGroupName": "${aws:AutoScalingGroupName}",
                        "ImageId": "${aws:ImageId}",
                        "InstanceId": "${aws:InstanceId}",
                        "InstanceType": "${aws:InstanceType}"
                },
                "metrics_collected": {
                        "cpu": {
                                "measurement": [
                                        "cpu_usage_idle",
                                        "cpu_usage_iowait",
                                        "cpu_usage_user",
                                        "cpu_usage_system"
                                ],
                                "metrics_collection_interval": 60,
                                "totalcpu": false
                        },
                        "disk": {
                                "measurement": [
                                        "used_percent",
                                        "inodes_free"
                                ],
                                "metrics_collection_interval": 60,
                                "resources": [
                                        "*"
                                ]
                        },
                        "diskio": {
                                "measurement": [
                                        "io_time"
                                ],
                                "metrics_collection_interval": 60,
                                "resources": [
                                        "*"
                                ]
                        },
                        "mem": {
                                "measurement": [
                                        "mem_used_percent"
                                ],
                                "metrics_collection_interval": 60
                        },
                        "swap": {
                                "measurement": [
                                        "swap_used_percent"
                                ],
                                "metrics_collection_interval": 60
                        }
                }
        }
}

Once I start the CloudWatch agent on the EC2 Instance, the custom metrics work ok and I can see them on CloudWatch.

sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -a fetch-config -m ec2 -c ssm:configuration-parameter-store-name -s

I created an AMI with this EC2 Instance and created an AutoScaling group with this AMI. I then set up an alarm based on the custom metric "mem_used_percent" (when >=50) and used it as an AutoScaling Group Policy (add 1 Instance). Image attached.

enter image description here

I connect to the EC2 instance and increase the memory used using "Stress". The alarm is raised, the AutoScaling policy is triggered and a new EC2 instance is created.

The problem I see is that I am creating an alarm on an specific EC2 Instance, instead of the whole AutoScaling Group, and once the initial EC2 Instance is terminated, the alarm does not make sense any more. How could I configured the same idea but for the AutoScaling Group?

Thanks.

2
Aggregate Statistics by Auto Scaling Group is what you are looking for? docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/…qkhanhpro
Yes, but for a custom metric using CloudWatch agent.Luis
I think you can check out the [Aggregating or Rolling Up Metrics Collected by the CloudWatch Agent] section docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/… If you use Pearl script, check out the --aggregate / --autoscaling options docs.aws.amazon.com/AWSEC2/latest/UserGuide/mon-scripts.htmlqkhanhpro
Thanks qkhanhpro. I´ll test that and let you know if it worked.Luis
"aggregation_dimensions" : [["AutoScalingGroupName"], ["InstanceId", "InstanceType"]]. That worked like a charm. @qkhanhpro - do you want to put it as the answer?Luis

2 Answers

6
votes

From AWS documentation,

For CloudWatch agent, You can aggregate custom metric using aggregation_dimensions

"metrics": {
  "cpu":{...}
  "disk":{...}
  "aggregation_dimensions" : [["AutoScalingGroupName"], ["InstanceId", "InstanceType"]]
}

https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/CloudWatch-Agent-common-scenarios.html

For those who are still using the old perl script, check the options :

--aggregated
--auto-scaling

https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/mon-scripts.html

0
votes

I have taken the same approach but with no success.

{
"metrics": {
    "metrics_collected": {
        "LogicalDisk": {
            "measurement": [
                "% Free Space"
            ],
            "metrics_collection_interval": 60,
            "resources": [
                "*"
            ]
        },
        "Memory": {
            "measurement": [
                "% Committed Bytes In Use"
            ],
            "metrics_collection_interval": 60
        },
        "Paging File": {
            "measurement": [
                "% Usage"
            ],
            "metrics_collection_interval": 60,
            "resources": [
                "*"
            ]
        },
        "PhysicalDisk": {
            "measurement": [
                "% Disk Time"
            ],
            "metrics_collection_interval": 60,
            "resources": [
                "*"
            ]
        },
        "Processor": {
            "measurement": [
                "% User Time",
                "% Idle Time",
                "% Interrupt Time"
            ],
            "metrics_collection_interval": 60,
            "resources": [
                "_Total"
            ]
        }
    },
    "append_dimensions": {
        "ImageId": "${aws:ImageId}",
        "InstanceId": "${aws:InstanceId}",
        "InstanceType": "${aws:InstanceType}",
        "AutoScalingGroupName": "${aws:AutoScalingGroupName}"
    },
    "aggregation_dimensions" : [["AutoScalingGroupName"]]
}

}

When i review the output in the cloudwatch management console, i do not see the aggregation by the AutoScalingGroupName under All>CWAgent