-
Notifications
You must be signed in to change notification settings - Fork 184
/
Copy pathcloudwatch_logs.js
55 lines (48 loc) · 1.32 KB
/
cloudwatch_logs.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
'use strict'
class CloudWatchLogs {
constructor (aws, region) {
// Authenticated `aws` object in `lib/main.js`
this.lambda = new aws.Lambda({
region,
apiVersion: '2015-03-31'
})
this.cloudwatchlogs = new aws.CloudWatchLogs({
apiVersion: '2014-03-28'
})
}
_logGroupName (params) {
return `/aws/lambda/${params.FunctionName}`
}
_createLogGroup (params) {
return new Promise((resolve, reject) => {
this.cloudwatchlogs.createLogGroup({
logGroupName: this._logGroupName(params)
}, (err, data) => {
if (err) {
if (err.code === 'ResourceAlreadyExistsException') {
// If it exists it will result in an error but there is no problem.
return resolve({})
}
return reject(err)
}
resolve(data)
})
})
}
_putRetentionPolicy (params) {
return new Promise((resolve, reject) => {
this.cloudwatchlogs.putRetentionPolicy({
logGroupName: this._logGroupName(params),
retentionInDays: params.retentionInDays
}, (err, data) => {
if (err) return reject(err)
resolve(data)
})
})
}
setLogsRetentionPolicy (params) {
return this._createLogGroup(params)
.then(() => this._putRetentionPolicy(params))
}
}
module.exports = CloudWatchLogs