[英]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񱲍
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.