Skip to content

Commit 37311b3

Browse files
Merge pull request #9 from interisti/feature/stage-filter
add stage filter option
2 parents be24342 + 2b412aa commit 37311b3

File tree

5 files changed

+34
-5
lines changed

5 files changed

+34
-5
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@ custom:
3434
logForwarding:
3535
destinationARN: '[ARN of Lambda Function to forward logs to]'
3636
# optional:
37-
filterPattern: `[filter pattern for logs that are sent to Lambda function]'
37+
filterPattern: '[filter pattern for logs that are sent to Lambda function]'
38+
stages:
39+
- '[name of the stage to apply log forwarding]'
40+
- '[another stage name to filter]'
3841
```
3942
4043
## Running Tests

index.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,15 @@ class LogForwardingPlugin {
1818
* Updates CloudFormation resources with log forwarding
1919
*/
2020
updateResources() {
21+
// check if stage is specified in config
22+
const service = this.serverless.service;
23+
const stage = this.options.stage;
24+
if (service.custom.logForwarding.stages &&
25+
service.custom.logForwarding.stages.indexOf(stage) === -1) {
26+
this.serverless.cli.log(`Log Forwarding is ignored for ${stage} stage`);
27+
return;
28+
}
29+
2130
this.serverless.cli.log('Updating Log Forwarding Resources...');
2231
const resourceObj = this.createResourcesObj();
2332
if (this.serverless.service.resources === undefined) {
@@ -46,8 +55,8 @@ class LogForwardingPlugin {
4655
const serviceName = service.service;
4756
const arn = service.custom.logForwarding.destinationARN;
4857
const stage = options.stage && options.stage.length > 0
49-
? options.stage
50-
: service.provider.stage;
58+
? options.stage
59+
: service.provider.stage;
5160
// Get list of all functions in this lambda
5261
const functions = _.keys(service.functions);
5362
const principal = `logs.${service.provider.region}.amazonaws.com`;

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "serverless-log-forwarding",
3-
"version": "1.1.2",
3+
"version": "1.1.3",
44
"description": "a serverless plugin to forward logs to given lambda function",
55
"main": "index.js",
66
"directories": {

test/index-test.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ const correctConfigWithFilterPattern = {
1313
destinationARN: 'arn:aws:lambda:us-moon-1:314159265358:function:testforward-test-forward',
1414
filterPattern: 'Test Pattern',
1515
};
16+
const correctConfigWithStageFilter = {
17+
destinationARN: 'arn:aws:lambda:us-moon-1:314159265358:function:testforward-test-forward',
18+
filterPattern: 'Test Pattern',
19+
stages: ['production'],
20+
};
1621
const constructPluginResources = (logForwarding) => {
1722
const serverless = {
1823
service: {
@@ -279,6 +284,18 @@ describe('Given a serverless config', () => {
279284
plugin.updateResources();
280285
expect(plugin.serverless.service.resources).to.eql(expectedResources);
281286
});
287+
it('uses stage filter if set', () => {
288+
const plugin = constructPluginResources(correctConfigWithStageFilter);
289+
const expectedResources = {
290+
Resources: {
291+
TestExistingFilter: {
292+
Type: 'AWS:Test:Filter',
293+
},
294+
},
295+
};
296+
plugin.updateResources();
297+
expect(plugin.serverless.service.resources).to.eql(expectedResources);
298+
});
282299
});
283300

284301
describe('Catching errors in serverless config ', () => {

0 commit comments

Comments
 (0)