diff --git a/.gitignore b/.gitignore index f805976..41c023e 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ node_modules/ coverage npm-debug.log .idea +.vscode diff --git a/index.js b/index.js index 43d7b4d..1e0e078 100644 --- a/index.js +++ b/index.js @@ -6,6 +6,7 @@ class LogForwardingPlugin { constructor(serverless, options) { this.serverless = serverless; this.options = options; + this.provider = this.serverless.getProvider('aws'); /* Hooks tell Serverless when to do what */ this.hooks = { @@ -20,7 +21,9 @@ class LogForwardingPlugin { updateResources() { // check if stage is specified in config const service = this.serverless.service; - const stage = this.options.stage; + const stage = this.options.stage && this.options.stage.length > 0 + ? this.options.stage + : service.provider.stage; if (service.custom.logForwarding.stages && service.custom.logForwarding.stages.indexOf(stage) === -1) { this.serverless.cli.log(`Log Forwarding is ignored for ${stage} stage`); @@ -33,6 +36,8 @@ class LogForwardingPlugin { this.serverless.service.resources = { Resources: {}, }; + } else if (this.serverless.service.resources.Resources === undefined) { + this.serverless.service.resources.Resources = {}; } _.extend(this.serverless.service.resources.Resources, resourceObj); this.serverless.cli.log('Log Forwarding Resources Updated'); @@ -45,19 +50,13 @@ class LogForwardingPlugin { */ createResourcesObj() { const service = this.serverless.service; - const options = this.options; // Checks if the serverless file is setup correctly if (service.custom.logForwarding.destinationARN == null) { throw new Error('Serverless-log-forwarding is not configured correctly. Please see README for proper setup.'); } const filterPattern = service.custom.logForwarding.filterPattern || ''; // Get options and parameters to make resources object - const serviceName = service.service; - const awsProvider = this.serverless.getProvider('aws'); const arn = service.custom.logForwarding.destinationARN; - const stage = options.stage && options.stage.length > 0 - ? options.stage - : service.provider.stage; // Get list of all functions in this lambda const functions = _.keys(service.functions); const principal = `logs.${service.provider.region}.amazonaws.com`; @@ -75,9 +74,7 @@ class LogForwardingPlugin { }; for (let i = 0; i < functions.length; i += 1) { /* merge new SubscriptionFilter with current resources object */ - const functionLogGroupId = awsProvider.naming.getLogGroupLogicalId(functions[i]); - const subscriptionFilter = LogForwardingPlugin.makeSubscriptionFilter(serviceName, - stage, arn, functions[i], filterPattern, functionLogGroupId); + const subscriptionFilter = this.makeSubscriptionFilter(arn, functions[i], filterPattern); _.extend(resourceObj, subscriptionFilter); } return resourceObj; @@ -86,19 +83,18 @@ class LogForwardingPlugin { /** * Makes a Subscription Filter object for given function name - * @param {String} serviceName name of current service - * @param {String} stage stage this lambda is being deployed to * @param {String} arn arn of the lambda to forward to * @param {String} functionName name of function to make SubscriptionFilter for * @param {String} filterPattern filter pattern for the Subscription - * @param {String} functionLogGroupId name of the function Log Group to add as a dependency * @return {Object} SubscriptionFilter */ - static makeSubscriptionFilter(serviceName, stage, arn, functionName, filterPattern, - functionLogGroupId) { - const logGroupName = `/aws/lambda/${serviceName}-${stage}-${functionName}`; + makeSubscriptionFilter(arn, functionName, filterPattern) { + const functionObject = this.serverless.service.getFunction(functionName); + const logGroupName = this.provider.naming.getLogGroupName(functionObject.name); + const filterLogicalId = `SubscriptionFilter${this.provider.naming.getNormalizedFunctionName(functionName)}`; + const functionLogGroupId = this.provider.naming.getLogGroupLogicalId(functionName); const filter = {}; - filter[`SubscriptionFilter${functionName}`] = { + filter[filterLogicalId] = { Type: 'AWS::Logs::SubscriptionFilter', Properties: { DestinationArn: arn, diff --git a/package.json b/package.json index 2a64291..f133a71 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "serverless-log-forwarding", - "version": "1.1.4", + "version": "1.1.5", "description": "a serverless plugin to forward logs to given lambda function", "main": "index.js", "directories": { @@ -21,7 +21,7 @@ "serverless": "^1.20.2" }, "scripts": { - "test": "node ./node_modules/istanbul/lib/cli.js cover _mocha -- -R spec && node ./node_modules/istanbul/lib/cli.js check-coverage --line 70 coverage/coverage.json", + "test": "node ./node_modules/istanbul/lib/cli.js cover ./node_modules/mocha/bin/_mocha -- -R spec && node ./node_modules/istanbul/lib/cli.js check-coverage --line 70 coverage/coverage.json", "lint": "eslint ." }, "repository": { diff --git a/test/index-test.js b/test/index-test.js index b8811ad..3fa3ac4 100644 --- a/test/index-test.js +++ b/test/index-test.js @@ -129,7 +129,7 @@ describe('Given a serverless config', () => { Principal: 'logs.us-moon-1.amazonaws.com', }, }, - SubscriptionFiltertestFunctionOne: { + SubscriptionFilterTestFunctionOne: { Type: 'AWS::Logs::SubscriptionFilter', Properties: { DestinationArn: 'arn:aws:lambda:us-moon-1:314159265358:function:testforward-test-forward', @@ -141,7 +141,7 @@ describe('Given a serverless config', () => { 'TestFunctionOneLogGroup', ], }, - SubscriptionFiltertestFunctionTwo: { + SubscriptionFilterTestFunctionTwo: { Type: 'AWS::Logs::SubscriptionFilter', Properties: { DestinationArn: 'arn:aws:lambda:us-moon-1:314159265358:function:testforward-test-forward', @@ -173,7 +173,7 @@ describe('Given a serverless config', () => { Principal: 'logs.us-moon-1.amazonaws.com', }, }, - SubscriptionFiltertestFunctionOne: { + SubscriptionFilterTestFunctionOne: { Type: 'AWS::Logs::SubscriptionFilter', Properties: { DestinationArn: 'arn:aws:lambda:us-moon-1:314159265358:function:testforward-dev-forward', @@ -185,7 +185,7 @@ describe('Given a serverless config', () => { 'TestFunctionOneLogGroup', ], }, - SubscriptionFiltertestFunctionTwo: { + SubscriptionFilterTestFunctionTwo: { Type: 'AWS::Logs::SubscriptionFilter', Properties: { DestinationArn: 'arn:aws:lambda:us-moon-1:314159265358:function:testforward-dev-forward', @@ -214,7 +214,7 @@ describe('Given a serverless config', () => { Principal: 'logs.us-moon-1.amazonaws.com', }, }, - SubscriptionFiltertestFunctionOne: { + SubscriptionFilterTestFunctionOne: { Type: 'AWS::Logs::SubscriptionFilter', Properties: { DestinationArn: 'arn:aws:lambda:us-moon-1:314159265358:function:testforward-test-forward', @@ -226,7 +226,7 @@ describe('Given a serverless config', () => { 'TestFunctionOneLogGroup', ], }, - SubscriptionFiltertestFunctionTwo: { + SubscriptionFilterTestFunctionTwo: { Type: 'AWS::Logs::SubscriptionFilter', Properties: { DestinationArn: 'arn:aws:lambda:us-moon-1:314159265358:function:testforward-test-forward', @@ -258,7 +258,7 @@ describe('Given a serverless config', () => { Principal: 'logs.us-moon-1.amazonaws.com', }, }, - SubscriptionFiltertestFunctionOne: { + SubscriptionFilterTestFunctionOne: { Type: 'AWS::Logs::SubscriptionFilter', Properties: { DestinationArn: 'arn:aws:lambda:us-moon-1:314159265358:function:testforward-test-forward', @@ -270,7 +270,7 @@ describe('Given a serverless config', () => { 'TestFunctionOneLogGroup', ], }, - SubscriptionFiltertestFunctionTwo: { + SubscriptionFilterTestFunctionTwo: { Type: 'AWS::Logs::SubscriptionFilter', Properties: { DestinationArn: 'arn:aws:lambda:us-moon-1:314159265358:function:testforward-test-forward',