diff --git a/lib/main.js b/lib/main.js index 1d597aa..dcf14a0 100644 --- a/lib/main.js +++ b/lib/main.js @@ -973,115 +973,108 @@ they may not work as expected in the Lambda environment. !!err.message.match(/^Function not found:/) } - _deployToRegion (program, params, region, buffer) { + async _deployToRegion (program, params, region, buffer) { aws.updateConfig(program, region) console.log('=> Reading event source file to memory') const eventSourceList = this._eventSourceList(program) - return Promise.resolve().then(() => { - if (this._isUseS3(program)) { - const s3Deploy = new S3Deploy(aws.sdk, region) - return s3Deploy.putPackage(params, region, buffer) - } - return null - }).then((code) => { - if (code != null) params.Code = code - }).then(() => { - if (!this._isUseS3(program)) { - console.log(`=> Uploading zip file to AWS Lambda ${region} with parameters:`) - } else { - console.log(`=> Uploading AWS Lambda ${region} with parameters:`) - } - console.log(params) - - const lambda = new aws.sdk.Lambda({ - region, - apiVersion: '2015-03-31' - }) - const scheduleEvents = new ScheduleEvents(aws.sdk, region) - const s3Events = new S3Events(aws.sdk, region) - const cloudWatchLogs = new CloudWatchLogs(aws.sdk, region) + if (this._isUseS3(program)) { + const s3Deploy = new S3Deploy(aws.sdk, region) + params.Code = await s3Deploy.putPackage(params, region, buffer) + console.log(`=> Uploading AWS Lambda ${region} with parameters:`) + } else { + console.log(`=> Uploading zip file to AWS Lambda ${region} with parameters:`) + } + console.log(params) + const lambda = new aws.sdk.Lambda({ + region, + apiVersion: '2015-03-31' + }) + const scheduleEvents = new ScheduleEvents(aws.sdk, region) + const s3Events = new S3Events(aws.sdk, region) + const cloudWatchLogs = new CloudWatchLogs(aws.sdk, region) - // Checking function - return lambda.getFunction({ - FunctionName: params.FunctionName - }).promise().then(() => { - // Function exists - return this._listEventSourceMappings(lambda, { + const existsFunction = await (async () => { + try { + await lambda.getFunction({ FunctionName: params.FunctionName - }).then((existingEventSourceList) => { - return Promise.all([ - this._uploadExisting(lambda, params).then((results) => { - console.log('=> Done uploading. Results follow: ') - console.log(results) - return results - }).then(results => { - return Promise.all([ - this._updateScheduleEvents( - scheduleEvents, - results.FunctionArn, - eventSourceList.ScheduleEvents - ), - this._updateS3Events( - s3Events, - results.FunctionArn, - eventSourceList.S3Events - ), - this._updateTags( - lambda, - results.FunctionArn, - params.Tags) - ]) - }), - this._updateEventSources( - lambda, - params.FunctionName, - existingEventSourceList, - eventSourceList.EventSourceMappings - ), - this._setLogsRetentionPolicy( - cloudWatchLogs, - program, - params.FunctionName - ) - ]) - }) - }).catch((err) => { + }).promise() + return true + } catch (err) { if (!this._isFunctionDoesNotExist(err)) { throw err } - // Function does not exist - return this._uploadNew(lambda, params).then((results) => { - console.log('=> Done uploading. Results follow: ') - console.log(results) - - return Promise.all([ - this._updateEventSources( - lambda, - params.FunctionName, - [], - eventSourceList.EventSourceMappings - ), - this._updateScheduleEvents( - scheduleEvents, - results.FunctionArn, - eventSourceList.ScheduleEvents - ), - this._updateS3Events( - s3Events, - results.FunctionArn, - eventSourceList.S3Events - ), - this._setLogsRetentionPolicy( - cloudWatchLogs, - program, - params.FunctionName - ) - ]) - }) + return false + } + })() + + if (existsFunction) { + const existingEventSourceList = await this._listEventSourceMappings(lambda, { + FunctionName: params.FunctionName }) - }) + const results = await this._uploadExisting(lambda, params) + console.log('=> Done uploading. Results follow: ') + console.log(results) + + return Promise.all([ + Promise.all([ + this._updateScheduleEvents( + scheduleEvents, + results.FunctionArn, + eventSourceList.ScheduleEvents + ), + this._updateS3Events( + s3Events, + results.FunctionArn, + eventSourceList.S3Events + ), + this._updateTags( + lambda, + results.FunctionArn, + params.Tags) + ]), + this._updateEventSources( + lambda, + params.FunctionName, + existingEventSourceList, + eventSourceList.EventSourceMappings + ), + this._setLogsRetentionPolicy( + cloudWatchLogs, + program, + params.FunctionName + ) + ]) + } else { + const results = await this._uploadNew(lambda, params) + console.log('=> Done uploading. Results follow: ') + console.log(results) + + return Promise.all([ + this._updateEventSources( + lambda, + params.FunctionName, + [], + eventSourceList.EventSourceMappings + ), + this._updateScheduleEvents( + scheduleEvents, + results.FunctionArn, + eventSourceList.ScheduleEvents + ), + this._updateS3Events( + s3Events, + results.FunctionArn, + eventSourceList.S3Events + ), + this._setLogsRetentionPolicy( + cloudWatchLogs, + program, + params.FunctionName + ) + ]) + } } _printDeployResults (results, isFirst) {