UNPKG

1.36 kBJavaScriptView Raw
1'use strict'
2
3class CloudWatchLogs {
4 constructor (aws, region) {
5 // Authenticated `aws` object in `lib/main.js`
6 this.lambda = new aws.Lambda({
7 region: region,
8 apiVersion: '2015-03-31'
9 })
10 this.cloudwatchlogs = new aws.CloudWatchLogs({
11 apiVersion: '2014-03-28'
12 })
13 }
14
15 _logGroupName (params) {
16 return `/aws/lambda/${params.FunctionName}`
17 }
18
19 _createLogGroup (params) {
20 return new Promise((resolve, reject) => {
21 this.cloudwatchlogs.createLogGroup({
22 logGroupName: this._logGroupName(params)
23 }, (err, data) => {
24 if (err) {
25 if (err.code === 'ResourceAlreadyExistsException') {
26 // If it exists it will result in an error but there is no problem.
27 return resolve({})
28 }
29 return reject(err)
30 }
31
32 resolve(data)
33 })
34 })
35 }
36
37 _putRetentionPolicy (params) {
38 return new Promise((resolve, reject) => {
39 this.cloudwatchlogs.putRetentionPolicy({
40 logGroupName: this._logGroupName(params),
41 retentionInDays: params.retentionInDays
42 }, (err, data) => {
43 if (err) return reject(err)
44 resolve(data)
45 })
46 })
47 }
48
49 setLogsRetentionPolicy (params) {
50 return this._createLogGroup(params)
51 .then(() => this._putRetentionPolicy(params))
52 }
53}
54
55module.exports = CloudWatchLogs