AWS CloudWatch Alarm On Startup Of New EC2 Instanc

2020-07-19 05:00发布

I want to apply a CloudWatch alarm to instances as they are being created. The alarm should send a message to an email account when CPU usage drops below 10% for 1 full day. I believe the best way of achieving this is by using a User data script at the time of launching the instance.

2条回答
ゆ 、 Hurt°
2楼-- · 2020-07-19 05:32

John Rotenstein is correct. What is required is to create an SNS topic, subscribe to it, and create a CloudWatch alarm on "CPUUtilization" metric. In order to do this automatically, either these steps need to be executed at the time when a new instance is started, or baked-in into the base AMI from which new instances are launched!

You can refer to AwsWinSysOps for a quick-start guide to setting it up.

AwsWinSysOps is a quick-start guide and package for monitoring AWS EC2 windows instances using AWS CloudWatch metrics.

查看更多
淡お忘
3楼-- · 2020-07-19 05:33

Yes, you could use User Data to create the CloudWatch Alarm and notification.

Start by creating an Amazon SNS topic for receiving notifications. Subscribe an email address to receive the notifications. This SNS topic can be used for all notifications, so only needs to be created once.

Then, create a User Data script to configure the alarm. I recommend using the AWS Command Line Interface (CLI). The example below assumes Linux, but you could do similar in Windows, especially PowerShell.

First, grab the InstanceId:

instance=`curl -s http://169.254.169.254/latest/meta-data/instance-id/`

Then, create an alarm to trigger when CPU drops below 10%, measured on an hourly average, over a period of 24 hours:

aws cloudwatch put-metric-alarm --alarm-name low-cpu --alarm-description "Alarm when CPU drops below 10% over a day" --metric-name CPUUtilization --namespace AWS/EC2 --statistic Average --period 3600 --threshold 10 --comparison-operator LessThanThreshold --dimensions Name=InstanceId,Value=$instance --evaluation-periods 24 --alarm-actions arn:aws:sns:us-east-1:111122223333:MyTopic --unit Percent

The actual values might take some tweaking to get the results you desire.

Alternatively, you could just write a script that runs on the instance itself, monitors itself, and sends out an email without involving CloudWatch. Less moving parts!

查看更多
登录 后发表回答