-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
78 lines (68 loc) · 2.52 KB
/
index.js
File metadata and controls
78 lines (68 loc) · 2.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
"use strict";
const _ = require("lodash");
class LogStreamingPlugin {
constructor(serverless, options) {
this.serverless = serverless;
this.options = options;
this.provider = this.serverless.getProvider("aws");
this.hooks = {
"after:deploy:compileFunctions": this.logServerless.bind(this)
};
}
logServerless() {
const logHandlerFnName = _.get(this.serverless, "service.custom.logHandler.function", "loghandler");
const logHandlerLogicalId = this.provider.naming.getLambdaLogicalId(logHandlerFnName);
const cloudwatchLogsSubscriptionFilterTemplate = {
Type: "AWS::Logs::SubscriptionFilter",
DependsOn: "LoggingLambdaPermission",
Properties: {
LogGroupName: "LogGroup",
FilterPattern: "",
DestinationArn: {
"Fn::GetAtt": [
logHandlerLogicalId,
"Arn"
]
}
}
};
_.forEach(this.serverless.service.getAllFunctions(), functionName => {
const functionLogicalId = this.provider.naming.getLambdaLogicalId(functionName);
if (functionName === logHandlerFnName) {
// Do not add the loghandler to itself.
return;
}
const functionObject = this.serverless.service.getFunction(functionName);
if (_.get(functionObject, "loghandler", true) === false) {
// If the loghandler property is set to false then do not create a loghandler
return;
}
const logGroupLogicalId = this.provider.naming.getLogGroupName(functionObject.name);
cloudwatchLogsSubscriptionFilterTemplate.Properties.LogGroupName = logGroupLogicalId;
const newResources = {
[`${functionLogicalId}SubscriptionFilter`]: cloudwatchLogsSubscriptionFilterTemplate
};
_.merge(this.serverless.service.provider.compiledCloudFormationTemplate.Resources, newResources);
});
if (!_.has(this.serverless.service.provider.compiledCloudFormationTemplate.Resources, "LoggingLambdaPermission")) {
if (!logHandlerLogicalId || !logHandlerFnName) {
throw new Error("Loghandler plugin is not properly configured. Missing loghandler function definition.");
}
const loggingPermissions = {
["LoggingLambdaPermission"]: {
Type: "AWS::Lambda::Permission",
Properties: {
FunctionName: {
Ref: logHandlerLogicalId
},
Action: "lambda:InvokeFunction",
Principal: "logs.amazonaws.com"
}
}
};
this.serverless.cli.log(`Adding 'LoggingLambdaPermission' to Resources with FunctionName ${logHandlerLogicalId}`);
_.merge(this.serverless.service.provider.compiledCloudFormationTemplate.Resources, loggingPermissions);
}
}
}
module.exports = LogStreamingPlugin;