Last active
October 16, 2024 18:30
-
-
Save gene1wood/5299969edc4ef21d8efcfea52158dd40 to your computer and use it in GitHub Desktop.
Parse an AWS ARN (Amazon Resource Name) into it's constituent elements
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
Fixed version to handle resource types and resources correctly.