【问题标题】:Boto / Cloudwatch recover instance alarmBoto / Cloudwatch 恢复实例报警
【发布时间】:2017-04-21 07:21:27
【问题描述】:

我一直在用头撞墙,试图完成这项工作。 我正在尝试使用 python/boto 创建一个恢复失败的 ec2 实例的 cloutwatch 警报。 我很难让 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)

任何建议将不胜感激。

谢谢。

--麦克

【问题讨论】:

  • topic 的定义在哪里。您可以查询所有主题,例如sns = connect_to_region(...); topics = sns.get_all_topics() 或仅在 AWS 管理控制台中查找 SNS ARN。它应该看起来像 arn:aws:sns:&lt;region&gt;:&lt;account&gt;:&lt;name&gt;
  • 嗨 AChampion -- 我已经更新了代码以显示主题检索部分。

标签: python amazon-ec2 boto cloudwatch alarms


【解决方案1】:

我认为问题在于这些警报操作在arn 中没有&lt;acct&gt;cli reference 记录了有效的 arns:

有效值:arn:aws:automate:region:ec2:stop | arn:aws:automate:区域:ec2:terminate | arn:aws:automate:区域: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)

【讨论】:

  • 您好 AChampion,非常感谢您....这当然容易得多,而且效果很好。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-02-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-06
  • 1970-01-01
相关资源
最近更新 更多