Skip to content

Commit 382b098

Browse files
Merge pull request #11 from flemay/bugfix/add-function-log-group-as-dependency
Bugfix/add function log group as dependency
2 parents c33f75a + 4edbefa commit 382b098

File tree

3 files changed

+92
-83
lines changed

3 files changed

+92
-83
lines changed

index.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ class LogForwardingPlugin {
5353
const filterPattern = service.custom.logForwarding.filterPattern || '';
5454
// Get options and parameters to make resources object
5555
const serviceName = service.service;
56+
const awsProvider = this.serverless.getProvider('aws');
5657
const arn = service.custom.logForwarding.destinationARN;
5758
const stage = options.stage && options.stage.length > 0
5859
? options.stage
@@ -74,8 +75,9 @@ class LogForwardingPlugin {
7475
};
7576
for (let i = 0; i < functions.length; i += 1) {
7677
/* merge new SubscriptionFilter with current resources object */
78+
const functionLogGroupId = awsProvider.naming.getLogGroupLogicalId(functions[i]);
7779
const subscriptionFilter = LogForwardingPlugin.makeSubscriptionFilter(serviceName,
78-
stage, arn, functions[i], filterPattern);
80+
stage, arn, functions[i], filterPattern, functionLogGroupId);
7981
_.extend(resourceObj, subscriptionFilter);
8082
}
8183
return resourceObj;
@@ -89,9 +91,11 @@ class LogForwardingPlugin {
8991
* @param {String} arn arn of the lambda to forward to
9092
* @param {String} functionName name of function to make SubscriptionFilter for
9193
* @param {String} filterPattern filter pattern for the Subscription
94+
* @param {String} functionLogGroupId name of the function Log Group to add as a dependency
9295
* @return {Object} SubscriptionFilter
9396
*/
94-
static makeSubscriptionFilter(serviceName, stage, arn, functionName, filterPattern) {
97+
static makeSubscriptionFilter(serviceName, stage, arn, functionName, filterPattern,
98+
functionLogGroupId) {
9599
const logGroupName = `/aws/lambda/${serviceName}-${stage}-${functionName}`;
96100
const filter = {};
97101
filter[`SubscriptionFilter${functionName}`] = {
@@ -103,6 +107,7 @@ class LogForwardingPlugin {
103107
},
104108
DependsOn: [
105109
'LogForwardingLambdaPermission',
110+
functionLogGroupId,
106111
],
107112
};
108113
return filter;

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "serverless-log-forwarding",
3-
"version": "1.1.3",
3+
"version": "1.1.4",
44
"description": "a serverless plugin to forward logs to given lambda function",
55
"main": "index.js",
66
"directories": {
@@ -17,7 +17,8 @@
1717
"eslint-plugin-jsx-a11y": "^3.0.2",
1818
"eslint-plugin-react": "^6.10.3",
1919
"istanbul": "^0.4.5",
20-
"mocha": "^2.2.5"
20+
"mocha": "^2.2.5",
21+
"serverless": "^1.20.2"
2122
},
2223
"scripts": {
2324
"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/index-test.js

Lines changed: 82 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -18,104 +18,99 @@ const correctConfigWithStageFilter = {
1818
filterPattern: 'Test Pattern',
1919
stages: ['production'],
2020
};
21+
22+
const Serverless = require('serverless');
23+
const AwsProvider = require('serverless/lib/plugins/aws/provider/awsProvider');
24+
25+
const createServerless = (options, service) => {
26+
const serverless = new Serverless(options);
27+
serverless.cli = {
28+
log() {
29+
},
30+
};
31+
new AwsProvider(serverless, options); // eslint-disable-line no-new
32+
serverless.service.update(service);
33+
serverless.service.setFunctionNames(options);
34+
return serverless;
35+
};
36+
2137
const constructPluginResources = (logForwarding) => {
22-
const serverless = {
23-
service: {
24-
provider: {
25-
region: 'us-moon-1',
26-
stage: 'test-stage',
27-
},
28-
custom: {
29-
logForwarding,
30-
},
31-
resources: {
32-
Resources: {
33-
TestExistingFilter: {
34-
Type: 'AWS:Test:Filter',
35-
},
36-
},
37-
},
38-
functions: {
39-
testFunctionOne: {
40-
name: 'functionOne',
41-
filterPattern: 'Pattern',
42-
},
43-
testFunctionTwo: {
44-
name: 'functionTwo',
38+
const options = {};
39+
const serverless = createServerless(options, {
40+
provider: {
41+
region: 'us-moon-1',
42+
stage: 'test-stage',
43+
},
44+
custom: {
45+
logForwarding,
46+
},
47+
resources: {
48+
Resources: {
49+
TestExistingFilter: {
50+
Type: 'AWS:Test:Filter',
4551
},
4652
},
47-
service: 'test-service',
4853
},
49-
cli: {
50-
log() {
54+
functions: {
55+
testFunctionOne: {
56+
filterPattern: 'Pattern',
57+
},
58+
testFunctionTwo: {
5159
},
5260
},
53-
};
54-
return new LogForwardingPlugin(serverless, {});
61+
service: 'test-service',
62+
});
63+
return new LogForwardingPlugin(serverless, options);
5564
};
5665
const constructPluginNoResources = (logForwarding) => {
57-
const serverless = {
58-
service: {
59-
provider: {
60-
region: 'us-moon-1',
61-
stage: 'test-stage',
62-
},
63-
custom: {
64-
logForwarding,
65-
},
66-
resources: undefined,
67-
functions: {
68-
testFunctionOne: {
69-
name: 'functionOne',
70-
},
71-
testFunctionTwo: {
72-
name: 'functionTwo',
73-
},
74-
},
75-
service: 'test-service',
66+
const options = {};
67+
const serverless = createServerless(options, {
68+
provider: {
69+
region: 'us-moon-1',
70+
stage: 'test-stage',
7671
},
77-
cli: {
78-
log() {
72+
custom: {
73+
logForwarding,
74+
},
75+
functions: {
76+
testFunctionOne: {
77+
},
78+
testFunctionTwo: {
7979
},
8080
},
81-
};
82-
return new LogForwardingPlugin(serverless, {});
81+
service: 'test-service',
82+
});
83+
serverless.service.resources = undefined;
84+
return new LogForwardingPlugin(serverless, options);
8385
};
8486

8587
const constructPluginResourcesWithParam = (logForwarding) => {
86-
const serverless = {
87-
service: {
88-
provider: {
89-
region: 'us-moon-1',
90-
stage: 'test-stage',
91-
},
92-
custom: {
93-
logForwarding,
94-
},
95-
resources: {
96-
Resources: {
97-
TestExistingFilter: {
98-
Type: 'AWS:Test:Filter',
99-
},
100-
},
101-
},
102-
functions: {
103-
testFunctionOne: {
104-
name: 'functionOne',
105-
filterPattern: 'Pattern',
106-
},
107-
testFunctionTwo: {
108-
name: 'functionTwo',
88+
const options = { stage: 'dev' };
89+
const serverless = createServerless(options, {
90+
provider: {
91+
region: 'us-moon-1',
92+
stage: 'test-stage',
93+
},
94+
custom: {
95+
logForwarding,
96+
},
97+
resources: {
98+
Resources: {
99+
TestExistingFilter: {
100+
Type: 'AWS:Test:Filter',
109101
},
110102
},
111-
service: 'test-service',
112103
},
113-
cli: {
114-
log() {
104+
functions: {
105+
testFunctionOne: {
106+
filterPattern: 'Pattern',
107+
},
108+
testFunctionTwo: {
115109
},
116110
},
117-
};
118-
return new LogForwardingPlugin(serverless, { stage: 'dev' });
111+
service: 'test-service',
112+
});
113+
return new LogForwardingPlugin(serverless, options);
119114
};
120115

121116
describe('Given a serverless config', () => {
@@ -143,6 +138,7 @@ describe('Given a serverless config', () => {
143138
},
144139
DependsOn: [
145140
'LogForwardingLambdaPermission',
141+
'TestFunctionOneLogGroup',
146142
],
147143
},
148144
SubscriptionFiltertestFunctionTwo: {
@@ -154,6 +150,7 @@ describe('Given a serverless config', () => {
154150
},
155151
DependsOn: [
156152
'LogForwardingLambdaPermission',
153+
'TestFunctionTwoLogGroup',
157154
],
158155
},
159156
},
@@ -185,6 +182,7 @@ describe('Given a serverless config', () => {
185182
},
186183
DependsOn: [
187184
'LogForwardingLambdaPermission',
185+
'TestFunctionOneLogGroup',
188186
],
189187
},
190188
SubscriptionFiltertestFunctionTwo: {
@@ -196,6 +194,7 @@ describe('Given a serverless config', () => {
196194
},
197195
DependsOn: [
198196
'LogForwardingLambdaPermission',
197+
'TestFunctionTwoLogGroup',
199198
],
200199
},
201200
},
@@ -224,6 +223,7 @@ describe('Given a serverless config', () => {
224223
},
225224
DependsOn: [
226225
'LogForwardingLambdaPermission',
226+
'TestFunctionOneLogGroup',
227227
],
228228
},
229229
SubscriptionFiltertestFunctionTwo: {
@@ -235,6 +235,7 @@ describe('Given a serverless config', () => {
235235
},
236236
DependsOn: [
237237
'LogForwardingLambdaPermission',
238+
'TestFunctionTwoLogGroup',
238239
],
239240
},
240241
},
@@ -266,6 +267,7 @@ describe('Given a serverless config', () => {
266267
},
267268
DependsOn: [
268269
'LogForwardingLambdaPermission',
270+
'TestFunctionOneLogGroup',
269271
],
270272
},
271273
SubscriptionFiltertestFunctionTwo: {
@@ -277,6 +279,7 @@ describe('Given a serverless config', () => {
277279
},
278280
DependsOn: [
279281
'LogForwardingLambdaPermission',
282+
'TestFunctionTwoLogGroup',
280283
],
281284
},
282285
},

0 commit comments

Comments
 (0)