Skip to content

Commit 3ab7e6b

Browse files
authored
Merge pull request #784 from abetomo/refactoring-deployregion
Refactoring _deployToRegion()
2 parents 0aa92e5 + d25ab21 commit 3ab7e6b

File tree

1 file changed

+91
-98
lines changed

1 file changed

+91
-98
lines changed

lib/main.js

+91-98
Original file line numberDiff line numberDiff line change
@@ -973,115 +973,108 @@ they may not work as expected in the Lambda environment.
973973
!!err.message.match(/^Function not found:/)
974974
}
975975

976-
_deployToRegion (program, params, region, buffer) {
976+
async _deployToRegion (program, params, region, buffer) {
977977
aws.updateConfig(program, region)
978978

979979
console.log('=> Reading event source file to memory')
980980
const eventSourceList = this._eventSourceList(program)
981981

982-
return Promise.resolve().then(() => {
983-
if (this._isUseS3(program)) {
984-
const s3Deploy = new S3Deploy(aws.sdk, region)
985-
return s3Deploy.putPackage(params, region, buffer)
986-
}
987-
return null
988-
}).then((code) => {
989-
if (code != null) params.Code = code
990-
}).then(() => {
991-
if (!this._isUseS3(program)) {
992-
console.log(`=> Uploading zip file to AWS Lambda ${region} with parameters:`)
993-
} else {
994-
console.log(`=> Uploading AWS Lambda ${region} with parameters:`)
995-
}
996-
console.log(params)
997-
998-
const lambda = new aws.sdk.Lambda({
999-
region,
1000-
apiVersion: '2015-03-31'
1001-
})
1002-
const scheduleEvents = new ScheduleEvents(aws.sdk, region)
1003-
const s3Events = new S3Events(aws.sdk, region)
1004-
const cloudWatchLogs = new CloudWatchLogs(aws.sdk, region)
982+
if (this._isUseS3(program)) {
983+
const s3Deploy = new S3Deploy(aws.sdk, region)
984+
params.Code = await s3Deploy.putPackage(params, region, buffer)
985+
console.log(`=> Uploading AWS Lambda ${region} with parameters:`)
986+
} else {
987+
console.log(`=> Uploading zip file to AWS Lambda ${region} with parameters:`)
988+
}
989+
console.log(params)
990+
const lambda = new aws.sdk.Lambda({
991+
region,
992+
apiVersion: '2015-03-31'
993+
})
994+
const scheduleEvents = new ScheduleEvents(aws.sdk, region)
995+
const s3Events = new S3Events(aws.sdk, region)
996+
const cloudWatchLogs = new CloudWatchLogs(aws.sdk, region)
1005997

1006-
// Checking function
1007-
return lambda.getFunction({
1008-
FunctionName: params.FunctionName
1009-
}).promise().then(() => {
1010-
// Function exists
1011-
return this._listEventSourceMappings(lambda, {
998+
const existsFunction = await (async () => {
999+
try {
1000+
await lambda.getFunction({
10121001
FunctionName: params.FunctionName
1013-
}).then((existingEventSourceList) => {
1014-
return Promise.all([
1015-
this._uploadExisting(lambda, params).then((results) => {
1016-
console.log('=> Done uploading. Results follow: ')
1017-
console.log(results)
1018-
return results
1019-
}).then(results => {
1020-
return Promise.all([
1021-
this._updateScheduleEvents(
1022-
scheduleEvents,
1023-
results.FunctionArn,
1024-
eventSourceList.ScheduleEvents
1025-
),
1026-
this._updateS3Events(
1027-
s3Events,
1028-
results.FunctionArn,
1029-
eventSourceList.S3Events
1030-
),
1031-
this._updateTags(
1032-
lambda,
1033-
results.FunctionArn,
1034-
params.Tags)
1035-
])
1036-
}),
1037-
this._updateEventSources(
1038-
lambda,
1039-
params.FunctionName,
1040-
existingEventSourceList,
1041-
eventSourceList.EventSourceMappings
1042-
),
1043-
this._setLogsRetentionPolicy(
1044-
cloudWatchLogs,
1045-
program,
1046-
params.FunctionName
1047-
)
1048-
])
1049-
})
1050-
}).catch((err) => {
1002+
}).promise()
1003+
return true
1004+
} catch (err) {
10511005
if (!this._isFunctionDoesNotExist(err)) {
10521006
throw err
10531007
}
1054-
// Function does not exist
1055-
return this._uploadNew(lambda, params).then((results) => {
1056-
console.log('=> Done uploading. Results follow: ')
1057-
console.log(results)
1058-
1059-
return Promise.all([
1060-
this._updateEventSources(
1061-
lambda,
1062-
params.FunctionName,
1063-
[],
1064-
eventSourceList.EventSourceMappings
1065-
),
1066-
this._updateScheduleEvents(
1067-
scheduleEvents,
1068-
results.FunctionArn,
1069-
eventSourceList.ScheduleEvents
1070-
),
1071-
this._updateS3Events(
1072-
s3Events,
1073-
results.FunctionArn,
1074-
eventSourceList.S3Events
1075-
),
1076-
this._setLogsRetentionPolicy(
1077-
cloudWatchLogs,
1078-
program,
1079-
params.FunctionName
1080-
)
1081-
])
1082-
})
1008+
return false
1009+
}
1010+
})()
1011+
1012+
if (existsFunction) {
1013+
const existingEventSourceList = await this._listEventSourceMappings(lambda, {
1014+
FunctionName: params.FunctionName
10831015
})
1084-
})
1016+
const results = await this._uploadExisting(lambda, params)
1017+
console.log('=> Done uploading. Results follow: ')
1018+
console.log(results)
1019+
1020+
return Promise.all([
1021+
Promise.all([
1022+
this._updateScheduleEvents(
1023+
scheduleEvents,
1024+
results.FunctionArn,
1025+
eventSourceList.ScheduleEvents
1026+
),
1027+
this._updateS3Events(
1028+
s3Events,
1029+
results.FunctionArn,
1030+
eventSourceList.S3Events
1031+
),
1032+
this._updateTags(
1033+
lambda,
1034+
results.FunctionArn,
1035+
params.Tags)
1036+
]),
1037+
this._updateEventSources(
1038+
lambda,
1039+
params.FunctionName,
1040+
existingEventSourceList,
1041+
eventSourceList.EventSourceMappings
1042+
),
1043+
this._setLogsRetentionPolicy(
1044+
cloudWatchLogs,
1045+
program,
1046+
params.FunctionName
1047+
)
1048+
])
1049+
} else {
1050+
const results = await this._uploadNew(lambda, params)
1051+
console.log('=> Done uploading. Results follow: ')
1052+
console.log(results)
1053+
1054+
return Promise.all([
1055+
this._updateEventSources(
1056+
lambda,
1057+
params.FunctionName,
1058+
[],
1059+
eventSourceList.EventSourceMappings
1060+
),
1061+
this._updateScheduleEvents(
1062+
scheduleEvents,
1063+
results.FunctionArn,
1064+
eventSourceList.ScheduleEvents
1065+
),
1066+
this._updateS3Events(
1067+
s3Events,
1068+
results.FunctionArn,
1069+
eventSourceList.S3Events
1070+
),
1071+
this._setLogsRetentionPolicy(
1072+
cloudWatchLogs,
1073+
program,
1074+
params.FunctionName
1075+
)
1076+
])
1077+
}
10851078
}
10861079

10871080
_printDeployResults (results, isFirst) {

0 commit comments

Comments
 (0)