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
4
cat /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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
SNS_TOPIC_ARN="arn:aws:sns:us-east-1:111122223333:MyTopic"

IFS=','
while read -r INST_ID INST_NAME; do
aws cloudwatch put-metric-alarm \
--alarm-name "$INST_NAME CPU USAGE > 80%" \
--alarm-description "$INST_NAME CPU USAGE > 80%" \
--actions-enabled \
--alarm-actions "$SNS_TOPIC_ARN" \
--metric-name CPUUtilization \
--namespace AWS/EC2 \
--statistic Average \
--dimensions "Name=InstanceId,Value=$INST_ID" \
--period 300 \
--unit Percent \
--evaluation-periods 1 \
--threshold 80 \
--comparison-operator GreaterThanThreshold
done < /home/varun/instance_details.csv

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
2
3
4
GreaterThanOrEqualToThreshold
GreaterThanThreshold
LessThanThreshold
LessThanOrEqualToThreshold

–unit

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
Seconds
Microseconds
Milliseconds
Bytes
Kilobytes
Megabytes
Gigabytes
Terabytes
Bits
Kilobits
Megabits
Gigabits
Terabits
Percent
Count
Bytes/Second
Kilobytes/Second
Megabytes/Second
Gigabytes/Second
Terabytes/Second
Bits/Second
Kilobits/Second
Megabits/Second
Gigabits/Second
Terabits/Second
Count/Second
None

–statistic

1
2
3
4
5
SampleCount
Average
Sum
Minimum
Maximum

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.

amazon web services aws alarm awscli cloud cloudwatch command command line cpu instance linux sns ubuntu