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.
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.