Skip to content

Instantly share code, notes, and snippets.

@mjclemente
Last active January 11, 2018 22:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mjclemente/37d8ccd25b3ae5f0a31f9b209b0a8a74 to your computer and use it in GitHub Desktop.
Save mjclemente/37d8ccd25b3ae5f0a31f9b209b0a8a74 to your computer and use it in GitHub Desktop.
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