Skip to content

Bugfix/add function log group as dependency #11

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
merged 5 commits into from
Sep 27, 2017
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
9 changes: 7 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class LogForwardingPlugin {
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
Expand All @@ -74,8 +75,9 @@ 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);
stage, arn, functions[i], filterPattern, functionLogGroupId);
_.extend(resourceObj, subscriptionFilter);
}
return resourceObj;
Expand All @@ -89,9 +91,11 @@ class LogForwardingPlugin {
* @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) {
static makeSubscriptionFilter(serviceName, stage, arn, functionName, filterPattern,
functionLogGroupId) {
const logGroupName = `/aws/lambda/${serviceName}-${stage}-${functionName}`;
const filter = {};
filter[`SubscriptionFilter${functionName}`] = {
Expand All @@ -103,6 +107,7 @@ class LogForwardingPlugin {
},
DependsOn: [
'LogForwardingLambdaPermission',
functionLogGroupId,
],
};
return filter;
Expand Down
5 changes: 3 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.3",
"version": "1.1.4",
"description": "a serverless plugin to forward logs to given lambda function",
"main": "index.js",
"directories": {
Expand All @@ -17,7 +17,8 @@
"eslint-plugin-jsx-a11y": "^3.0.2",
"eslint-plugin-react": "^6.10.3",
"istanbul": "^0.4.5",
"mocha": "^2.2.5"
"mocha": "^2.2.5",
"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",
Expand Down
161 changes: 82 additions & 79 deletions test/index-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,104 +18,99 @@ const correctConfigWithStageFilter = {
filterPattern: 'Test Pattern',
stages: ['production'],
};

const Serverless = require('serverless');
const AwsProvider = require('serverless/lib/plugins/aws/provider/awsProvider');

const createServerless = (options, service) => {
const serverless = new Serverless(options);
serverless.cli = {
log() {
},
};
new AwsProvider(serverless, options); // eslint-disable-line no-new
serverless.service.update(service);
serverless.service.setFunctionNames(options);
return serverless;
};

const constructPluginResources = (logForwarding) => {
const serverless = {
service: {
provider: {
region: 'us-moon-1',
stage: 'test-stage',
},
custom: {
logForwarding,
},
resources: {
Resources: {
TestExistingFilter: {
Type: 'AWS:Test:Filter',
},
},
},
functions: {
testFunctionOne: {
name: 'functionOne',
filterPattern: 'Pattern',
},
testFunctionTwo: {
name: 'functionTwo',
const options = {};
const serverless = createServerless(options, {
provider: {
region: 'us-moon-1',
stage: 'test-stage',
},
custom: {
logForwarding,
},
resources: {
Resources: {
TestExistingFilter: {
Type: 'AWS:Test:Filter',
},
},
service: 'test-service',
},
cli: {
log() {
functions: {
testFunctionOne: {
filterPattern: 'Pattern',
},
testFunctionTwo: {
},
},
};
return new LogForwardingPlugin(serverless, {});
service: 'test-service',
});
return new LogForwardingPlugin(serverless, options);
};
const constructPluginNoResources = (logForwarding) => {
const serverless = {
service: {
provider: {
region: 'us-moon-1',
stage: 'test-stage',
},
custom: {
logForwarding,
},
resources: undefined,
functions: {
testFunctionOne: {
name: 'functionOne',
},
testFunctionTwo: {
name: 'functionTwo',
},
},
service: 'test-service',
const options = {};
const serverless = createServerless(options, {
provider: {
region: 'us-moon-1',
stage: 'test-stage',
},
cli: {
log() {
custom: {
logForwarding,
},
functions: {
testFunctionOne: {
},
testFunctionTwo: {
},
},
};
return new LogForwardingPlugin(serverless, {});
service: 'test-service',
});
serverless.service.resources = undefined;
return new LogForwardingPlugin(serverless, options);
};

const constructPluginResourcesWithParam = (logForwarding) => {
const serverless = {
service: {
provider: {
region: 'us-moon-1',
stage: 'test-stage',
},
custom: {
logForwarding,
},
resources: {
Resources: {
TestExistingFilter: {
Type: 'AWS:Test:Filter',
},
},
},
functions: {
testFunctionOne: {
name: 'functionOne',
filterPattern: 'Pattern',
},
testFunctionTwo: {
name: 'functionTwo',
const options = { stage: 'dev' };
const serverless = createServerless(options, {
provider: {
region: 'us-moon-1',
stage: 'test-stage',
},
custom: {
logForwarding,
},
resources: {
Resources: {
TestExistingFilter: {
Type: 'AWS:Test:Filter',
},
},
service: 'test-service',
},
cli: {
log() {
functions: {
testFunctionOne: {
filterPattern: 'Pattern',
},
testFunctionTwo: {
},
},
};
return new LogForwardingPlugin(serverless, { stage: 'dev' });
service: 'test-service',
});
return new LogForwardingPlugin(serverless, options);
};

describe('Given a serverless config', () => {
Expand Down Expand Up @@ -143,6 +138,7 @@ describe('Given a serverless config', () => {
},
DependsOn: [
'LogForwardingLambdaPermission',
'TestFunctionOneLogGroup',
],
},
SubscriptionFiltertestFunctionTwo: {
Expand All @@ -154,6 +150,7 @@ describe('Given a serverless config', () => {
},
DependsOn: [
'LogForwardingLambdaPermission',
'TestFunctionTwoLogGroup',
],
},
},
Expand Down Expand Up @@ -185,6 +182,7 @@ describe('Given a serverless config', () => {
},
DependsOn: [
'LogForwardingLambdaPermission',
'TestFunctionOneLogGroup',
],
},
SubscriptionFiltertestFunctionTwo: {
Expand All @@ -196,6 +194,7 @@ describe('Given a serverless config', () => {
},
DependsOn: [
'LogForwardingLambdaPermission',
'TestFunctionTwoLogGroup',
],
},
},
Expand Down Expand Up @@ -224,6 +223,7 @@ describe('Given a serverless config', () => {
},
DependsOn: [
'LogForwardingLambdaPermission',
'TestFunctionOneLogGroup',
],
},
SubscriptionFiltertestFunctionTwo: {
Expand All @@ -235,6 +235,7 @@ describe('Given a serverless config', () => {
},
DependsOn: [
'LogForwardingLambdaPermission',
'TestFunctionTwoLogGroup',
],
},
},
Expand Down Expand Up @@ -266,6 +267,7 @@ describe('Given a serverless config', () => {
},
DependsOn: [
'LogForwardingLambdaPermission',
'TestFunctionOneLogGroup',
],
},
SubscriptionFiltertestFunctionTwo: {
Expand All @@ -277,6 +279,7 @@ describe('Given a serverless config', () => {
},
DependsOn: [
'LogForwardingLambdaPermission',
'TestFunctionTwoLogGroup',
],
},
},
Expand Down