2016-12-06 42 views
0

我一直在撞牆試圖做這項工作。 我正在嘗試使用python/boto創建恢復失敗的ec2實例的cloutwatch警報。 我在獲取ec2:RecoverInstance操作時很困難。我懷疑我的話題沒有正確設置。Boto/Cloudwatch恢復實例鬧鐘

topics = sns_conn.get_all_topics() 

topic = topics[u'ListTopicsResponse']['ListTopicsResult']['Topics'][0]['TopicArn'] 

# arn:aws:sns:us-east-1:*********:CloudWatch 

status_check_failed_alarm = boto.ec2.cloudwatch.alarm.MetricAlarm(
     connection=cw_conn, 
     name=_INSTANCE_NAME + "RECOVERY-High-Status-Check-Failed-Any", 
     metric='StatusCheckFailed', 
     namespace='AWS/EC2', 
     statistic='Average', 
     comparison='>=', 
     description='status check for %s %s' % (_INSTANCE, _INSTANCE_NAME), 
     threshold=1.0, 
     period=60, 
     evaluation_periods=5, 
     dimensions={'InstanceId': _INSTANCE}, 
     # alarm_actions = [topic], 
     ok_actions=[topic], 
     insufficient_data_actions=[topic]) 

# status_check_failed_alarm.add_alarm_action('arn:aws:sns:us-east-1:<acct#>:ec2:recover') 
# status_check_failed_alarm.add_alarm_action('arn:aws:sns:us-east-1:<acct#>:ec2:RecoverInstances') 
status_check_failed_alarm.add_alarm_action('ec2:RecoverInstances') 

cw_conn.put_metric_alarm(status_check_failed_alarm) 

任何建議將不勝感激。

謝謝。

--Mike

+1

在哪裏定義了「topic」。您可以查詢所有主題,例如'sns = connect_to_region(...); topics = sns.get_all_topics()'或者只需在AWS管理控制檯中查找SNS ARN。它應該看起來像'arn:aws:sns:'。 – AChampion

+0

嗨AChampion - 我已更新我的代碼以顯示主題 - 檢索部分。 –

回答

0

我認爲這個問題是這些警報操作沒有<acct>arn。該cli reference記錄了有效arn S:

有效值:阿爾恩:AWS:自動化:區域:EC2:停止| arn:aws:automate:region:ec2:terminate |阿爾恩:AWS:自動化:區域:EC2:恢復

我會覺得更容易從AWS拉指標,並創建從報警而不是試圖達到從地面構造它,例如(未經測試的代碼):

topics = sns_conn.get_all_topics() 
topic = topics[u'ListTopicsResponse']['ListTopicsResult']['Topics'][0]['TopicArn'] 

metric = cloudwatch_conn.list_metrics(dimensions={'InstanceId': _INSTANCE}, 
             metric_name="StatusCheckFailed")[0] 
alarm = metric.create_alarm(name=_INSTANCE_NAME + "RECOVERY-High-Status-Check-Failed-Any", 
          description='status check for {} {}'.format(_INSTANCE, _INSTANCE_NAME), 
          alarm_actions=[topic, 'arn:aws:automate:us-east-1:ec2:recover'], 
          ok_actions=[topic], 
          insufficient_data_actions=[topic], 
          statistic='Average', 
          comparison='>=', 
          threshold=1.0, 
          period=60, 
          evaluation_periods=5) 
+0

嗨AChampion, 非常感謝你....這當然更容易,它完美的作品。 –