From 13f8df0da01c600e3952ffa4754fa5db5ea6d91f Mon Sep 17 00:00:00 2001 From: "Eslam A. Hefnawy" Date: Wed, 16 Dec 2015 12:24:27 +0200 Subject: [PATCH] added event source array to functions --- lib/Actions.json | 4 ++ lib/actions/CodeEventDeployLambda.js | 66 ++++++++++++++++------------ lib/templates/s-function.json | 9 +++- 3 files changed, 51 insertions(+), 28 deletions(-) diff --git a/lib/Actions.json b/lib/Actions.json index 7191108ee..fd8cdee6f 100644 --- a/lib/Actions.json +++ b/lib/Actions.json @@ -36,6 +36,10 @@ "path": "./actions/CodeDeployLambdaNodeJs.js", "config": {} }, + { + "path": "./actions/CodeEventDeployLambda.js", + "config": {} + }, { "path": "./actions/EndpointDeploy.js", "config": {} diff --git a/lib/actions/CodeEventDeployLambda.js b/lib/actions/CodeEventDeployLambda.js index b32280dc5..ea092292e 100644 --- a/lib/actions/CodeEventDeployLambda.js +++ b/lib/actions/CodeEventDeployLambda.js @@ -4,11 +4,17 @@ * Action: FunctionRunLambdaNodeJs */ + module.exports = function (SPlugin, serverlessPath) { - const path = require('path'), - SCli = require(path.join(serverlessPath, 'utils/cli')), + + const SPlugin = require('../ServerlessPlugin'), + SError = require('../ServerlessError'), + SCli = require('../utils/cli'), + async = require('async'), + path = require('path'), BbPromise = require('bluebird'); + class CodeEventDeployLambda extends SPlugin { constructor(S, config) { @@ -34,11 +40,10 @@ module.exports = function (SPlugin, serverlessPath) { * Create Event Source for a Lambda Function */ + codeEventDeployLambda(evt) { - let _this = this; - - if (!evt.function.eventSourceArn) return evt; + if (!evt.function.event[0].EventSourceArn) return evt; // Load AWS Service Instances let awsConfig = { @@ -47,37 +52,44 @@ module.exports = function (SPlugin, serverlessPath) { secretAccessKey: _this.S._awsAdminSecretKey, }; - _this.Lambda = require('../utils/aws/Lambda')(awsConfig); - - let params = { - FunctionName: _this.Lambda.sGetLambdaName(_this.S._projectJson, evt.function), - EventSourceArn: evt.function.eventSourceArn, - StartingPosition: 'LATEST' - }; + return new BbPromise(function (resolve, reject) { - return _this.Lambda.createEventSourceMappingPromised(params) - .then(function (data) { + async.eachLimit(evt.function.event, 5, function (event, cb) { - SCli.log('Created event source for lambda: ' + evt.function.name); + // Create new evt object for concurrent operations + let params = event; - evt.function.EventSourceUUID = data.UUID; - return evt; - }) - .catch(function (e) { - if (e.code === 'ResourceConflictException') { + params.FunctionName = _this.Lambda.sGetLambdaName(_this.S._projectJson, evt.function); - SCli.log('Event source already exists for lambda: ' + evt.function.name); + return _this.Lambda.createEventSourceMappingPromised(params) + .then(function (data) { + + SCli.sDebug(`Created event source ${event.EventSourceArn} for lambda ${evt.function.name}`); + + evt.function.event.UUID = data.UUID; + return cb(); + }) + .catch(function (e) { + if (e.code === 'ResourceConflictException') { + + SCli.sDebug(`Event source ${event.EventSourceArn} already exists for lambda ${evt.function.name}`); + + return cb(); + } else { + throw new SError(`Error setting lambda event source: ` + e); + } + }) + + }, function () { + return resolve(evt); + }); + }); - return evt; - } else { - return evt; - //throw new SError(`Error setting lambda event source: ` + e); - } - }) } + } return ( CodeEventDeployLambda ); diff --git a/lib/templates/s-function.json b/lib/templates/s-function.json index 83beeb57f..81a3f4db2 100644 --- a/lib/templates/s-function.json +++ b/lib/templates/s-function.json @@ -8,7 +8,14 @@ "handler": "", "timeout": 6, "memorySize": 1024, - "eventSourceArn": "", + "event": [ + { + "EventSourceArn": "", + "StartingPosition": "TRIM_HORIZON", + "BatchSize": 100, + "Enabled": true + } + ], "endpoints": [ { "path": "",