【发布时间】:2019-12-30 18:41:30
【问题描述】:
我正在为 AWS SNS 编写自定义配置规则,并希望遍历所有主题。我可以使用.list_topics() 获取所有主题,但无法遍历这些主题以获取每个主题的属性。
目前,我能够获取唯一第一个的 SNS 主题属性
topic_arn = response["Topics"][0]['TopicArn']
我想从列出的所有主题中获取 SNS 主题属性,可以想到 for 循环吗?如何for循环主题?
def get_all_topics(sns_client):
sns_client = boto3.client('sns')
response = sns_client.list_topics()
topics = [topic['TopicArn'] for topic in response['Topics']]
print("Topic List: %s" % topics)
return topics
'''
Fetch SNS Topic attributes
'''
def get_topic_attributes():
sns_client = boto3.client('sns')
response = sns_client.list_topics()
topic_arn = response["Topics"][0]['TopicArn']
response_topic_attribute_dict = sns_client.get_topic_attributes(TopicArn=topic_arn)
print("SNS Topic attributes: %s" % response_topic_attribute_dict)
return response_topic_attribute_dict
''' 测试 SNS For Loop 出现错误 '''
def evaluate_compliance():
sns_client = boto3.client('sns')
response = sns_client.list_topics()
topics = [topic['TopicArn'] for topic in response['Topics']]
for topic_dict in topics:
response = sns_client.list_topics()
response_topic_attributes_dict = sns_client.get_topic_attributes(TopicArn=topic_dict['TopicArn'])
【问题讨论】:
标签: python aws-lambda boto3 amazon-sns