Skip to content

Instantly share code, notes, and snippets.

@spuds51
Forked from gene1wood/parse_arn.py
Created June 28, 2020 23:32
Show Gist options
  • Save spuds51/26f4a714a2211c9f490bfe8cdbc75111 to your computer and use it in GitHub Desktop.
Save spuds51/26f4a714a2211c9f490bfe8cdbc75111 to your computer and use it in GitHub Desktop.
Parse an AWS ARN (Amazon Resource Name) into it's constituent elements
def parse_arn(arn):
# http://docs.aws.amazon.com/general/latest/gr/aws-arns-and-namespaces.html
elements = arn.split(':')
result = {'arn': elements[0],
'partition': elements[1],
'service': elements[2],
'region': elements[3],
'account': elements[4]
}
if len(elements) == 7:
result['resourcetype'], result['resource'] = elements[5:]
elif '/' not in elements[5]:
result['resource'] = elements[5]
result['resourcetype'] = None
else:
result['resourcetype'], result['resource'] = elements[5].split('/')
return result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment