Skip to content

Commit 66a7059

Browse files
abetomoDeviaVir
authored andcommitted
Fix to add ScheduleEvents to main.js (#228)
* Fix to use authenticated `aws` object in main.js * Fix to enable ScheduleEvents to be specified in event_sources.json * Supports new event_sources.json format * modify default value to new format * Added import of ScheduleEvents * Add _updateScheduleEvents function * Add execution process of _updateScheduleEvents * Improve log output Improved to be displayed as `[[[null], [[Object], [Object]]]]` * Fix parameters to `FunctionArn` * Remove unnecessary key * Fix as argument changed from functionName to functionArn * Remove unnecessary comma
1 parent e527962 commit 66a7059

File tree

5 files changed

+104
-18
lines changed

5 files changed

+104
-18
lines changed

lib/event_sources.json.example

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
],
1010
"ScheduleEvents": [
1111
{
12-
"FunctionArnPrefix": "arn:aws:lambda:us-west-2:XXX:function:",
1312
"ScheduleName": "node-lambda-test-schedule",
1413
"ScheduleState": "ENABLED",
1514
"ScheduleExpression": "rate(1 hour)"

lib/main.js

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ var path = require('path');
99
var async = require('async');
1010
var zip = new require('node-zip')();
1111
var dotenv = require('dotenv');
12+
var ScheduleEvents = require('./schedule_events');
1213

1314
var maxBufferSize = 50 * 1024 * 1024;
1415

@@ -532,6 +533,21 @@ Lambda.prototype._updateEventSources = function (lambda, functionName, existingE
532533
});
533534
};
534535

536+
Lambda.prototype._updateScheduleEvents = function (scheduleEvents, functionArn, scheduleList, cb) {
537+
return async.series(scheduleList.map(function(schedule) {
538+
return function(_cb) {
539+
const params = Object.assign(schedule, { FunctionArn: functionArn });
540+
scheduleEvents.add(params).then(function (data) {
541+
_cb(null, params);
542+
}).catch(function (err) {
543+
_cb(err);
544+
});
545+
};
546+
}), function(err, results) {
547+
cb(err, results);
548+
});
549+
};
550+
535551
Lambda.prototype.package = function (program) {
536552
var _this = this;
537553
if (!program.packageDirectory) {
@@ -609,6 +625,7 @@ Lambda.prototype.deploy = function (program) {
609625
var lambda = new aws.Lambda({
610626
apiVersion: '2015-03-31'
611627
});
628+
var scheduleEvents = new ScheduleEvents(aws);
612629

613630
// Checking function
614631
return lambda.getFunction({
@@ -623,9 +640,20 @@ Lambda.prototype.deploy = function (program) {
623640
console.log('=> Zip file(s) done uploading. Results follow: ');
624641
console.log(results);
625642

626-
// Updating event source(s)
627-
_this._updateEventSources(lambda, params.FunctionName, [], eventSourceList.EventSourceMappings, function(err, results) {
628-
cb(null, results);
643+
async.parallel([
644+
function(_callback) {
645+
// Updating event source(s)
646+
_this._updateEventSources(lambda, params.FunctionName, [], eventSourceList.EventSourceMappings, function(err, results) {
647+
_callback(null, results);
648+
});
649+
},
650+
function(_callback) {
651+
_this._updateScheduleEvents(scheduleEvents, results.FunctionArn, eventSourceList.ScheduleEvents, function(err, results) {
652+
_callback(err, results);
653+
});
654+
}
655+
], function(err, results) {
656+
cb(err, results);
629657
});
630658
});
631659
}
@@ -645,7 +673,9 @@ Lambda.prototype.deploy = function (program) {
645673
}
646674
console.log('=> Zip file(s) done uploading. Results follow: ');
647675
console.log(results);
648-
_callback(err, results);
676+
_this._updateScheduleEvents(scheduleEvents, results.FunctionArn, eventSourceList.ScheduleEvents, function(err, results) {
677+
_callback(err, results);
678+
});
649679
});
650680
},
651681
function(_callback) {
@@ -663,7 +693,7 @@ Lambda.prototype.deploy = function (program) {
663693
throw err;
664694
}
665695
console.log('=> All tasks done. Results follow: ');
666-
console.log(results);
696+
console.log(JSON.stringify(results, null, ' '));
667697
});
668698
});
669699
};

lib/schedule_events.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ ScheduleEvents.prototype = {
1515
return `${params.ScheduleName} - ${params.ScheduleExpression}`;
1616
},
1717

18-
_functionArn: (params) => {
19-
return params.FunctionArnPrefix + params.FunctionName;
18+
_functionName: (params) => {
19+
return params.FunctionArn.split(':').pop();
2020
},
2121

2222
_putRulePrams: function(params) {
@@ -40,10 +40,10 @@ ScheduleEvents.prototype = {
4040
});
4141
},
4242

