From 8c37cb7d5a2f0a1401ea44075dd0570018244035 Mon Sep 17 00:00:00 2001 From: James Wing Date: Tue, 21 Jul 2015 10:54:07 -0700 Subject: [PATCH] Parsing AWS region from ARN, when ARN provided --- tasks/lambda_deploy.js | 6 ++++- test/arn_parser_test.js | 53 +++++++++++++++++++++++++++++++++++++++++ utils/arn_parser.js | 31 ++++++++++++++++++++++++ 3 files changed, 89 insertions(+), 1 deletion(-) create mode 100644 test/arn_parser_test.js create mode 100644 utils/arn_parser.js diff --git a/tasks/lambda_deploy.js b/tasks/lambda_deploy.js index e8397a0..316a322 100644 --- a/tasks/lambda_deploy.js +++ b/tasks/lambda_deploy.js @@ -13,7 +13,7 @@ module.exports = function (grunt) { var path = require('path'); var fs = require('fs'); var AWS = require('aws-sdk'); - + var arnParser = require('../utils/arn_parser'); // Please see the Grunt documentation for more information regarding task // creation: http://gruntjs.com/creating-tasks @@ -44,6 +44,10 @@ module.exports = function (grunt) { if(deploy_arn !== null) { deploy_function = deploy_arn; + var functionInfo = arnParser.parse(deploy_arn); + if (functionInfo && functionInfo.region) { + options.region = functionInfo.region; + } } var done = this.async(); diff --git a/test/arn_parser_test.js b/test/arn_parser_test.js new file mode 100644 index 0000000..86a286a --- /dev/null +++ b/test/arn_parser_test.js @@ -0,0 +1,53 @@ +'use strict'; + +var arnParser = require('../utils/arn_parser'); + +exports.parseFullArn = function(test){ + var arn = 'arn:aws:lambda:us-west-2:123456789012:function:MyFunctionName'; + var functionInfo = arnParser.parse(arn); + test.ok(functionInfo, 'parser should return function info'); + test.equal(functionInfo.region, 'us-west-2'); + test.equal(functionInfo.accountId, '123456789012'); + test.equal(functionInfo.functionName, 'MyFunctionName'); + test.done(); +}; + +exports.parsePartialArn = function(test){ + var arn = '123456789012:MyFunctionName'; + var functionInfo = arnParser.parse(arn); + test.ok(functionInfo, 'parser should return function info'); + test.equal(functionInfo.region, undefined); + test.equal(functionInfo.accountId, '123456789012'); + test.equal(functionInfo.functionName, 'MyFunctionName'); + test.done(); +}; + +exports.parseFunctionName = function(test){ + var arn = 'MyFunctionName'; + var functionInfo = arnParser.parse(arn); + test.ok(functionInfo, 'parser should return function info'); + test.equal(functionInfo.region, undefined); + test.equal(functionInfo.accountId, undefined); + test.equal(functionInfo.functionName, 'MyFunctionName'); + test.done(); +}; + +exports.parseEmptyArn = function(test){ + var arn = ''; + var functionInfo = arnParser.parse(arn); + test.equal(functionInfo, undefined); + test.done(); +}; + +exports.parseUndefinedArn = function(test){ + var functionInfo = arnParser.parse(undefined); + test.equal(functionInfo, undefined); + test.done(); +}; + +exports.parseBadArn = function(test){ + var arn = ':#!!'; + var functionInfo = arnParser.parse(arn); + test.equal(functionInfo, undefined); + test.done(); +}; \ No newline at end of file diff --git a/utils/arn_parser.js b/utils/arn_parser.js new file mode 100644 index 0000000..cc1c4fc --- /dev/null +++ b/utils/arn_parser.js @@ -0,0 +1,31 @@ +var arnParser = {}; + +/** + * Parses Lambda ARNs to identify region and other components + * See CreateFunction in the AWS Lambda API Reference for the ARN formats and regex + * http://docs.aws.amazon.com/lambda/latest/dg/API_CreateFunction.html + * + * Acceptable ARN-like formats for Lambda include: + * - Function name only "Thumbnail" + * - Partial ARN "123456789012:Thumbnail" + * - Full ARN "arn:aws:lambda:us-west-2:123456789012:function:ThumbNail" + * + * @param {string} arn - An ARN-like string specifying the Lambda function. + */ +arnParser.parse = function (arn) { + if (!arn) { + return; + } + var match = arn.match(/(arn:aws:lambda:)?([a-z]{2}-[a-z]+-\d{1}:)?(\d{12}:)?(function:)?([a-zA-Z0-9-_]+)/); + if (!match) { + return; + } + var functionInfo = { + "region": match[2] ? match[2].replace(":", "") : undefined, + "accountId": match[3] ? match[3].replace(":", "") : undefined, + "functionName": match[5] + }; + return functionInfo; +}; + +module.exports = arnParser; \ No newline at end of file