|
1 | 1 | 'use strict'
|
2 | 2 |
|
| 3 | +const { |
| 4 | + CloudWatchLogsClient, |
| 5 | + CreateLogGroupCommand, |
| 6 | + PutRetentionPolicyCommand |
| 7 | +} = require('@aws-sdk/client-cloudwatch-logs') |
| 8 | + |
3 | 9 | class CloudWatchLogs {
|
4 |
| - constructor (aws, region) { |
5 |
| - // Authenticated `aws` object in `lib/main.js` |
6 |
| - this.lambda = new aws.Lambda({ |
7 |
| - region, |
8 |
| - apiVersion: '2015-03-31' |
9 |
| - }) |
10 |
| - this.cloudwatchlogs = new aws.CloudWatchLogs({ |
11 |
| - apiVersion: '2014-03-28' |
12 |
| - }) |
| 10 | + constructor (config) { |
| 11 | + this.client = new CloudWatchLogsClient(config) |
13 | 12 | }
|
14 | 13 |
|
15 | 14 | _logGroupName (params) {
|
16 | 15 | return `/aws/lambda/${params.FunctionName}`
|
17 | 16 | }
|
18 | 17 |
|
19 | 18 | _createLogGroup (params) {
|
20 |
| - return new Promise((resolve, reject) => { |
21 |
| - this.cloudwatchlogs.createLogGroup({ |
| 19 | + try { |
| 20 | + return this.client.send(new CreateLogGroupCommand({ |
22 | 21 | 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 |
| - }) |
| 22 | + })) |
| 23 | + } catch (err) { |
| 24 | + if (err.code === 'ResourceAlreadyExistsException') { |
| 25 | + // If it exists it will result in an error but there is no problem. |
| 26 | + return {} |
| 27 | + } |
| 28 | + throw err |
| 29 | + } |
35 | 30 | }
|
36 | 31 |
|
37 | 32 | _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 |
| - }) |
| 33 | + return this.client.send(new PutRetentionPolicyCommand({ |
| 34 | + logGroupName: this._logGroupName(params), |
| 35 | + retentionInDays: params.retentionInDays |
| 36 | + })) |
47 | 37 | }
|
48 | 38 |
|
49 | 39 | setLogsRetentionPolicy (params) {
|
|
0 commit comments