43-
_addPermissionParams: (params) => {
43+
_addPermissionParams: function(params) {
4444
return {
4545
Action: 'lambda:InvokeFunction',
46-
FunctionName: params.FunctionName,
46+
FunctionName: this._functionName(params),
4747
Principal: 'events.amazonaws.com',
4848
SourceArn: params.RuleArn,
4949
StatementId: params.ScheduleName
@@ -69,8 +69,8 @@ ScheduleEvents.prototype = {
6969
return {
7070
Rule: params.ScheduleName,
7171
Targets: [{
72-
Arn: this._functionArn(params),
73-
Id: params.FunctionName
72+
Arn: params.FunctionArn,
73+
Id: this._functionName(params)
7474
}]
7575
};
7676
},

test/main.js

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -565,7 +565,6 @@ describe('node-lambda', function () {
565565
StartingPosition: 'LATEST',
566566
}],
567567
ScheduleEvents: [{
568-
FunctionArnPrefix: 'arn:aws:lambda:us-west-2:XXX:function:',
569568
ScheduleName: 'node-lambda-test-schedule',
570569
ScheduleState: 'ENABLED',
571570
ScheduleExpression: 'rate(1 hour)',
@@ -601,6 +600,65 @@ describe('node-lambda', function () {
601600
});
602601
});
603602

603+
describe('_updateScheduleEvents', function () {
604+
const aws = require('aws-sdk-mock');
605+
const ScheduleEvents = require('../lib/schedule_events');
606+
const eventSourcesJsonValue = {
607+
ScheduleEvents: [{
608+
ScheduleName: 'node-lambda-test-schedule',
609+
ScheduleState: 'ENABLED',
610+
ScheduleExpression: 'rate(1 hour)',
611+
}]
612+
};
613+
614+
var schedule = null;
615+
616+
before(function () {
617+
aws.mock('CloudWatchEvents', 'putRule', function (params, callback) {
618+
callback(null, {});
619+
});
620+
aws.mock('CloudWatchEvents', 'putTargets', function (params, callback) {
621+
callback(null, {});
622+
});
623+
aws.mock('Lambda', 'addPermission', function (params, callback) {
624+
callback(null, {});
625+
});
626+
627+
fs.writeFileSync(
628+
'event_sources.json',
629+
JSON.stringify(eventSourcesJsonValue)
630+
);
631+
632+
schedule = new ScheduleEvents(require('aws-sdk'));
633+
});
634+
635+
after(function () {
636+
fs.unlinkSync('event_sources.json');
637+
aws.restore('CloudWatchEvents');
638+
aws.restore('Lambda');
639+
});
640+
641+
it('simple test with mock', function () {
642+
program.eventSourceFile = 'event_sources.json';
643+
const eventSourceList = lambda._eventSourceList(program);
644+
const functionArn = 'arn:aws:lambda:us-west-2:XXX:function:node-lambda-test-function';
645+
return new Promise(function (resolve) {
646+
lambda._updateScheduleEvents(schedule, functionArn, eventSourceList.ScheduleEvents, function(err, results) {
647+
resolve({ err: err, results: results });
648+
});
649+
}).then(function (actual) {
650+
const expected = {
651+
err: undefined,
652+
results: [Object.assign(
653+
eventSourcesJsonValue.ScheduleEvents[0],
654+
{ FunctionArn: functionArn }
655+
)]
656+
};
657+
assert.deepEqual(actual, expected);
658+
});
659+
});
660+
});
661+
604662
describe('check env vars before create sample files', function () {
605663
const filesCreatedBySetup = [
606664
'.env',

test/schedule_events.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ aws.setSDK(path.resolve('node_modules/aws-sdk'));
77
const ScheduleEvents = require(path.join('..', 'lib', 'schedule_events'));
88

99
const params = {
10-
FunctionName: 'node-lambda-test-function',
11-
FunctionArnPrefix: 'arn:aws:lambda:us-west-2:XXX:function:',
10+
FunctionArn: 'arn:aws:lambda:us-west-2:XXX:function:node-lambda-test-function',
1211
ScheduleName: 'node-lambda-test-schedule',
1312
ScheduleState: 'ENABLED',
1413
ScheduleExpression: 'rate(1 hour)'
@@ -62,11 +61,11 @@ describe('schedule_events', () => {
6261
});
6362
});
6463

65-
describe('_functionArn', () => {
64+
describe('_functionName', () => {
6665
it('correct value', () => {
6766
assert.equal(
68-
schedule._functionArn(params),
69-
'arn:aws:lambda:us-west-2:XXX:function:node-lambda-test-function'
67+
schedule._functionName(params),
68+
'node-lambda-test-function'
7069
);
7170
});
7271
});

0 commit comments

Comments
 (0)