简体   繁体   English

在Java中使用Cloudwatch警报停止AWS EC2实例

[英]Stopping an AWS EC2 Instance with a Cloudwatch Alarm in Java

So far, I have set up an alarm in such a way: 到目前为止,我已经通过以下方式设置了警报:

dim = new Dimension()
        .withName("InstanceId")
        .withValue(hashedId);

alarmreq = new PutMetricAlarmRequest()
              .withDimensions(dim)
              .withMetricName(metricName)
              .withNamespace(nameSpace)
              .withAlarmName(alarmName)
              .withActionsEnabled(true)
              .withStatistic(statistic)
              .withThreshold(threshold)
              .withComparisonOperator("GreaterThanThreshold")
              .withPeriod(period)
              .withEvaluationPeriods(evaluationPeriods)
              .withAlarmActions("arn:aws:sns:us-west-2:xxxxxxxxx:NotifyMe");

gCloudWatch.putMetricAlarm(alarmreq);

This creates an alarm for the specified instance just fine that executes the SNS NotifyMe. 这会为执行SNS NotifyMe的指定实例创建警报。 However, I am unable to find any documentation on how to add to this alarm, or maybe the SNS, to Stop or Terminate the instance when the alarm goes to ALARM state. 但是,我找不到任何有关如何在警报进入ALARM状态时将该警报或SNS添加到停止或终止实例的文档。

The only leads I have is that although .withAlarmActions() only accepts an SNS or SQS action, SNS can make an HTTP request that I may be able to work with in the very worst case. 我仅有的线索是,尽管.withAlarmActions()仅接受SNS或SQS操作,但在最坏的情况下SNS可以发出我可以使用的HTTP请求。

Also I know it is possible to add this functionality to an alarm because on the AWS web interface, you may create an alarm that stops or terminates the instance. 我也知道可以将此功能添加到警报中,因为您可以在AWS Web界面上创建一个用于停止或终止实例的警报。

Found the answer through asking in the Amazon forums. 通过在亚马逊论坛中提问找到了答案。 Basically, I was wrong in the assumption that withAlarmActions only accepts SNS or SQS actions. 基本上,我认为withAlarmActions仅接受SNS或SQS操作是错误的。 It can also accept stop or terminate actions in the form of "arn:aws:automate:us-west-2:ec2:stop". 它还可以接受“ arn:aws:automate:us-west-2:ec2:stop”形式的停止或终止操作。 The final line of code edited to be fixed would look like: 编辑为固定的最后一行代码如下所示:

.withAlarmActions("arn:aws:sns:us-west-2:xxxxxxxxx:NotifyMe", "arn:aws:automate:us-west-2:ec2:stop");

Here is the full answer if anyone who is curious. 如果有人好奇,这是完整的答案。 https://forums.aws.amazon.com/thread.jspa?messageID=466061&#466061 https://forums.aws.amazon.com/thread.jspa?messageID=466061&#466061

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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