From 1d622f39b857a63864c3e0b75d917ba56c359f81 Mon Sep 17 00:00:00 2001 From: "Shane P. Van Hart" Date: Wed, 2 Aug 2017 15:27:52 -0400 Subject: [PATCH 1/5] Added the ability to set constants when scheduling a Lambda function Cloudwatch event. --- lib/event_sources.json.example | 7 ++++++- lib/schedule_events.js | 3 ++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/event_sources.json.example b/lib/event_sources.json.example index 1b2d7936..69db547d 100644 --- a/lib/event_sources.json.example +++ b/lib/event_sources.json.example @@ -11,7 +11,12 @@ { "ScheduleName": "node-lambda-test-schedule", "ScheduleState": "ENABLED", - "ScheduleExpression": "rate(1 hour)" + "ScheduleExpression": "rate(1 hour)", + "Input": + { + "key": "value", + "key2": "value" + } } ] } diff --git a/lib/schedule_events.js b/lib/schedule_events.js index 1c6ac503..186c92fc 100644 --- a/lib/schedule_events.js +++ b/lib/schedule_events.js @@ -71,7 +71,8 @@ class ScheduleEvents { Rule: params.ScheduleName, Targets: [{ Arn: params.FunctionArn, - Id: this._functionName(params) + Id: this._functionName(params), + Input: JSON.stringify(params.Input) }] } } From 8ac28a345ee908c18524422bf620ba99e4b96219 Mon Sep 17 00:00:00 2001 From: abetomo Date: Thu, 5 Oct 2017 11:49:16 +0900 Subject: [PATCH 2/5] Remove unnecessary space --- lib/event_sources.json.example | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/event_sources.json.example b/lib/event_sources.json.example index 69db547d..9e164125 100644 --- a/lib/event_sources.json.example +++ b/lib/event_sources.json.example @@ -16,7 +16,7 @@ { "key": "value", "key2": "value" - } + } } ] } From cbb96d07cd31fb38428417c5b771a5b2760679f4 Mon Sep 17 00:00:00 2001 From: abetomo Date: Thu, 5 Oct 2017 11:49:43 +0900 Subject: [PATCH 3/5] Modify example JSON key name --- lib/event_sources.json.example | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/event_sources.json.example b/lib/event_sources.json.example index 9e164125..d95bd4a9 100644 --- a/lib/event_sources.json.example +++ b/lib/event_sources.json.example @@ -14,7 +14,7 @@ "ScheduleExpression": "rate(1 hour)", "Input": { - "key": "value", + "key1": "value", "key2": "value" } } From 3222431f0bea21c5a80a7dec94e074a879dec9d4 Mon Sep 17 00:00:00 2001 From: abetomo Date: Thu, 5 Oct 2017 11:50:23 +0900 Subject: [PATCH 4/5] Set default value when key does not exist --- 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 186c92fc..ac40db84 100644 --- a/lib/schedule_events.js +++ b/lib/schedule_events.js @@ -72,7 +72,7 @@ class ScheduleEvents { Targets: [{ Arn: params.FunctionArn, Id: this._functionName(params), - Input: JSON.stringify(params.Input) + Input: params.hasOwnProperty('Input') ? JSON.stringify(params.Input) : '' }] } } From 2f58f7b348e4b938d02b1d9e55e95dee24c4be6c Mon Sep 17 00:00:00 2001 From: abetomo Date: Thu, 5 Oct 2017 11:50:56 +0900 Subject: [PATCH 5/5] Update unit test --- test/main.js | 6 +++++- test/schedule_events.js | 20 ++++++++++++++++++-- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/test/main.js b/test/main.js index f5782e3f..0bccc7d3 100644 --- a/test/main.js +++ b/test/main.js @@ -814,7 +814,11 @@ describe('lib/main', function () { ScheduleEvents: [{ ScheduleName: 'node-lambda-test-schedule', ScheduleState: 'ENABLED', - ScheduleExpression: 'rate(1 hour)' + ScheduleExpression: 'rate(1 hour)', + Input: { + key1: 'value', + key2: 'value' + } }] } assert.deepEqual(lambda._eventSourceList(program), expected) diff --git a/test/schedule_events.js b/test/schedule_events.js index 1003a4cd..b5e8ab1a 100644 --- a/test/schedule_events.js +++ b/test/schedule_events.js @@ -121,16 +121,32 @@ describe('lib/schedule_events', () => { }) describe('_putTargetsParams', () => { - it('correct value', () => { + it('correct value (No "Input" setting)', () => { const expected = { Rule: 'node-lambda-test-schedule', Targets: [{ Arn: 'arn:aws:lambda:us-west-2:XXX:function:node-lambda-test-function', - Id: 'node-lambda-test-function' + Id: 'node-lambda-test-function', + Input: '' }] } assert.deepEqual(schedule._putTargetsParams(params), expected) }) + + it('correct value ("Input" setting)', () => { + const expected = { + Rule: 'node-lambda-test-schedule', + Targets: [{ + Arn: 'arn:aws:lambda:us-west-2:XXX:function:node-lambda-test-function', + Id: 'node-lambda-test-function', + Input: '{"key":"value"}' + }] + } + assert.deepEqual( + schedule._putTargetsParams(Object.assign({Input: {key: 'value'}}, params)), + expected + ) + }) }) describe('_putRule', () => {