使用 AWS CLI 的方式,設定 CloudWatch 偵測到 StatusCheckFailed 時的 Auto recover action
用下面的命令可以成功創建用於恢復 EC2 執行個體的告警:
- 首先創建一個文檔並將下面的內容添加到文檔中,將文檔命名為 config.json,:
注意,您需要替換下面的 json 字串中的相應參數如 AlarmActions 中的 us-west-2 部分,以及 InstanceId。
===============================
{
"AlarmName": "TestRecovery",
"AlarmDescription": "auto recovery alarm for ec2",
"ActionsEnabled": false,
"AlarmActions": [
"arn:aws:automate:us-west-2:ec2:recover"
],
"MetricName": "StatusCheckFailed_System",
"Namespace": "AWS/EC2",
"Statistic": "Average",
"Dimensions": [{
"Name": "InstanceId",
"Value": "i-0217b37c13540a5c2"
}],
"Period": 60,
"EvaluationPeriods": 2,
"DatapointsToAlarm": 2,
"Threshold": 0.99,
"ComparisonOperator": "GreaterThanOrEqualToThreshold",
"TreatMissingData": "missing"
}
===============================
- 配置文件創建完畢後,執行下面的命令即可創建告警:
===============================
aws cloudwatch put-metric-alarm --cli-input-json file://config.json
===============================
更多關於如何使用 put-metric-alarm 的信息可以參考:
- https://docs.aws.amazon.com/cli/latest/reference/cloudwatch/put-metric-alarm.html