【问题标题】:Lambda: How to iterate /loop through the SNS topics to get all Topic Attributes?Lambda:如何遍历/循环 SNS 主题以获取所有主题属性?
【发布时间】: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


    【解决方案1】:

    这是我的测试代码。 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']))
    

    【讨论】:

    • 谢谢,但您上面的代码只打印所有 TopicArn 的。您如何从所有主题中获取主题属性?在我的代码中,我只能获取一个 topic 的属性。 topic_arn = response["Topics"][0]['TopicArn'] response_topic_attribute_dict = sns_client.get_topic_attributes(TopicArn=topic_arn)
    猜你喜欢
    • 2017-04-30
    • 1970-01-01
    • 2018-05-18
    • 1970-01-01
    • 1970-01-01
    • 2015-07-27
    • 2012-06-07
    • 2018-06-06
    • 2018-02-27
    相关资源
    最近更新 更多