Boto / Cloudwatch恢复实例警报

时间:2016-12-06 15:28:52

标签: python amazon-ec2 boto cloudwatch alarms

我一直在撞墙试图做这项工作。 我试图使用python / boto创建一个cloutwatch警报来恢复失败的ec2实例。 我很难让ec2:RecoverInstance行动起作用。我怀疑我的主题没有正确设置。

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)

任何建议都将受到高度赞赏。

谢谢。

- 麦克

1 个答案:

答案 0 :(得分:0)

我认为问题是这些警报操作在<acct>中没有arncli reference记录了有效arn s:

  

有效值:arn:aws:automate: region :ec2:stop | arn:aws:automate: region :ec2:terminate | arn:aws:automate: region :ec2:recover

我认为从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)