Reputation: 3
I'm configuring an alarm for an instance from the Amazon CLI. for example, to trigger a notification when the cpu is idle for 5min. but I want to set this alarm for a lot of Instances.
With this Bash Script I created one alarm for one instance :
aws cloudwatch put-metric-alarm --alarm-name cpu-mon --alarm-description "Alarm when CPU exceeds 70 percent" --metric-name CPUUtilization --namespace AWS/EC2 --statistic Average --period 300 --threshold 70 --comparison-operator GreaterThanThreshold --dimensions "Name=InstanceId,Value=i-12345678" --evaluation-periods 2 --alarm-actions arn:aws:sns:us-east-1:111122223333:MyTopic --unit Percent
So, I don't see how can I use this script to choose another instances, or eventually loop on that script, in order to choose another instances.
Upvotes: 0
Views: 4506
Reputation: 498
If you have a list of instance IDs you want to create alarms for you could do something like:
#!/bin/bash
instances=(instanceId1 instanceId2 etc)
for i in "${instances[@]}"; do
aws cloudwatch put-metric-alarm \
--alarm-name cpu-mon-${i} \
--alarm-description "Alarm when CPU exceeds 70 percent" \
--metric-name CPUUtilization \
--namespace AWS/EC2 \
--statistic Average \
--period 300 \
--threshold 70 \
--comparison-operator GreaterThanThreshold \
--dimensions "Name=InstanceId,Value=${i}" \
--evaluation-periods 2 \
--alarm-actions arn:aws:sns:us-east-1:111122223333:MyTopic \
--unit Percent
done
You could also initially use the AWS CLI to grab instance IDs based on tags, instance names etc and then use those to create the alarms along the same lines.
Upvotes: 3