Lambda:如何迭代/循环 SNS 主题以获取所有主题属性?
Lambda: How to iterate /loop through the SNS topics to get all Topic Attributes?
我正在为 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'])
这是我的测试代码。 boto3 会话的定义可能不同,但您可以忽略。
import boto3
session = boto3.session.Session(region_name='ap-northeast-2')
client = session.client('sns')
response = client.list_topics()
for item in response['Topics']:
print(item['TopicArn'])
或
import boto3
session = boto3.session.Session(region_name='ap-northeast-2')
sns = session.resource('sns')
for topic in sns.topics.all():
print(topic.arn)
结果是:
arn:aws:sns:ap-northeast-2:1...3:2...
arn:aws:sns:ap-northeast-2:1...3:A...
arn:aws:sns:ap-northeast-2:1...3:A...
arn:aws:sns:ap-northeast-2:1...3:A...
arn:aws:sns:ap-northeast-2:1...3:A...
arn:aws:sns:ap-northeast-2:1...3:A...
arn:aws:sns:ap-northeast-2:1...3:A...
arn:aws:sns:ap-northeast-2:1...3:B...
arn:aws:sns:ap-northeast-2:1...3:B...
arn:aws:sns:ap-northeast-2:1...3:B...
arn:aws:sns:ap-northeast-2:1...3:B...
继续此代码,您可以获得所有主题的属性,例如:
import boto3
session = boto3.session.Session(region_name='ap-northeast-2')
client = session.client('sns')
response = client.list_topics()
for item in response['Topics']:
print(item['TopicArn'])
print(client.get_topic_attributes(TopicArn=item['TopicArn']))
我正在为 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'])
这是我的测试代码。 boto3 会话的定义可能不同,但您可以忽略。
import boto3
session = boto3.session.Session(region_name='ap-northeast-2')
client = session.client('sns')
response = client.list_topics()
for item in response['Topics']:
print(item['TopicArn'])
或
import boto3
session = boto3.session.Session(region_name='ap-northeast-2')
sns = session.resource('sns')
for topic in sns.topics.all():
print(topic.arn)
结果是:
arn:aws:sns:ap-northeast-2:1...3:2...
arn:aws:sns:ap-northeast-2:1...3:A...
arn:aws:sns:ap-northeast-2:1...3:A...
arn:aws:sns:ap-northeast-2:1...3:A...
arn:aws:sns:ap-northeast-2:1...3:A...
arn:aws:sns:ap-northeast-2:1...3:A...
arn:aws:sns:ap-northeast-2:1...3:A...
arn:aws:sns:ap-northeast-2:1...3:B...
arn:aws:sns:ap-northeast-2:1...3:B...
arn:aws:sns:ap-northeast-2:1...3:B...
arn:aws:sns:ap-northeast-2:1...3:B...
继续此代码,您可以获得所有主题的属性,例如:
import boto3
session = boto3.session.Session(region_name='ap-northeast-2')
client = session.client('sns')
response = client.list_topics()
for item in response['Topics']:
print(item['TopicArn'])
print(client.get_topic_attributes(TopicArn=item['TopicArn']))