All HowTo's

Add & Remove CloudWatch Alarms with AWS CLI

All sysadmins need to know the state of the servers they look after. CloudWatch lets sysadmins monitor their Amazon AWS resources and be alerted when things go wrong. And by wrong, i mean “out of the norm”. For example, if the CPU goes above a percentage, we should be notified via email. Actually you can even set rules to have the machine reboot if serious errors occur and your options don’t end there.

Before you can send emails from CloudWatch, you need to configure SNS. It’s easy to do. Go to the “SNS Dashboard” and create a new topic. You’ll have to verify your email address in the normal way (receive it in your email and click the link within it to verify that you’ve received it and you’re really you) and note down the ARN which looks something like this “arn:aws:sns:ap-southeast-2:111222333:my-sns-target”. we’ll need your SNS ARN string for later.

For this article we’re going to see how to create a new alarm and then delete it.

The following is an example of how to create an alarm that is active in the “ap-southeast-2” region, monitors CPU utilisation (anything more than 70% for a few minutes will trigger it) and send a notification to an SNS email adddress.

aws cloudwatch put-metric-alarm --region=ap-southeast-2 --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-123456" --evaluation-periods 2 --alarm-actions arn:aws:sns:ap-southeast-2:111222333:my-sns-target --unit Percent

Let’s see if the new alarm was created ok. Either run the following command which “should” list the details of the alarm but didn’t for me. Perhaps it needs some time? Not sure. Or you can use the Web Console to view the alarm. It “is” listed there for mw.

aws cloudwatch describe-alarms --alarm-names "cpu-mon"

You can delete our new alarm using the command:

aws cloudwatch delete-alarms --alarm-name cpu-mon

At this point we’re back where we started. So you can see that you can create alarms easily and as part of other scripts using the “aws cloudwatch” commands.

I hope this has been a time saver for you.

Leave a Reply

Your email address will not be published. Required fields are marked *