Skip to content Skip to sidebar Skip to footer

Boto - Aws Sns How To Extract Topic's Arn Number

When creating a AWS SNS topic: a = conn.create_topic(topicname) or getting the topic already created: a = conn.get_all_topics() the result is: {u'CreateTopicResponse': {u'Respons

Solution 1:

import boto

defget_account_id():
    # suggested by https://groups.google.com/forum/#!topic/boto-users/QhASXlNBm40return boto.connect_iam().get_user().arn.split(':')[4]

deftopic_arn_from_name(self, region, name):
    return":".join(["arn", "aws", "sns", region, get_account_id(), name])

Solution 2:

When you create a new topic, boto returns a Python dictionary with the data you describe above. To get the topic ARN as a string, simply reference that key in the dictionary like this:

a = conn.create_topic(topicname)
a_arn = a['CreateTopicResponse']['CreateTopicResult']['TopicArn']

it's kind of clunky but it works.

The list_topics call returns a different structure, basically like this:

{u'ListTopicsResponse':
  {u'ListTopicsResult':
    {u'NextToken': None,
     u'Topics': [
      {u'TopicArn': u'arn:aws:sns:us-east-1:467741034465:exampletopic'},
      {u'TopicArn': u'arn:aws:sns:us-east-1:467741034465:footopic'}
     ]
    },
  u'ResponseMetadata': {u'RequestId': u'aef821f6-d595-55e1-af14-6d3a8064536a'}}}

In this case, if you wanted to get the ARN of the first topic you would use:

a = conn.list_topics()
a_arn = a['ListTopicsResponse']['ListTopicsResult']['Topics'][0]['TopicArn']

Post a Comment for "Boto - Aws Sns How To Extract Topic's Arn Number"