Skip to content

Commit 9d4e464

Browse files
committed
sdk-v3: Migrate CloudwatchLogs to V3
motdotlaGH-641
1 parent f890d2f commit 9d4e464

File tree

7 files changed

+9167
-5129
lines changed

7 files changed

+9167
-5129
lines changed

Diff for: lib/cloudwatch_logs.js

+22-32
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,39 @@
11
'use strict'
22

3+
const {
4+
CloudWatchLogsClient,
5+
CreateLogGroupCommand,
6+
PutRetentionPolicyCommand
7+
} = require('@aws-sdk/client-cloudwatch-logs')
8+
39
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)
1312
}
1413

1514
_logGroupName (params) {
1615
return `/aws/lambda/${params.FunctionName}`
1716
}
1817

1918
_createLogGroup (params) {
20-
return new Promise((resolve, reject) => {
21-
this.cloudwatchlogs.createLogGroup({
19+
try {
20+
return this.client.send(new CreateLogGroupCommand({
2221
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+
}
3530
}
3631

3732
_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+
}))
4737
}
4838

4939
setLogsRetentionPolicy (params) {

Diff for: lib/main.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -971,11 +971,12 @@ they may not work as expected in the Lambda environment.
971971
}
972972
console.log(params)
973973

974-
const lambdaClient = new LambdaClient({ region })
974+
const config = { region }
975+
const lambdaClient = new LambdaClient(config)
975976
// Migrating to v3.
976977
const scheduleEvents = new ScheduleEvents(aws.sdk, region)
977978
const s3Events = new S3Events(aws.sdk, region)
978-
const cloudWatchLogs = new CloudWatchLogs(aws.sdk, region)
979+
const cloudWatchLogs = new CloudWatchLogs(config)
979980

980981
const existsFunction = await (async () => {
981982
try {

0 commit comments

Comments
 (0)