Skip to content

Commit

Permalink
fix(_uploadExisting): fix function update errors (#575)
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
abetomo authored Sep 24, 2021
1 parent 8175737 commit 87580cb
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 22 deletions.
43 changes: 22 additions & 21 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion test/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ const lambdaMockSettings = {
addPermission: {},
getFunction: {
Code: {},
Configuration: {},
Configuration: { LastUpdateStatus: 'Successful' },
FunctionArn: 'Lambda.getFunction.mock.FunctionArn'
},
createFunction: {
Expand Down

0 comments on commit 87580cb

Please sign in to comment.