AttributeError: 'ResultSet' object has no attribute 'create_alarm'

AttributeError: 'ResultSet' object has no attribute 'create_alarm'

我有一个名为 "topic" 的主题,我想使用 cloudwatch 进行监控。

并且使用 cloudwatch,我希望当这个主题 "topic" 有 NumberOfMessagesPublished > 100 我想向名称为 "cloudwatchSNS" 的其他主题发送通知。

我正在尝试使用下面的代码来执行此操作,但出现此错误:

AttributeError: 'ResultSet' object has no attribute 'create_alarm'

你能帮我让它正常工作吗?

import boto.sns
from boto.ec2.cloudwatch import MetricAlarm
import boto.ec2.cloudwatch

sns = boto.sns.connect_to_region("us-east-1")
cloudwatch = boto.ec2.cloudwatch.connect_to_region("us-east-1")

#topic to send notification when NumberOfMessagesPublished > 100
topicToSendNotification = sns.create_topic("cloudwatchSNS")

topicArnToSendNotification = topicarn['CreateTopicResponse']['CreateTopicResult']['TopicArn']

#topicName to control the number of messages published
topicNameToMonitor = "topic"

#subscriptor in topic to receive an email when NumberOfMessagesPublished > 100
subscription = sns.subscribe(topicArnToSendNotification, "email", "mail@mail.com")

metric = cloudwatch.list_metrics(dimensions={'TopicName':topicNameToMonitor},
                         metric_name="NumberOfMessagesPublished")

alarmName = "test"

metric.create_alarm(name=alarmName, comparison='>=', threshold=100, period=300,
                    evaluation_periods=2, statistic='Average', alarm_actions=[topicarnToSendNotification])

调用 cloud watch.list_metrics(...) 将始终 return 一个名为 ResultSet 的类似列表的对象,即使只有 1 个结果。在尝试对其创建警报之前,您需要从列表中取出实际的 Metric 对象。

metric = cloudwatch.list_metrics(dimensions={'TopicName':topicNameToMonitor},
                     metric_name="NumberOfMessagesPublished")[0]