AWS ARN (Amazon Resource Name) in PY
Match AWS ARNs (Amazon Resource Names) across commercial, China, and GovCloud partitions.
Try it in the PY tester →Pattern
regexPY
arn:(?:aws|aws-cn|aws-us-gov):[a-z0-9\-]+:[a-z0-9\-]*:\d{12}?:[\w\-\/.*:]+ (flags: g)Python (re) code
pyPython
import re
pattern = re.compile(r"arn:(?:aws|aws-cn|aws-us-gov):[a-z0-9\-]+:[a-z0-9\-]*:\d{12}?:[\w\-\/.*:]+")
input_text = "Bucket arn:aws:s3:::my-app-bucket/path"
for m in pattern.finditer(input_text):
print(m.group(0))Stdlib `re` module — no third-party dependency. Works on Python 3.6+.
How the pattern works
arn:(?:aws|aws-cn|aws-us-gov): matches the ARN prefix and partition. [a-z0-9\-]+ matches the service (s3, lambda, iam, etc.). [a-z0-9\-]* matches the optional region (some services like IAM omit it). \d{12}? matches the 12-digit account ID (or omitted for some services). [\w\-\/.*:]+ matches the resource portion which varies wildly per service.
Examples
Input
Bucket arn:aws:s3:::my-app-bucket/pathMatches
arn:aws:s3:::my-app-bucket/path
Input
Lambda arn:aws:lambda:us-east-1:123456789012:function:helloMatches
arn:aws:lambda:us-east-1:123456789012:function:hello
Input
no arns hereNo match
—