Skip to content

Commit a02adef

Browse files
authored
Merge pull request motdotla#792 from abetomo/migration-to-aws-sdk-v3-schedule_events
sdk-v3: Migrate lib/schedule_events to V3
2 parents 732bc69 + ab6e069 commit a02adef

File tree

7 files changed

+8730
-6594
lines changed

7 files changed

+8730
-6594
lines changed

lib/main.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -973,8 +973,7 @@ they may not work as expected in the Lambda environment.
973973
console.log(params)
974974

975975
const lambdaClient = new LambdaClient(config)
976-
// Migrating to v3.
977-
const scheduleEvents = new ScheduleEvents(aws.sdk, region)
976+
const scheduleEvents = new ScheduleEvents(config)
978977
const s3Events = new S3Events(config)
979978
const cloudWatchLogs = new CloudWatchLogs(config)
980979

lib/schedule_events.js

Lines changed: 21 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
'use strict'
22

3+
const { CloudWatchEventsClient, PutRuleCommand, PutTargetsCommand } = require('@aws-sdk/client-cloudwatch-events')
4+
const { LambdaClient, AddPermissionCommand } = require('@aws-sdk/client-lambda')
5+
36
class ScheduleEvents {
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.cloudwatchevents = new aws.CloudWatchEvents({
11-
apiVersion: '2015-10-07'
12-
})
7+
constructor (config) {
8+
this.cweClient = new CloudWatchEventsClient(config)
9+
this.lambdaClient = new LambdaClient(config)
1310
}
1411

1512
_ruleDescription (params) {
@@ -34,13 +31,9 @@ class ScheduleEvents {
3431

3532
_putRule (params) {
3633
// return RuleArn if created
37-
return new Promise((resolve, reject) => {
38-
const _params = this._putRulePrams(params)
39-
this.cloudwatchevents.putRule(_params, (err, rule) => {
40-
if (err) reject(err)
41-
resolve(rule)
42-
})
43-
})
34+
return this.cweClient.send(
35+
new PutRuleCommand(this._putRulePrams(params))
36+
)
4437
}
4538

4639
_addPermissionParams (params) {
@@ -54,17 +47,14 @@ class ScheduleEvents {
5447
}
5548

5649
_addPermission (params) {
57-
return new Promise((resolve, reject) => {
58-
const _params = this._addPermissionParams(params)
59-
this.lambda.addPermission(_params, (err, data) => {
60-
if (err) {
61-
if (err.code !== 'ResourceConflictException') reject(err)
62-
// If it exists it will result in an error but there is no problem.
63-
resolve('Permission already set')
64-
}
65-
resolve(data)
66-
})
67-
})
50+
const _params = this._addPermissionParams(params)
51+
try {
52+
return this.lambdaClient.send(new AddPermissionCommand(_params))
53+
} catch (err) {
54+
if (err.code !== 'ResourceConflictException') throw err
55+
// If it exists it will result in an error but there is no problem.
56+
return 'Permission already set'
57+
}
6858
}
6959

7060
_putTargetsParams (params) {
@@ -79,14 +69,10 @@ class ScheduleEvents {
7969
}
8070

8171
_putTargets (params) {
82-
return new Promise((resolve, reject) => {
83-
const _params = this._putTargetsParams(params)
84-
this.cloudwatchevents.putTargets(_params, (err, data) => {
85-
// even if it is already registered, it will not be an error.
86-
if (err) reject(err)
87-
resolve(data)
88-
})
89-
})
72+
// even if it is already registered, it will not be an error.
73+
return this.cweClient.send(
74+
new PutTargetsCommand(this._putTargetsParams(params))
75+
)
9076
}
9177

9278
add (params) {

0 commit comments

Comments
 (0)