mirror of
https://github.com/serverless/serverless.git
synced 2026-01-18 14:58:43 +00:00
84 lines
1.9 KiB
JavaScript
84 lines
1.9 KiB
JavaScript
'use strict';
|
|
|
|
/**
|
|
* Action: FunctionRunLambdaNodeJs
|
|
*/
|
|
|
|
const SPlugin = require('../ServerlessPlugin'),
|
|
SError = require('../ServerlessError'),
|
|
SCli = require('../utils/cli'),
|
|
BbPromise = require('bluebird');
|
|
|
|
|
|
class CreateEventSourceLambdaNodejs extends SPlugin {
|
|
|
|
constructor(S, config) {
|
|
super(S, config);
|
|
}
|
|
|
|
static getName() {
|
|
return 'serverless.core.' + CreateEventSourceLambdaNodejs.name;
|
|
}
|
|
|
|
registerActions() {
|
|
|
|
this.S.addAction(this.createEventSourceLambdaNodejs.bind(this), {
|
|
handler: 'createEventSourceLambdaNodejs',
|
|
description: 'Creates an event source for a lambda function.'
|
|
});
|
|
|
|
return BbPromise.resolve();
|
|
}
|
|
|
|
/**
|
|
* Create Event Source for a Lambda Function
|
|
*/
|
|
|
|
createEventSourceLambdaNodejs(evt) {
|
|
|
|
let _this = this;
|
|
|
|
if (!evt.function.eventSourceArn) return evt;
|
|
|
|
// Load AWS Service Instances
|
|
let awsConfig = {
|
|
region: evt.region.region,
|
|
accessKeyId: _this.S._awsAdminKeyId,
|
|
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 _this.Lambda.createEventSourceMappingPromised(params)
|
|
.then(function(data) {
|
|
|
|
SCli.log('Created event source for lambda: ' + evt.function.name);
|
|
|
|
evt.function.EventSourceUUID = data.UUID;
|
|
return evt;
|
|
})
|
|
.catch(function(e) {
|
|
if (e.code === 'ResourceConflictException') {
|
|
|
|
SCli.log('Event source already exists for lambda: ' + evt.function.name);
|
|
|
|
return evt;
|
|
} else {
|
|
return evt;
|
|
//throw new SError(`Error setting lambda event source: ` + e);
|
|
}
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
module.exports = CreateEventSourceLambdaNodejs;
|