新的EC2实例启动时AWS CloudWatch警报 [英] AWS CloudWatch Alarm On Startup Of New EC2 Instance

查看:89
本文介绍了新的EC2实例启动时AWS CloudWatch警报的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要在实例创建时将CloudWatch警报应用于实例。当CPU使用率连续1天下降到10%以下时,警报应向电子邮件帐户发送消息。我认为实现此目标的最佳方法是在启动实例时使用User数据脚本。

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.

推荐答案

是的,您可以使用用户数据来创建 CloudWatch警报和通知

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

首先创建一个 Amazon SNS主题接收通知。 订阅电子邮件地址以接收通知。此SNS主题可用于所有通知,因此只需创建一次。

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.

然后,创建一个用户数据脚本以配置报警。我建议使用 AWS命令行界面(CLI) 。下面的示例假定使用Linux,但是您可以在Windows中执行类似的操作,尤其是PowerShell。

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.

首先,获取InstanceId:

First, grab the InstanceId:

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

然后,创建警报,以在24小时内每小时平均CPU下降到10%以下时触发:

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 CPU低于10%时发出警报一整天 --metric-name CPUUtilization --namespace AWS / EC2 --statistic平均值--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

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.

或者,您只需编写一个脚本即可在实例本身上运行,监控自身并发送电子邮件,而无需涉及CloudWatch。运动部件更少!

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!

这篇关于新的EC2实例启动时AWS CloudWatch警报的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