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

时间:2013-07-03 18:53:48

标签: java amazon-web-services amazon-ec2 amazon-cloudwatch

到目前为止,我已经以这种方式设置了警报:

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);

这会为执行SNS NotifyMe的指定实例创建一个警报。但是,当警报进入警报状态时,我无法找到有关如何添加到此警报或SNS的任何文档来停止或终止实例。

我唯一的领导是虽然.withAlarmActions()只接受SNS或SQS操作,但SNS可以发出一个我可以在最糟糕的情况下使用的HTTP请求。

此外,我知道可以将此功能添加到警报中,因为在AWS Web界面上,您可以创建一个停止或终止实例的警报。

1 个答案:

答案 0 :(得分:2)

通过在亚马逊论坛中提问来找到答案。基本上,我认为withAlarmActions只接受SNS或SQS动作是错误的。它还可以接受以“arn:aws:automate:us-west-2:ec2:stop”形式停止或终止动作。编辑修复的最后一行代码如下:

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

如果有人好奇,这是完整的答案。 https://forums.aws.amazon.com/thread.jspa?messageID=466061&#466061

相关问题