AWS CloudWatch alarms using AWS command line
Setting AWS Cloudwatch alarms using AWS Console might be easy for 1 or 2 instances, but what if the number of instances are around 50, or 100 ? Doesn’t look easy, right ?
aws ec2 describe-instances --query 'Reservations[*].Instances[*].[Tags[?Key==`Name`].Value, InstanceId]' --output text | paste -d, - - > /home/varun/instance_details.csv
If you check the content of /home/varun/instance_details.csv:
The above is just an example. Your CSV might have more than 3 lines.
_Note: remove the lines, from /home/varun/instance_details.csv, of instance ID you do not want to set alarms on._
Once the CSV file is confirmed, we can proceed to the next step.
Note: The following step assumes that you have setup SNS for receiving alarm notifications.
The above command will set alarm on $INST_ID, for CPUUtilization Metric, with alarm name “$INST_NAME CPU USAGE > 80%“, which will trigger SNS alert on “$SNS_TOPIC_ARN“, if the threshold crosses 80% for a period of 5 minutes.
We can also change the above script use the below parameters as per our requirement:
It is recommended to test the above command on 1 server before running on all the servers. I am not responsible for any mishaps occurred due to such.
Comments are welcome.