AWS ARN (Amazon Resource Name) in GO
Match AWS ARNs (Amazon Resource Names) across commercial, China, and GovCloud partitions.
Try it in the GO tester →Pattern
regexGO
arn:(?:aws|aws-cn|aws-us-gov):[a-z0-9\-]+:[a-z0-9\-]*:\d{12}?:[\w\-\/.*:]+ (flags: g)Go (RE2) code
goGo
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`arn:(?:aws|aws-cn|aws-us-gov):[a-z0-9\-]+:[a-z0-9\-]*:\d{12}?:[\w\-\/.*:]+`)
input := `Bucket arn:aws:s3:::my-app-bucket/path`
for _, match := range re.FindAllString(input, -1) {
fmt.Println(match)
}
}Uses `regexp.MustCompile` (panics on bad patterns at startup) and `FindAllString` for all matches.
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
—