Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Parses an Amazon Resource Name (ARN) and returns its component parts as an object.
/**
* @hint Parses an Amazon Resource Name (ARN) and returns its component parts as an object.
* This follows the general format of ARNs outlined by Amazon (http://docs.aws.amazon.com/general/latest/gr/aws-arns-and-namespaces.html), but does not fully account for all possible formats
* Derived from https://gist.github.com/gene1wood/5299969edc4ef21d8efcfea52158dd40
*/
public struct function parseArn( required string arn ) {
var elements = arn.listToArray( ':', true );
var result = {
'original' : arn,
'arn' : elements[1],
'partition' : elements[2],
'service' : elements[3],
'region' : elements[4],
'account' : elements[5]
};
if ( elements.len() >= 7 ) {
result[ 'resourcetype' ] = elements[6];
result[ 'resource' ] = elements[7];
} else if ( !elements[6].find( '/' ) ) {
result[ 'resource' ] = elements[6];
result[ 'resourcetype' ] = '';
} else {
result[ 'resourcetype' ] = elements[6].listFirst( '/' );
result[ 'resource' ] = elements[6].listRest( '/' );
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment