gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 04:19:29 32 4
gpt4 key购买 nike

到目前为止,我是这样设置闹钟的:

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 界面上,您可以创建一个停止或终止实例的警报。

最佳答案

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

关于java - 使用 Java 中的 Cloudwatch 警报停止 AWS EC2 实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17455979/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com