简体   繁体   中英

Setting CloudWatch Alarm for multiple instances

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM