added event source array to functions

This commit is contained in:
Eslam A. Hefnawy 2015-12-16 12:24:27 +02:00
parent dc018aa3d4
commit 13f8df0da0
3 changed files with 51 additions and 28 deletions

View File

@ -36,6 +36,10 @@
"path": "./actions/CodeDeployLambdaNodeJs.js",
"config": {}
},
{
"path": "./actions/CodeEventDeployLambda.js",
"config": {}
},
{
"path": "./actions/EndpointDeploy.js",
"config": {}

View File

@ -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 );

View File

@ -8,7 +8,14 @@
"handler": "",
"timeout": 6,
"memorySize": 1024,
"eventSourceArn": "",
"event": [
{
"EventSourceArn": "",
"StartingPosition": "TRIM_HORIZON",
"BatchSize": 100,
"Enabled": true
}
],
"endpoints": [
{
"path": "",