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 ?
In this post, we’ll see how to set Cloudwatch alarms using command line for multiple servers.
Gather instance IDs and Names in a CSV file:
1 | 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:1
2
3
4cat /home/varun/instance_details.csv
-1234567890abcdef0,Server1
i-abcdef01234567890,Server2
i-01a2b3c4d5e6f7890,Server3
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.
Create the shell script for cloudwatch alarms
1 | SNS_TOPIC_ARN="arn:aws:sns:us-east-1:111122223333:MyTopic" |
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:
–comparison-operator
1 | GreaterThanOrEqualToThreshold |
–unit
1 | Seconds |
–statistic
1 | SampleCount |
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.