AttributeError:'ResultSet'对象没有属性'create_alarm'

时间:2015-05-12 15:36:24

标签: python amazon-web-services boto amazon-sns

我有一个名为“topic”的主题,我想用cloudwatch监控。

使用cloudwatch,我希望当这个主题“主题”具有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])

1 个答案:

答案 0 :(得分:1)

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

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