From 1d41f9f50bd8ff8592ba9c62ad364bddb8364e42 Mon Sep 17 00:00:00 2001 From: Enrique Canals Date: Mon, 1 May 2017 15:51:03 -0700 Subject: [PATCH 1/4] Add ability to define a custom scheduled event description --- lib/schedule_events.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/schedule_events.js b/lib/schedule_events.js index 25e6be85..cf2d7707 100644 --- a/lib/schedule_events.js +++ b/lib/schedule_events.js @@ -12,7 +12,11 @@ const ScheduleEvents = function(aws) { ScheduleEvents.prototype = { _ruleDescription: (params) => { - return `${params.ScheduleName} - ${params.ScheduleExpression}`; + if (params.ScheduleDescription != null) { + return `${params.ScheduleDescription}`; + } else { + return `${params.ScheduleName} - ${params.ScheduleExpression}`; + } }, _functionName: (params) => { From f9066161c2d93744e069633c9b4b90a69066986d Mon Sep 17 00:00:00 2001 From: Enrique Canals Date: Mon, 1 May 2017 16:39:45 -0700 Subject: [PATCH 2/4] Add test for custom ScheduleEvents rule description Update example in sample event_sources.json --- lib/event_sources.json.example | 3 ++- test/main.js | 2 ++ test/schedule_events.js | 22 ++++++++++++++++++++-- 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/lib/event_sources.json.example b/lib/event_sources.json.example index 1b2d7936..2d3bfbf4 100644 --- a/lib/event_sources.json.example +++ b/lib/event_sources.json.example @@ -11,7 +11,8 @@ { "ScheduleName": "node-lambda-test-schedule", "ScheduleState": "ENABLED", - "ScheduleExpression": "rate(1 hour)" + "ScheduleExpression": "rate(1 hour)", + "ScheduleDescription": "Run node-lambda-test-function once per hour" } ] } diff --git a/test/main.js b/test/main.js index ff9b2014..1b31691a 100644 --- a/test/main.js +++ b/test/main.js @@ -648,6 +648,7 @@ describe('node-lambda', function () { ScheduleName: 'node-lambda-test-schedule', ScheduleState: 'ENABLED', ScheduleExpression: 'rate(1 hour)', + ScheduleDescription: 'Run node-lambda-test-function once per hour' }], }; assert.deepEqual(lambda._eventSourceList(program), expected); @@ -691,6 +692,7 @@ describe('node-lambda', function () { ScheduleName: 'node-lambda-test-schedule', ScheduleState: 'ENABLED', ScheduleExpression: 'rate(1 hour)', + ScheduleDescription: 'Run node-lambda-test-function once per hour' }] }; diff --git a/test/schedule_events.js b/test/schedule_events.js index 0a1b784a..1f5d2e84 100644 --- a/test/schedule_events.js +++ b/test/schedule_events.js @@ -10,7 +10,8 @@ const params = { FunctionArn: 'arn:aws:lambda:us-west-2:XXX:function:node-lambda-test-function', ScheduleName: 'node-lambda-test-schedule', ScheduleState: 'ENABLED', - ScheduleExpression: 'rate(1 hour)' + ScheduleExpression: 'rate(1 hour)', + ScheduleDescription: null }; const mockResponse = { @@ -52,7 +53,7 @@ describe('schedule_events', () => { schedule = new ScheduleEvents(require('aws-sdk')); }); - describe('_ruleDescription', () => { + describe('_ruleDescription (default)', () => { it('correct value', () => { assert.equal( schedule._ruleDescription(params), @@ -61,6 +62,23 @@ describe('schedule_events', () => { }); }); + describe('_ruleDescription (custom)', () => { + before(() => { + params.ScheduleDescription = 'Run node-lambda-test-function once per hour'; + }); + + after(() => { + params.ScheduleDescription = null; + }); + + it('correct value', () => { + assert.equal( + schedule._ruleDescription(params), + 'Run node-lambda-test-function once per hour' + ); + }); + }); + describe('_functionName', () => { it('correct value', () => { assert.equal( From 5cb1ac66a3fcd2d43c60672e5e9097ac70753236 Mon Sep 17 00:00:00 2001 From: Enrique Canals Date: Wed, 3 May 2017 12:40:41 -0700 Subject: [PATCH 3/4] Check if ScheduleDescription is in params and not null --- lib/schedule_events.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/schedule_events.js b/lib/schedule_events.js index cf2d7707..a204a04e 100644 --- a/lib/schedule_events.js +++ b/lib/schedule_events.js @@ -12,7 +12,7 @@ const ScheduleEvents = function(aws) { ScheduleEvents.prototype = { _ruleDescription: (params) => { - if (params.ScheduleDescription != null) { + if ('ScheduleDescription' in params && params.ScheduleDescription != null) { return `${params.ScheduleDescription}`; } else { return `${params.ScheduleName} - ${params.ScheduleExpression}`; From 09b51d4277eec3f86996be07d06200f05100fb0a Mon Sep 17 00:00:00 2001 From: Enrique Canals Date: Mon, 8 May 2017 14:16:07 -0700 Subject: [PATCH 4/4] Add ScheduleEvents section to README Remove optional ScheduleEvents param from example event_sources.json --- README.md | 7 +++++++ lib/event_sources.json.example | 3 +-- test/main.js | 3 +-- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index a0a307e0..e3fdf05a 100644 --- a/README.md +++ b/README.md @@ -166,6 +166,13 @@ The `--prebuiltDirectory` flag is useful for working with Webpack for example. I ## Handling `npm link` and Dependencies With Local Paths Perhaps the easiest way to handle these cases is to bundle the code using Webpack and use the `--prebuiltDirectory` flag to package the output for deployment. +## ScheduleEvents +#### Optional Parameter +When using the eventSourceFile flag (-S or --eventSourceFile) to set a ScheduleEvent trigger, you can pass an optional _ScheduleDescription_ key into the ScheduleEvent object with a custom description for the CloudWatch event rule you are defining. By default, node-lambda generates a _ScheduleDescription_ for you based on the ScheduleName and ScheduleExpression of the rule. + +#### Note on ScheduleState for ScheduleEvents +When setting ScheduleState to `ENABLED` or `DISABLED` for ScheduleEvents, it is useful to note that this sets the state of the CloudWatch Event rule but _DOES NOT_ set the state of the trigger for the Lambda function you are deploying; ScheduleEvent triggers are enabled by default in the Lambda console when added using the eventSourceFile flag. + ## Other AWS Lambda Tools Projects + [lambdaws](https://github.com/mentum/lambdaws) diff --git a/lib/event_sources.json.example b/lib/event_sources.json.example index 2d3bfbf4..1b2d7936 100644 --- a/lib/event_sources.json.example +++ b/lib/event_sources.json.example @@ -11,8 +11,7 @@ { "ScheduleName": "node-lambda-test-schedule", "ScheduleState": "ENABLED", - "ScheduleExpression": "rate(1 hour)", - "ScheduleDescription": "Run node-lambda-test-function once per hour" + "ScheduleExpression": "rate(1 hour)" } ] } diff --git a/test/main.js b/test/main.js index 1b31691a..fbccb4c0 100644 --- a/test/main.js +++ b/test/main.js @@ -647,8 +647,7 @@ describe('node-lambda', function () { ScheduleEvents: [{ ScheduleName: 'node-lambda-test-schedule', ScheduleState: 'ENABLED', - ScheduleExpression: 'rate(1 hour)', - ScheduleDescription: 'Run node-lambda-test-function once per hour' + ScheduleExpression: 'rate(1 hour)' }], }; assert.deepEqual(lambda._eventSourceList(program), expected);