Skip to content

Use Serverless naming module to format log group name #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ node_modules/
coverage
npm-debug.log
.idea
.vscode
30 changes: 13 additions & 17 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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`);
Expand All @@ -33,6 +36,8 @@ class LogForwardingPlugin {
this.serverless.service.resources = {
Resources: {},
};
} else if (this.serverless.service.resources.Resources === undefined) {
this.serverless.service.resources.Resources = {};
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updates the resources object if it doesn\'t exist test was failing due to this.serverless.service.resources being instantiated by Serverless framework but didn't have Resources property set.

}
_.extend(this.serverless.service.resources.Resources, resourceObj);
this.serverless.cli.log('Log Forwarding Resources Updated');
Expand All @@ -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`;
Expand All @@ -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;
Expand All @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand All @@ -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": {
Expand Down
16 changes: 8 additions & 8 deletions test/index-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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',
Expand Down Expand Up @@ -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',
Expand All @@ -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',
Expand Down Expand Up @@ -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',
Expand All @@ -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',
Expand Down Expand Up @@ -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',
Expand All @@ -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',
Expand Down