-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathadd-log-retention.js
92 lines (78 loc) · 3.09 KB
/
add-log-retention.js
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
'use strict';
const nco = require('nco');
const semver = require('semver');
const PLUGIN_NAME = 'serverless-plugin-log-retention';
//values from http://docs.aws.amazon.com/AmazonCloudWatchLogs/latest/APIReference/API_PutRetentionPolicy.html
const validRetentionInDays = [1, 3, 5, 7, 14, 30, 60, 90, 120, 150, 180, 365, 400, 545, 731, 1827, 3653];
class AwsAddLogRetention {
constructor(serverless, options) {
if(!semver.satisfies(serverless.version, '>= 1.20.2')) {
throw new Error('serverless-plugin-log-retention requires serverless 1.20.2 or higher');
}
this.serverless = serverless;
this.options = options;
this.provider = this.serverless.getProvider('aws');
// Add schema based validation of service config
const propertyDefinitions = {
properties: {
logRetentionInDays: { type: 'number' }
}
};
if(this.serverless.configSchemaHandler.defineFunctionProperties) {
this.serverless.configSchemaHandler.defineFunctionProperties('aws', propertyDefinitions);
}
if(this.serverless.configSchemaHandler.defineCustomProperties) {
this.serverless.configSchemaHandler.defineCustomProperties({
type: 'object',
properties: {
[PLUGIN_NAME] : {
type: 'object',
...propertyDefinitions
}
}
});
}
this.hooks = {
'package:createDeploymentArtifacts': this.beforeDeploy.bind(this),
};
}
sanitizeRetentionValue(inputValue) {
const value = Number(inputValue);
if(Number.isInteger(value) && validRetentionInDays.includes(value)) {
return value;
} else {
throw new Error(`RetentionInDays value must be one of ${validRetentionInDays}`);
}
}
addLogRetentionForFunctions(globalLogRetentionInDays) {
const service = this.serverless.service;
if(typeof service.functions !== 'object') {
return;
}
const resources = nco(service.resources, {});
resources.Resources = nco(resources.Resources, {});
Object.keys(service.functions).forEach(functionName => {
const localLogRentationInDays = nco(service.functions[functionName].logRetentionInDays, null);
if(localLogRentationInDays === null && globalLogRetentionInDays === null) {
return;
}
const functionLogRetentionInDays = localLogRentationInDays === null ? globalLogRetentionInDays : this.sanitizeRetentionValue(localLogRentationInDays);
const logGroupLogicalId = this.provider.naming.getLogGroupLogicalId(functionName);
const resource = {
Type: 'AWS::Logs::LogGroup',
Properties: {
RetentionInDays: functionLogRetentionInDays
}
};
resources.Resources[logGroupLogicalId] = resource;
});
}
beforeDeploy() {
const service = this.serverless.service;
const globalLogRetentionInDays = service.custom && service.custom[PLUGIN_NAME] && service.custom[PLUGIN_NAME].logRetentionInDays
? this.sanitizeRetentionValue(service.custom[PLUGIN_NAME].logRetentionInDays)
: null;
this.addLogRetentionForFunctions(globalLogRetentionInDays);
}
}
module.exports = AwsAddLogRetention;