繁体   English   中英

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

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

到目前为止,我已经通过以下方式设置了警报:

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的指定实例创建警报。 但是,我找不到任何有关如何在警报进入ALARM状态时将该警报或SNS添加到停止或终止实例的文档。

我仅有的线索是,尽管.withAlarmActions()仅接受SNS或SQS操作,但在最坏的情况下SNS可以发出我可以使用的HTTP请求。

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

通过在亚马逊论坛中提问找到了答案。 基本上,我认为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

暂无
暂无

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

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