From 87580cb425c4a540f7f5f12b831b85107f223067 Mon Sep 17 00:00:00 2001 From: Abe Tomoaki Date: Sat, 25 Sep 2021 01:25:30 +0900 Subject: [PATCH] fix(_uploadExisting): fix function update errors (#575) Immediately after updating the settings, `Configuration.LastUpdateStatus` is `InProgress`. If you update the code in that state, you will get an error. You need to update the code after the `Configuration.LastUpdateStatus` becomes `Successful`. I've fixed it to wait until it becomes `Successful`. --- lib/main.js | 43 ++++++++++++++++++++++--------------------- test/main.js | 2 +- 2 files changed, 23 insertions(+), 22 deletions(-) diff --git a/lib/main.js b/lib/main.js index c9cef55..051a432 100644 --- a/lib/main.js +++ b/lib/main.js @@ -566,7 +566,7 @@ Emulate only the body of the API Gateway event. } } - _uploadExisting (lambda, params) { + async _uploadExisting (lambda, params) { const functionCodeParams = Object.assign({ FunctionName: params.FunctionName, Publish: params.Publish @@ -594,29 +594,30 @@ Emulate only the body of the API Gateway event. delete functionConfigParams.Layers } - return new Promise((resolve, reject) => { - const updateConfigRequest = lambda.updateFunctionConfiguration( - functionConfigParams, - (err, configResponse) => { - if (err) return reject(err) - - const updateCodeRequest = lambda.updateFunctionCode(functionCodeParams, (err) => { - if (err) return reject(err) - resolve(configResponse) - }) + const updateConfigRequest = lambda.updateFunctionConfiguration(functionConfigParams) + updateConfigRequest.on('retry', (response) => { + console.log(response.error.message) + console.log('=> Retrying') + }) + const updateConfigResponse = await updateConfigRequest.promise() - updateCodeRequest.on('retry', (response) => { - console.log(response.error.message) - console.log('=> Retrying') - }) - } - ) + // Wait for the `Configuration.LastUpdateStatus` to change from `InProgress` to `Successful`. + for (let i = 0; i < 10; i++) { + const data = await lambda.getFunction({ FunctionName: params.FunctionName }).promise() + if (data.Configuration.LastUpdateStatus === 'Successful') { + break + } + await new Promise((resolve) => setTimeout(resolve, 3000)) + } - updateConfigRequest.on('retry', (response) => { - console.log(response.error.message) - console.log('=> Retrying') - }) + const updateCodeRequest = lambda.updateFunctionCode(functionCodeParams) + updateCodeRequest.on('retry', (response) => { + console.log(response.error.message) + console.log('=> Retrying') }) + await updateCodeRequest.promise() + + return updateConfigResponse } _uploadNew (lambda, params) { diff --git a/test/main.js b/test/main.js index 13d1692..b88d567 100644 --- a/test/main.js +++ b/test/main.js @@ -52,7 +52,7 @@ const lambdaMockSettings = { addPermission: {}, getFunction: { Code: {}, - Configuration: {}, + Configuration: { LastUpdateStatus: 'Successful' }, FunctionArn: 'Lambda.getFunction.mock.FunctionArn' }, createFunction: {