diff --git a/lib/event_sources.json.example b/lib/event_sources.json.example index 6e774869..1b2d7936 100644 --- a/lib/event_sources.json.example +++ b/lib/event_sources.json.example @@ -9,7 +9,6 @@ ], "ScheduleEvents": [ { - "FunctionArnPrefix": "arn:aws:lambda:us-west-2:XXX:function:", "ScheduleName": "node-lambda-test-schedule", "ScheduleState": "ENABLED", "ScheduleExpression": "rate(1 hour)" diff --git a/lib/main.js b/lib/main.js index 46df3f8a..61fc25e5 100644 --- a/lib/main.js +++ b/lib/main.js @@ -9,6 +9,7 @@ var path = require('path'); var async = require('async'); var zip = new require('node-zip')(); var dotenv = require('dotenv'); +var ScheduleEvents = require('./schedule_events'); var maxBufferSize = 50 * 1024 * 1024; @@ -532,6 +533,21 @@ Lambda.prototype._updateEventSources = function (lambda, functionName, existingE }); }; +Lambda.prototype._updateScheduleEvents = function (scheduleEvents, functionArn, scheduleList, cb) { + return async.series(scheduleList.map(function(schedule) { + return function(_cb) { + const params = Object.assign(schedule, { FunctionArn: functionArn }); + scheduleEvents.add(params).then(function (data) { + _cb(null, params); + }).catch(function (err) { + _cb(err); + }); + }; + }), function(err, results) { + cb(err, results); + }); +}; + Lambda.prototype.package = function (program) { var _this = this; if (!program.packageDirectory) { @@ -609,6 +625,7 @@ Lambda.prototype.deploy = function (program) { var lambda = new aws.Lambda({ apiVersion: '2015-03-31' }); + var scheduleEvents = new ScheduleEvents(aws); // Checking function return lambda.getFunction({ @@ -623,9 +640,20 @@ Lambda.prototype.deploy = function (program) { console.log('=> Zip file(s) done uploading. Results follow: '); console.log(results); - // Updating event source(s) - _this._updateEventSources(lambda, params.FunctionName, [], eventSourceList.EventSourceMappings, function(err, results) { - cb(null, results); + async.parallel([ + function(_callback) { + // Updating event source(s) + _this._updateEventSources(lambda, params.FunctionName, [], eventSourceList.EventSourceMappings, function(err, results) { + _callback(null, results); + }); + }, + function(_callback) { + _this._updateScheduleEvents(scheduleEvents, results.FunctionArn, eventSourceList.ScheduleEvents, function(err, results) { + _callback(err, results); + }); + } + ], function(err, results) { + cb(err, results); }); }); } @@ -645,7 +673,9 @@ Lambda.prototype.deploy = function (program) { } console.log('=> Zip file(s) done uploading. Results follow: '); console.log(results); - _callback(err, results); + _this._updateScheduleEvents(scheduleEvents, results.FunctionArn, eventSourceList.ScheduleEvents, function(err, results) { + _callback(err, results); + }); }); }, function(_callback) { @@ -663,7 +693,7 @@ Lambda.prototype.deploy = function (program) { throw err; } console.log('=> All tasks done. Results follow: '); - console.log(results); + console.log(JSON.stringify(results, null, ' ')); }); }); }; diff --git a/lib/schedule_events.js b/lib/schedule_events.js index c24f8ce9..25e6be85 100644 --- a/lib/schedule_events.js +++ b/lib/schedule_events.js @@ -15,8 +15,8 @@ ScheduleEvents.prototype = { return `${params.ScheduleName} - ${params.ScheduleExpression}`; }, - _functionArn: (params) => { - return params.FunctionArnPrefix + params.FunctionName; + _functionName: (params) => { + return params.FunctionArn.split(':').pop(); }, _putRulePrams: function(params) { @@ -40,10 +40,10 @@ ScheduleEvents.prototype = { }); }, - _addPermissionParams: (params) => { + _addPermissionParams: function(params) { return { Action: 'lambda:InvokeFunction', - FunctionName: params.FunctionName, + FunctionName: this._functionName(params), Principal: 'events.amazonaws.com', SourceArn: params.RuleArn, StatementId: params.ScheduleName @@ -69,8 +69,8 @@ ScheduleEvents.prototype = { return { Rule: params.ScheduleName, Targets: [{ - Arn: this._functionArn(params), - Id: params.FunctionName + Arn: params.FunctionArn, + Id: this._functionName(params) }] }; }, diff --git a/test/main.js b/test/main.js index 1c8713cd..883bf23b 100644 --- a/test/main.js +++ b/test/main.js @@ -565,7 +565,6 @@ describe('node-lambda', function () { StartingPosition: 'LATEST', }], ScheduleEvents: [{ - FunctionArnPrefix: 'arn:aws:lambda:us-west-2:XXX:function:', ScheduleName: 'node-lambda-test-schedule', ScheduleState: 'ENABLED', ScheduleExpression: 'rate(1 hour)', @@ -601,6 +600,65 @@ describe('node-lambda', function () { }); }); + describe('_updateScheduleEvents', function () { + const aws = require('aws-sdk-mock'); + const ScheduleEvents = require('../lib/schedule_events'); + const eventSourcesJsonValue = { + ScheduleEvents: [{ + ScheduleName: 'node-lambda-test-schedule', + ScheduleState: 'ENABLED', + ScheduleExpression: 'rate(1 hour)', + }] + }; + + var schedule = null; + + before(function () { + aws.mock('CloudWatchEvents', 'putRule', function (params, callback) { + callback(null, {}); + }); + aws.mock('CloudWatchEvents', 'putTargets', function (params, callback) { + callback(null, {}); + }); + aws.mock('Lambda', 'addPermission', function (params, callback) { + callback(null, {}); + }); + + fs.writeFileSync( + 'event_sources.json', + JSON.stringify(eventSourcesJsonValue) + ); + + schedule = new ScheduleEvents(require('aws-sdk')); + }); + + after(function () { + fs.unlinkSync('event_sources.json'); + aws.restore('CloudWatchEvents'); + aws.restore('Lambda'); + }); + + it('simple test with mock', function () { + program.eventSourceFile = 'event_sources.json'; + const eventSourceList = lambda._eventSourceList(program); + const functionArn = 'arn:aws:lambda:us-west-2:XXX:function:node-lambda-test-function'; + return new Promise(function (resolve) { + lambda._updateScheduleEvents(schedule, functionArn, eventSourceList.ScheduleEvents, function(err, results) { + resolve({ err: err, results: results }); + }); + }).then(function (actual) { + const expected = { + err: undefined, + results: [Object.assign( + eventSourcesJsonValue.ScheduleEvents[0], + { FunctionArn: functionArn } + )] + }; + assert.deepEqual(actual, expected); + }); + }); + }); + describe('check env vars before create sample files', function () { const filesCreatedBySetup = [ '.env', diff --git a/test/schedule_events.js b/test/schedule_events.js index 001f9b1a..0a1b784a 100644 --- a/test/schedule_events.js +++ b/test/schedule_events.js @@ -7,8 +7,7 @@ aws.setSDK(path.resolve('node_modules/aws-sdk')); const ScheduleEvents = require(path.join('..', 'lib', 'schedule_events')); const params = { - FunctionName: 'node-lambda-test-function', - FunctionArnPrefix: 'arn:aws:lambda:us-west-2:XXX:function:', + FunctionArn: 'arn:aws:lambda:us-west-2:XXX:function:node-lambda-test-function', ScheduleName: 'node-lambda-test-schedule', ScheduleState: 'ENABLED', ScheduleExpression: 'rate(1 hour)' @@ -62,11 +61,11 @@ describe('schedule_events', () => { }); }); - describe('_functionArn', () => { + describe('_functionName', () => { it('correct value', () => { assert.equal( - schedule._functionArn(params), - 'arn:aws:lambda:us-west-2:XXX:function:node-lambda-test-function' + schedule._functionName(params), + 'node-lambda-test-function' ); }); });