|
| 1 | +arnparse |
| 2 | +======== |
| 3 | + |
| 4 | +Parse ARNs using Python |
| 5 | + |
| 6 | +[](https://travis-ci.org/PokaInc/arnparse) |
| 7 | + |
| 8 | +Motivation |
| 9 | +---------- |
| 10 | + |
| 11 | +Sometimes, you want to parse an Amazon Resource Name (ARN) into its |
| 12 | +components to get some useful information from the ARN (e.g. AWS region, |
| 13 | +account ID, etc). |
| 14 | + |
| 15 | +You can find documentation on ARNs and their components here: |
| 16 | +https://docs.aws.amazon.com/general/latest/gr/aws-arns-and-namespaces.html |
| 17 | + |
| 18 | +Installation |
| 19 | +------------ |
| 20 | + |
| 21 | +`pip install arnparse` |
| 22 | + |
| 23 | +Examples |
| 24 | +-------- |
| 25 | + |
| 26 | +### S3 Object |
| 27 | + |
| 28 | +```python |
| 29 | +from arnparse import arnparse |
| 30 | + |
| 31 | +arn = arnparse('arn:aws:s3:::my_corporate_bucket/exampleobject.png') |
| 32 | + |
| 33 | +assert arn.partition == 'aws' |
| 34 | +assert arn.service == 's3' |
| 35 | +assert arn.region is None |
| 36 | +assert arn.account_id is None |
| 37 | +assert arn.resource_type is None |
| 38 | +assert arn.resource == 'my_corporate_bucket/exampleobject.png' |
| 39 | +``` |
| 40 | + |
| 41 | +### VPC |
| 42 | + |
| 43 | +```python |
| 44 | +from arnparse import arnparse |
| 45 | + |
| 46 | +arn = arnparse('arn:aws:ec2:us-east-1:123456789012:vpc/vpc-fd580e98') |
| 47 | + |
| 48 | +assert arn.partition == 'aws' |
| 49 | +assert arn.service == 'ec2' |
| 50 | +assert arn.region == 'us-east-1' |
| 51 | +assert arn.account_id == '123456789012' |
| 52 | +assert arn.resource_type == 'vpc' |
| 53 | +assert arn.resource == 'vpc-fd580e98' |
| 54 | +``` |
| 55 | + |
| 56 | +### CloudWatch Alarm |
| 57 | + |
| 58 | +```python |
| 59 | +from arnparse import arnparse |
| 60 | + |
| 61 | +arn = arnparse('arn:aws:cloudwatch:us-east-1:123456789012:alarm:MyAlarmName') |
| 62 | + |
| 63 | +assert arn.partition == 'aws' |
| 64 | +assert arn.service == 'cloudwatch' |
| 65 | +assert arn.region == 'us-east-1' |
| 66 | +assert arn.account_id == '123456789012' |
| 67 | +assert arn.resource_type == 'alarm' |
| 68 | +assert arn.resource == 'MyAlarmName' |
| 69 | +``` |
| 70 | + |
| 71 | +### SNS Topic |
| 72 | + |
| 73 | +```python |
| 74 | +from arnparse import arnparse |
| 75 | + |
| 76 | +arn = arnparse('arn:aws:sns:*:123456789012:my_corporate_topic') |
| 77 | + |
| 78 | +assert arn.partition == 'aws' |
| 79 | +assert arn.service == 'sns' |
| 80 | +assert arn.region == '*' |
| 81 | +assert arn.account_id == '123456789012' |
| 82 | +assert arn.resource_type is None |
| 83 | +assert arn.resource == 'my_corporate_topic' |
| 84 | +``` |
| 85 | + |
| 86 | +### API Gateway |
| 87 | + |
| 88 | +```python |
| 89 | +from arnparse import arnparse |
| 90 | + |
| 91 | +arn = arnparse('arn:aws:apigateway:us-east-1::a123456789012bc3de45678901f23a45:/test/mydemoresource/*') |
| 92 | + |
| 93 | +assert arn.partition == 'aws' |
| 94 | +assert arn.service == 'apigateway' |
| 95 | +assert arn.region == 'us-east-1' |
| 96 | +assert arn.account_id is None |
| 97 | +assert arn.resource_type is None |
| 98 | +assert arn.resource == 'a123456789012bc3de45678901f23a45:/test/mydemoresource/*' |
| 99 | +``` |
0 commit comments