Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -973,8 +973,7 @@ they may not work as expected in the Lambda environment.
console.log(params)

const lambdaClient = new LambdaClient(config)
// Migrating to v3.
const scheduleEvents = new ScheduleEvents(aws.sdk, region)
const scheduleEvents = new ScheduleEvents(config)
const s3Events = new S3Events(config)
const cloudWatchLogs = new CloudWatchLogs(config)

Expand Down
56 changes: 21 additions & 35 deletions lib/schedule_events.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
'use strict'

const { CloudWatchEventsClient, PutRuleCommand, PutTargetsCommand } = require('@aws-sdk/client-cloudwatch-events')
const { LambdaClient, AddPermissionCommand } = require('@aws-sdk/client-lambda')

class ScheduleEvents {
constructor (aws, region) {
// Authenticated `aws` object in `lib/main.js`
this.lambda = new aws.Lambda({
region,
apiVersion: '2015-03-31'
})
this.cloudwatchevents = new aws.CloudWatchEvents({
apiVersion: '2015-10-07'
})
constructor (config) {
this.cweClient = new CloudWatchEventsClient(config)
this.lambdaClient = new LambdaClient(config)
}

_ruleDescription (params) {
Expand All @@ -34,13 +31,9 @@ class ScheduleEvents {

_putRule (params) {
// return RuleArn if created
return new Promise((resolve, reject) => {
const _params = this._putRulePrams(params)
this.cloudwatchevents.putRule(_params, (err, rule) => {
if (err) reject(err)
resolve(rule)
})
})
return this.cweClient.send(
new PutRuleCommand(this._putRulePrams(params))
)
}

_addPermissionParams (params) {
Expand All @@ -54,17 +47,14 @@ class ScheduleEvents {
}

_addPermission (params) {
return new Promise((resolve, reject) => {
const _params = this._addPermissionParams(params)
this.lambda.addPermission(_params, (err, data) => {
if (err) {
if (err.code !== 'ResourceConflictException') reject(err)
// If it exists it will result in an error but there is no problem.
resolve('Permission already set')
}
resolve(data)
})
})
const _params = this._addPermissionParams(params)
try {
return this.lambdaClient.send(new AddPermissionCommand(_params))
} catch (err) {
if (err.code !== 'ResourceConflictException') throw err
// If it exists it will result in an error but there is no problem.
return 'Permission already set'
}
}

_putTargetsParams (params) {
Expand All @@ -79,14 +69,10 @@ class ScheduleEvents {
}

_putTargets (params) {
return new Promise((resolve, reject) => {
const _params = this._putTargetsParams(params)
this.cloudwatchevents.putTargets(_params, (err, data) => {
// even if it is already registered, it will not be an error.
if (err) reject(err)
resolve(data)
})
})
// even if it is already registered, it will not be an error.
return this.cweClient.send(
new PutTargetsCommand(this._putTargetsParams(params))
)
}

add (params) {
Expand Down
Loading
Loading