node-lambda 1.2.1 → 2.0.0-alpha.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/workflows/codeql-analysis.yml +3 -3
- package/.github/workflows/workflow.yml +2 -2
- package/CHANGELOG.md +8 -0
- package/README.md +2 -2
- package/bin/node-lambda +1 -1
- package/index_mjs.mjs +9 -0
- package/lib/.env.example +2 -1
- package/lib/aws.js +41 -38
- package/lib/cloudwatch_logs.js +22 -32
- package/lib/main.js +155 -170
- package/lib/s3_deploy.js +18 -24
- package/lib/s3_events.js +16 -27
- package/lib/schedule_events.js +21 -35
- package/package.json +12 -9
- package/tea.yaml +6 -0
- package/test/cloudwatch_logs.js +17 -13
- package/test/main.js +93 -61
- package/test/node-lambda.js +69 -10
- package/test/s3_deploy.js +15 -15
- package/test/s3_events.js +15 -17
- package/test/schedule_events.js +15 -17
package/lib/main.js
CHANGED
|
@@ -3,14 +3,13 @@
|
|
|
3
3
|
const process = require('process')
|
|
4
4
|
const path = require('path')
|
|
5
5
|
const os = require('os')
|
|
6
|
-
const aws = require(path.join(__dirname, 'aws'))
|
|
7
6
|
const { exec, execSync, execFile } = require('child_process')
|
|
8
7
|
const fs = require('fs-extra')
|
|
9
8
|
const klaw = require('klaw')
|
|
10
9
|
const packageJson = require(path.join(__dirname, '..', 'package.json'))
|
|
11
10
|
const { minimatch } = require('minimatch')
|
|
12
11
|
const archiver = require('archiver')
|
|
13
|
-
const dotenv = require('
|
|
12
|
+
const dotenv = require('@dotenvx/dotenvx')
|
|
14
13
|
const ScheduleEvents = require(path.join(__dirname, 'schedule_events'))
|
|
15
14
|
const S3Events = require(path.join(__dirname, 's3_events'))
|
|
16
15
|
const S3Deploy = require(path.join(__dirname, 's3_deploy'))
|
|
@@ -19,6 +18,29 @@ const CloudWatchLogs = require(path.join(__dirname, 'cloudwatch_logs'))
|
|
|
19
18
|
const AWSXRay = require('aws-xray-sdk-core')
|
|
20
19
|
const { createNamespace } = require('continuation-local-storage')
|
|
21
20
|
|
|
21
|
+
/*
|
|
22
|
+
* Migrating to v3.
|
|
23
|
+
*
|
|
24
|
+
* # About retry
|
|
25
|
+
* By default, each client already has a default retry strategy.
|
|
26
|
+
* The default retry count is 3, and only retryable errors will be retried.
|
|
27
|
+
* https://docs.aws.amazon.com/sdkref/latest/guide/feature-retry-behavior.html
|
|
28
|
+
*/
|
|
29
|
+
const {
|
|
30
|
+
LambdaClient,
|
|
31
|
+
CreateEventSourceMappingCommand,
|
|
32
|
+
CreateFunctionCommand,
|
|
33
|
+
DeleteEventSourceMappingCommand,
|
|
34
|
+
GetFunctionCommand,
|
|
35
|
+
ListEventSourceMappingsCommand,
|
|
36
|
+
ListTagsCommand,
|
|
37
|
+
TagResourceCommand,
|
|
38
|
+
UntagResourceCommand,
|
|
39
|
+
UpdateEventSourceMappingCommand,
|
|
40
|
+
UpdateFunctionCodeCommand,
|
|
41
|
+
UpdateFunctionConfigurationCommand
|
|
42
|
+
} = require('@aws-sdk/client-lambda')
|
|
43
|
+
|
|
22
44
|
const maxBufferSize = 50 * 1024 * 1024
|
|
23
45
|
|
|
24
46
|
class Lambda {
|
|
@@ -52,14 +74,25 @@ event_sources.json and ${program.eventFile} files as needed.`)
|
|
|
52
74
|
}
|
|
53
75
|
|
|
54
76
|
async run (program) {
|
|
55
|
-
if (!['
|
|
77
|
+
if (!['nodejs16.x', 'nodejs18.x', 'nodejs20.x'].includes(program.runtime)) {
|
|
56
78
|
console.error(`Runtime [${program.runtime}] is not supported.`)
|
|
57
79
|
process.exit(254)
|
|
58
80
|
}
|
|
59
81
|
|
|
60
82
|
this._createSampleFile(program.eventFile, 'event.json')
|
|
61
83
|
const splitHandler = program.handler.split('.')
|
|
62
|
-
const filename =
|
|
84
|
+
const filename = (() => {
|
|
85
|
+
for (const extension of ['.js', '.mjs']) {
|
|
86
|
+
if (fs.existsSync(splitHandler[0] + extension)) {
|
|
87
|
+
return splitHandler[0] + extension
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
})()
|
|
91
|
+
if (filename == null) {
|
|
92
|
+
console.error('Handler file not found.')
|
|
93
|
+
process.exitCode = 255
|
|
94
|
+
return
|
|
95
|
+
}
|
|
63
96
|
const handlername = splitHandler[1]
|
|
64
97
|
|
|
65
98
|
// Set custom environment variables if program.configFile is defined
|
|
@@ -225,8 +258,8 @@ Emulate only the body of the API Gateway event.
|
|
|
225
258
|
Role: program.role,
|
|
226
259
|
Runtime: program.runtime,
|
|
227
260
|
Description: program.description,
|
|
228
|
-
MemorySize: program.memorySize,
|
|
229
|
-
Timeout: program.timeout,
|
|
261
|
+
MemorySize: Number(program.memorySize),
|
|
262
|
+
Timeout: Number(program.timeout),
|
|
230
263
|
Architectures: program.architecture ? [program.architecture] : ['x86_64'],
|
|
231
264
|
Publish: (() => {
|
|
232
265
|
if (typeof program.publish === 'boolean') {
|
|
@@ -650,43 +683,24 @@ Emulate only the body of the API Gateway event.
|
|
|
650
683
|
delete functionConfigParams.Layers
|
|
651
684
|
}
|
|
652
685
|
|
|
653
|
-
const
|
|
654
|
-
updateConfigRequest.on('retry', (response) => {
|
|
655
|
-
console.log(response.error.message)
|
|
656
|
-
console.log('=> Retrying')
|
|
657
|
-
})
|
|
658
|
-
const updateConfigResponse = await updateConfigRequest.promise()
|
|
686
|
+
const updateConfigResponse = await lambda.send(new UpdateFunctionConfigurationCommand(functionConfigParams))
|
|
659
687
|
|
|
660
688
|
// Wait for the `Configuration.LastUpdateStatus` to change from `InProgress` to `Successful`.
|
|
689
|
+
const getFunction = new GetFunctionCommand({ FunctionName: params.FunctionName })
|
|
661
690
|
for (let i = 0; i < 10; i++) {
|
|
662
|
-
const data = await lambda.getFunction
|
|
691
|
+
const data = await lambda.send(getFunction)
|
|
663
692
|
if (data.Configuration.LastUpdateStatus === 'Successful') {
|
|
664
693
|
break
|
|
665
694
|
}
|
|
666
695
|
await new Promise((resolve) => setTimeout(resolve, 3000))
|
|
667
696
|
}
|
|
668
|
-
|
|
669
|
-
const updateCodeRequest = lambda.updateFunctionCode(functionCodeParams)
|
|
670
|
-
updateCodeRequest.on('retry', (response) => {
|
|
671
|
-
console.log(response.error.message)
|
|
672
|
-
console.log('=> Retrying')
|
|
673
|
-
})
|
|
674
|
-
await updateCodeRequest.promise()
|
|
697
|
+
lambda.send(new UpdateFunctionCodeCommand(functionCodeParams))
|
|
675
698
|
|
|
676
699
|
return updateConfigResponse
|
|
677
700
|
}
|
|
678
701
|
|
|
679
702
|
_uploadNew (lambda, params) {
|
|
680
|
-
return new
|
|
681
|
-
const request = lambda.createFunction(params, (err, data) => {
|
|
682
|
-
if (err) return reject(err)
|
|
683
|
-
resolve(data)
|
|
684
|
-
})
|
|
685
|
-
request.on('retry', (response) => {
|
|
686
|
-
console.log(response.error.message)
|
|
687
|
-
console.log('=> Retrying')
|
|
688
|
-
})
|
|
689
|
-
})
|
|
703
|
+
return lambda.send(new CreateFunctionCommand(params))
|
|
690
704
|
}
|
|
691
705
|
|
|
692
706
|
_readArchive (program) {
|
|
@@ -758,16 +772,12 @@ they may not work as expected in the Lambda environment.
|
|
|
758
772
|
return this._zip(program, codeDirectory)
|
|
759
773
|
}
|
|
760
774
|
|
|
761
|
-
_listEventSourceMappings (lambda, params) {
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
}
|
|
768
|
-
return resolve([])
|
|
769
|
-
})
|
|
770
|
-
})
|
|
775
|
+
async _listEventSourceMappings (lambda, params) {
|
|
776
|
+
const data = await lambda.send(new ListEventSourceMappingsCommand(params))
|
|
777
|
+
if (data && data.EventSourceMappings) {
|
|
778
|
+
return data.EventSourceMappings
|
|
779
|
+
}
|
|
780
|
+
return []
|
|
771
781
|
}
|
|
772
782
|
|
|
773
783
|
_getStartingPosition (eventSource) {
|
|
@@ -835,48 +845,30 @@ they may not work as expected in the Lambda environment.
|
|
|
835
845
|
switch (updateEventSource.type) {
|
|
836
846
|
case 'create':
|
|
837
847
|
delete updateEventSource.type
|
|
838
|
-
return new
|
|
839
|
-
lambda.createEventSourceMapping(updateEventSource, (err, data) => {
|
|
840
|
-
if (err) return reject(err)
|
|
841
|
-
resolve(data)
|
|
842
|
-
})
|
|
843
|
-
})
|
|
848
|
+
return lambda.send(new CreateEventSourceMappingCommand(updateEventSource))
|
|
844
849
|
case 'update':
|
|
845
850
|
delete updateEventSource.type
|
|
846
|
-
return new
|
|
847
|
-
lambda.updateEventSourceMapping(updateEventSource, (err, data) => {
|
|
848
|
-
if (err) return reject(err)
|
|
849
|
-
resolve(data)
|
|
850
|
-
})
|
|
851
|
-
})
|
|
851
|
+
return lambda.send(new UpdateEventSourceMappingCommand(updateEventSource))
|
|
852
852
|
case 'delete':
|
|
853
853
|
delete updateEventSource.type
|
|
854
|
-
return new
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
resolve(data)
|
|
858
|
-
})
|
|
859
|
-
})
|
|
854
|
+
return lambda.send(new DeleteEventSourceMappingCommand(updateEventSource))
|
|
855
|
+
default:
|
|
856
|
+
return Promise.resolve()
|
|
860
857
|
}
|
|
861
|
-
return Promise.resolve()
|
|
862
858
|
}))
|
|
863
859
|
}
|
|
864
860
|
|
|
865
|
-
_updateTags (lambda, functionArn, tags) {
|
|
861
|
+
async _updateTags (lambda, functionArn, tags) {
|
|
866
862
|
if (!tags || Object.keys(tags).length <= 0) {
|
|
867
|
-
return
|
|
868
|
-
} else {
|
|
869
|
-
return lambda.listTags({ Resource: functionArn }).promise()
|
|
870
|
-
.then(data => {
|
|
871
|
-
const keys = Object.keys(data.Tags)
|
|
872
|
-
return keys && keys.length > 0
|
|
873
|
-
? lambda.untagResource({ Resource: functionArn, TagKeys: keys }).promise()
|
|
874
|
-
: Promise.resolve()
|
|
875
|
-
})
|
|
876
|
-
.then(() => {
|
|
877
|
-
return lambda.tagResource({ Resource: functionArn, Tags: tags }).promise()
|
|
878
|
-
})
|
|
863
|
+
return []
|
|
879
864
|
}
|
|
865
|
+
|
|
866
|
+
const data = await lambda.send(new ListTagsCommand({ Resource: functionArn }))
|
|
867
|
+
const keys = Object.keys(data.Tags)
|
|
868
|
+
if (keys && keys.length > 0) {
|
|
869
|
+
await lambda.send(new UntagResourceCommand({ Resource: functionArn, TagKeys: keys }))
|
|
870
|
+
}
|
|
871
|
+
return lambda.send(new TagResourceCommand({ Resource: functionArn, Tags: tags }))
|
|
880
872
|
}
|
|
881
873
|
|
|
882
874
|
_updateScheduleEvents (scheduleEvents, functionArn, scheduleList) {
|
|
@@ -962,115 +954,108 @@ they may not work as expected in the Lambda environment.
|
|
|
962
954
|
!!err.message.match(/^Function not found:/)
|
|
963
955
|
}
|
|
964
956
|
|
|
965
|
-
_deployToRegion (program, params, region, buffer) {
|
|
966
|
-
aws.updateConfig
|
|
957
|
+
async _deployToRegion (program, params, region, buffer) {
|
|
958
|
+
// sdk v3 todo: Migration of aws.updateConfig.
|
|
959
|
+
// aws.updateConfig(program, region)
|
|
960
|
+
const config = { region }
|
|
967
961
|
|
|
968
962
|
console.log('=> Reading event source file to memory')
|
|
969
963
|
const eventSourceList = this._eventSourceList(program)
|
|
970
964
|
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
}
|
|
978
|
-
|
|
979
|
-
}).then(() => {
|
|
980
|
-
if (!this._isUseS3(program)) {
|
|
981
|
-
console.log(`=> Uploading zip file to AWS Lambda ${region} with parameters:`)
|
|
982
|
-
} else {
|
|
983
|
-
console.log(`=> Uploading AWS Lambda ${region} with parameters:`)
|
|
984
|
-
}
|
|
985
|
-
console.log(params)
|
|
965
|
+
if (this._isUseS3(program)) {
|
|
966
|
+
const s3Deploy = new S3Deploy(config)
|
|
967
|
+
params.Code = await s3Deploy.putPackage(params, region, buffer)
|
|
968
|
+
console.log(`=> Uploading AWS Lambda ${region} with parameters:`)
|
|
969
|
+
} else {
|
|
970
|
+
console.log(`=> Uploading zip file to AWS Lambda ${region} with parameters:`)
|
|
971
|
+
}
|
|
972
|
+
console.log(params)
|
|
986
973
|
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
const scheduleEvents = new ScheduleEvents(aws.sdk, region)
|
|
992
|
-
const s3Events = new S3Events(aws.sdk, region)
|
|
993
|
-
const cloudWatchLogs = new CloudWatchLogs(aws.sdk, region)
|
|
974
|
+
const lambdaClient = new LambdaClient(config)
|
|
975
|
+
const scheduleEvents = new ScheduleEvents(config)
|
|
976
|
+
const s3Events = new S3Events(config)
|
|
977
|
+
const cloudWatchLogs = new CloudWatchLogs(config)
|
|
994
978
|
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
}).promise().then(() => {
|
|
999
|
-
// Function exists
|
|
1000
|
-
return this._listEventSourceMappings(lambda, {
|
|
979
|
+
const existsFunction = await (async () => {
|
|
980
|
+
try {
|
|
981
|
+
await lambdaClient.send(new GetFunctionCommand({
|
|
1001
982
|
FunctionName: params.FunctionName
|
|
1002
|
-
})
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
console.log('=> Done uploading. Results follow: ')
|
|
1006
|
-
console.log(results)
|
|
1007
|
-
return results
|
|
1008
|
-
}).then(results => {
|
|
1009
|
-
return Promise.all([
|
|
1010
|
-
this._updateScheduleEvents(
|
|
1011
|
-
scheduleEvents,
|
|
1012
|
-
results.FunctionArn,
|
|
1013
|
-
eventSourceList.ScheduleEvents
|
|
1014
|
-
),
|
|
1015
|
-
this._updateS3Events(
|
|
1016
|
-
s3Events,
|
|
1017
|
-
results.FunctionArn,
|
|
1018
|
-
eventSourceList.S3Events
|
|
1019
|
-
),
|
|
1020
|
-
this._updateTags(
|
|
1021
|
-
lambda,
|
|
1022
|
-
results.FunctionArn,
|
|
1023
|
-
params.Tags)
|
|
1024
|
-
])
|
|
1025
|
-
}),
|
|
1026
|
-
this._updateEventSources(
|
|
1027
|
-
lambda,
|
|
1028
|
-
params.FunctionName,
|
|
1029
|
-
existingEventSourceList,
|
|
1030
|
-
eventSourceList.EventSourceMappings
|
|
1031
|
-
),
|
|
1032
|
-
this._setLogsRetentionPolicy(
|
|
1033
|
-
cloudWatchLogs,
|
|
1034
|
-
program,
|
|
1035
|
-
params.FunctionName
|
|
1036
|
-
)
|
|
1037
|
-
])
|
|
1038
|
-
})
|
|
1039
|
-
}).catch((err) => {
|
|
983
|
+
}))
|
|
984
|
+
return true
|
|
985
|
+
} catch (err) {
|
|
1040
986
|
if (!this._isFunctionDoesNotExist(err)) {
|
|
1041
987
|
throw err
|
|
1042
988
|
}
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
lambda,
|
|
1051
|
-
params.FunctionName,
|
|
1052
|
-
[],
|
|
1053
|
-
eventSourceList.EventSourceMappings
|
|
1054
|
-
),
|
|
1055
|
-
this._updateScheduleEvents(
|
|
1056
|
-
scheduleEvents,
|
|
1057
|
-
results.FunctionArn,
|
|
1058
|
-
eventSourceList.ScheduleEvents
|
|
1059
|
-
),
|
|
1060
|
-
this._updateS3Events(
|
|
1061
|
-
s3Events,
|
|
1062
|
-
results.FunctionArn,
|
|
1063
|
-
eventSourceList.S3Events
|
|
1064
|
-
),
|
|
1065
|
-
this._setLogsRetentionPolicy(
|
|
1066
|
-
cloudWatchLogs,
|
|
1067
|
-
program,
|
|
1068
|
-
params.FunctionName
|
|
1069
|
-
)
|
|
1070
|
-
])
|
|
1071
|
-
})
|
|
989
|
+
return false
|
|
990
|
+
}
|
|
991
|
+
})()
|
|
992
|
+
|
|
993
|
+
if (existsFunction) {
|
|
994
|
+
const existingEventSourceList = await this._listEventSourceMappings(lambdaClient, {
|
|
995
|
+
FunctionName: params.FunctionName
|
|
1072
996
|
})
|
|
1073
|
-
|
|
997
|
+
const results = await this._uploadExisting(lambdaClient, params)
|
|
998
|
+
console.log('=> Done uploading. Results follow: ')
|
|
999
|
+
console.log(results)
|
|
1000
|
+
|
|
1001
|
+
return Promise.all([
|
|
1002
|
+
Promise.all([
|
|
1003
|
+
this._updateScheduleEvents(
|
|
1004
|
+
scheduleEvents,
|
|
1005
|
+
results.FunctionArn,
|
|
1006
|
+
eventSourceList.ScheduleEvents
|
|
1007
|
+
),
|
|
1008
|
+
this._updateS3Events(
|
|
1009
|
+
s3Events,
|
|
1010
|
+
results.FunctionArn,
|
|
1011
|
+
eventSourceList.S3Events
|
|
1012
|
+
),
|
|
1013
|
+
this._updateTags(
|
|
1014
|
+
lambdaClient,
|
|
1015
|
+
results.FunctionArn,
|
|
1016
|
+
params.Tags)
|
|
1017
|
+
]),
|
|
1018
|
+
this._updateEventSources(
|
|
1019
|
+
lambdaClient,
|
|
1020
|
+
params.FunctionName,
|
|
1021
|
+
existingEventSourceList,
|
|
1022
|
+
eventSourceList.EventSourceMappings
|
|
1023
|
+
),
|
|
1024
|
+
this._setLogsRetentionPolicy(
|
|
1025
|
+
cloudWatchLogs,
|
|
1026
|
+
program,
|
|
1027
|
+
params.FunctionName
|
|
1028
|
+
)
|
|
1029
|
+
])
|
|
1030
|
+
} else {
|
|
1031
|
+
const results = await this._uploadNew(lambdaClient, params)
|
|
1032
|
+
console.log('=> Done uploading. Results follow: ')
|
|
1033
|
+
console.log(results)
|
|
1034
|
+
|
|
1035
|
+
return Promise.all([
|
|
1036
|
+
this._updateEventSources(
|
|
1037
|
+
lambdaClient,
|
|
1038
|
+
params.FunctionName,
|
|
1039
|
+
[],
|
|
1040
|
+
eventSourceList.EventSourceMappings
|
|
1041
|
+
),
|
|
1042
|
+
this._updateScheduleEvents(
|
|
1043
|
+
scheduleEvents,
|
|
1044
|
+
results.FunctionArn,
|
|
1045
|
+
eventSourceList.ScheduleEvents
|
|
1046
|
+
),
|
|
1047
|
+
this._updateS3Events(
|
|
1048
|
+
s3Events,
|
|
1049
|
+
results.FunctionArn,
|
|
1050
|
+
eventSourceList.S3Events
|
|
1051
|
+
),
|
|
1052
|
+
this._setLogsRetentionPolicy(
|
|
1053
|
+
cloudWatchLogs,
|
|
1054
|
+
program,
|
|
1055
|
+
params.FunctionName
|
|
1056
|
+
)
|
|
1057
|
+
])
|
|
1058
|
+
}
|
|
1074
1059
|
}
|
|
1075
1060
|
|
|
1076
1061
|
_printDeployResults (results, isFirst) {
|
package/lib/s3_deploy.js
CHANGED
|
@@ -2,6 +2,13 @@
|
|
|
2
2
|
|
|
3
3
|
const process = require('process')
|
|
4
4
|
const crypto = require('crypto')
|
|
5
|
+
|
|
6
|
+
const {
|
|
7
|
+
S3Client,
|
|
8
|
+
CreateBucketCommand,
|
|
9
|
+
PutObjectCommand
|
|
10
|
+
} = require('@aws-sdk/client-s3')
|
|
11
|
+
|
|
5
12
|
// https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#createBucket-property
|
|
6
13
|
const S3_LOCATION_POSSIBLE_VALUES = [
|
|
7
14
|
'EU',
|
|
@@ -33,12 +40,8 @@ const S3_LOCATION_POSSIBLE_VALUES = [
|
|
|
33
40
|
]
|
|
34
41
|
|
|
35
42
|
class S3Deploy {
|
|
36
|
-
constructor (
|
|
37
|
-
|
|
38
|
-
this.s3 = new aws.S3({
|
|
39
|
-
region,
|
|
40
|
-
apiVersion: '2006-03-01'
|
|
41
|
-
})
|
|
43
|
+
constructor (config) {
|
|
44
|
+
this.client = new S3Client(config)
|
|
42
45
|
}
|
|
43
46
|
|
|
44
47
|
_md5 (str) {
|
|
@@ -107,30 +110,21 @@ class S3Deploy {
|
|
|
107
110
|
LocationConstraint: s3Location
|
|
108
111
|
}
|
|
109
112
|
}
|
|
110
|
-
|
|
111
|
-
this.
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
resolve(data)
|
|
118
|
-
})
|
|
119
|
-
})
|
|
113
|
+
try {
|
|
114
|
+
return this.client.send(new CreateBucketCommand(_params))
|
|
115
|
+
} catch (err) {
|
|
116
|
+
// Ignored created
|
|
117
|
+
if (err.code === 'BucketAlreadyOwnedByYou') return {}
|
|
118
|
+
throw err
|
|
119
|
+
}
|
|
120
120
|
}
|
|
121
121
|
|
|
122
122
|
_putObject (params, buffer) {
|
|
123
|
-
|
|
123
|
+
return this.client.send(new PutObjectCommand({
|
|
124
124
|
Body: buffer,
|
|
125
125
|
Bucket: params.bucketName,
|
|
126
126
|
Key: params.s3Key
|
|
127
|
-
}
|
|
128
|
-
return new Promise((resolve, reject) => {
|
|
129
|
-
this.s3.putObject(_params, (err, data) => {
|
|
130
|
-
if (err) reject(err)
|
|
131
|
-
resolve(data)
|
|
132
|
-
})
|
|
133
|
-
})
|
|
127
|
+
}))
|
|
134
128
|
}
|
|
135
129
|
|
|
136
130
|
putPackage (params, region, buffer) {
|
package/lib/s3_events.js
CHANGED
|
@@ -1,20 +1,16 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
|
+
const { S3Client, PutBucketNotificationConfigurationCommand } = require('@aws-sdk/client-s3')
|
|
4
|
+
const { LambdaClient, AddPermissionCommand } = require('@aws-sdk/client-lambda')
|
|
5
|
+
|
|
3
6
|
/**
|
|
4
7
|
* Do not create S3 bucket.
|
|
5
8
|
* Put the Notification Configuration in the existing Bucket.
|
|
6
9
|
*/
|
|
7
10
|
class S3Events {
|
|
8
|
-
constructor (
|
|
9
|
-
|
|
10
|
-
this.
|
|
11
|
-
region,
|
|
12
|
-
apiVersion: '2015-03-31'
|
|
13
|
-
})
|
|
14
|
-
this.s3 = new aws.S3({
|
|
15
|
-
region,
|
|
16
|
-
apiVersion: '2006-03-01'
|
|
17
|
-
})
|
|
11
|
+
constructor (config) {
|
|
12
|
+
this.s3Client = new S3Client(config)
|
|
13
|
+
this.lambdaClient = new LambdaClient(config)
|
|
18
14
|
}
|
|
19
15
|
|
|
20
16
|
_functionName (params) {
|
|
@@ -36,17 +32,14 @@ class S3Events {
|
|
|
36
32
|
}
|
|
37
33
|
|
|
38
34
|
_addPermission (params) {
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
this.
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
resolve(data)
|
|
48
|
-
})
|
|
49
|
-
})
|
|
35
|
+
const _params = this._addPermissionParams(params)
|
|
36
|
+
try {
|
|
37
|
+
return this.lambdaClient.send(new AddPermissionCommand(_params))
|
|
38
|
+
} catch (err) {
|
|
39
|
+
if (err.code !== 'ResourceConflictException') throw err
|
|
40
|
+
// If it exists it will result in an error but there is no problem.
|
|
41
|
+
return 'Permission already set'
|
|
42
|
+
}
|
|
50
43
|
}
|
|
51
44
|
|
|
52
45
|
_lambdaFunctionConfiguration (params) {
|
|
@@ -86,12 +79,8 @@ class S3Events {
|
|
|
86
79
|
}
|
|
87
80
|
|
|
88
81
|
_putBucketNotificationConfiguration (putBucketNotificationConfigurationParams) {
|
|
89
|
-
return
|
|
90
|
-
|
|
91
|
-
if (err) reject(err)
|
|
92
|
-
resolve(data)
|
|
93
|
-
})
|
|
94
|
-
})
|
|
82
|
+
return this.s3Client.send(
|
|
83
|
+
new PutBucketNotificationConfigurationCommand(putBucketNotificationConfigurationParams))
|
|
95
84
|
}
|
|
96
85
|
|
|
97
86
|
add (paramsList) {
|
package/lib/schedule_events.js
CHANGED
|
@@ -1,15 +1,12 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
|
+
const { CloudWatchEventsClient, PutRuleCommand, PutTargetsCommand } = require('@aws-sdk/client-cloudwatch-events')
|
|
4
|
+
const { LambdaClient, AddPermissionCommand } = require('@aws-sdk/client-lambda')
|
|
5
|
+
|
|
3
6
|
class ScheduleEvents {
|
|
4
|
-
constructor (
|
|
5
|
-
|
|
6
|
-
this.
|
|
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)
|
|
13
10
|
}
|
|
14
11
|
|
|
15
12
|
_ruleDescription (params) {
|
|
@@ -34,13 +31,9 @@ class ScheduleEvents {
|
|
|
34
31
|
|
|
35
32
|
_putRule (params) {
|
|
36
33
|
// return RuleArn if created
|
|
37
|
-
return
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
if (err) reject(err)
|
|
41
|
-
resolve(rule)
|
|
42
|
-
})
|
|
43
|
-
})
|
|
34
|
+
return this.cweClient.send(
|
|
35
|
+
new PutRuleCommand(this._putRulePrams(params))
|
|
36
|
+
)
|
|
44
37
|
}
|
|
45
38
|
|
|
46
39
|
_addPermissionParams (params) {
|
|
@@ -54,17 +47,14 @@ class ScheduleEvents {
|
|
|
54
47
|
}
|
|
55
48
|
|
|
56
49
|
_addPermission (params) {
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
this.
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
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
|
+
}
|
|
68
58
|
}
|
|
69
59
|
|
|
70
60
|
_putTargetsParams (params) {
|
|
@@ -79,14 +69,10 @@ class ScheduleEvents {
|
|
|
79
69
|
}
|
|
80
70
|
|
|
81
71
|
_putTargets (params) {
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
this.
|
|
85
|
-
|
|
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
|
+
)
|
|
90
76
|
}
|
|
91
77
|
|
|
92
78
|
add (params) {
|