Alarms on custom metrics in Autoscaling group
Recently, I came across an interesting use case i.e to put alarms on custom metrics of autoscaling instances. Lets consider there is an autoscaling group for production environment and you need to put alarm on disk and memory utilization custom metrics. To accomplish that you need to follow the steps described below:
1. Create disk and memory utilization custom metrics and apply it on one of the production environment instance. Let the instance be named as X.
2. Put alarm on them as follows:
[shell]aws cloudwatch put-metric-alarm –alarm-name "Test" –metric-name "DiskSpaceUtilization-/dev/xvda1" –namespace System/Linux –statistic Average –period 60 –threshold 70 –comparison-operator GreaterThanThreshold –dimensions Name=InstanceId,Value=xxxxxxxx Name=Filesystem,Value=/dev/xvda1 Name=MountPath,Value=/ –evaluation-periods 1 –alarm-actions arn:aws:sns:region:account_id:topic[/shell]
3.Create AMI of this instance and use this AMI as the new AMI in your launch configuration.
4.Pass the user data given below in your launch configuration:
instance-id='ec2metadata --instance-id` aws cloudwatch put-metric-alarm --alarm-name "Prod-High-Disk-Usage" --metric-name "Production-Portal-Disk-Space-Utilization-$instance-id/" --namespace System/Linux --statistic Average --period 60 --threshold 70 --comparison-operator GreaterThanThreshold --dimensions Name=InstanceId,Value=xxxxxxxx Name=Filesystem,Value=/dev/xvda1 Name=MountPath,Value=/ --evaluation-periods 1 --alarm-actions arn:aws:sns:region:account_id:topic aws cloudwatch put-metric-alarm --alarm-name "Prod-Low-Disk-Usage" --metric-name "Production-Portal-Disk-Space-Utilization-$instance-id/" --namespace System/Linux --statistic Average --period 60 --threshold 70 --comparison-operator GreaterThanThreshold --dimensions Name=InstanceId,Value=xxxxxxxx Name=Filesystem,Value=/dev/xvda1 Name=MountPath,Value=/ --evaluation-periods 1 --alarm-actions arn:aws:sns:region:account_id:topic aws cloudwatch put-metric-alarm --alarm-name "Prod-High-Memory-Usage-" --metric-name "Production-Portal-Memory-Utilization-$instance-id" --namespace System/Linux --statistic Average --period 60 --threshold 70 --comparison-operator GreaterThanThreshold --dimensions Name=InstanceId,Value=xxxxxxxx --evaluation-periods 1 --alarm-actions arn:aws:sns:region:account_id:topic
Note: Here xxxxxxx is the instance id of the instance whose AMI you had created in step 3.
We need to provide the instance id of the instance X in the dimension for the new servers alarm, that will be launch while scaling up. If you do not follow the same then it will send the alarm in “Insufficient state”.