Skip to content

Commit 87580cb

Browse files
authored
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`.
1 parent 8175737 commit 87580cb

File tree

2 files changed

+23
-22
lines changed

2 files changed

+23
-22
lines changed

lib/main.js

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -566,7 +566,7 @@ Emulate only the body of the API Gateway event.
566566
}
567567
}
568568

569-
_uploadExisting (lambda, params) {
569+
async _uploadExisting (lambda, params) {
570570
const functionCodeParams = Object.assign({
571571
FunctionName: params.FunctionName,
572572
Publish: params.Publish
@@ -594,29 +594,30 @@ Emulate only the body of the API Gateway event.
594594
delete functionConfigParams.Layers
595595
}
596596

597-
return new Promise((resolve, reject) => {
598-
const updateConfigRequest = lambda.updateFunctionConfiguration(
599-
functionConfigParams,
600-
(err, configResponse) => {
601-
if (err) return reject(err)
602-
603-
const updateCodeRequest = lambda.updateFunctionCode(functionCodeParams, (err) => {
604-
if (err) return reject(err)
605-
resolve(configResponse)
606-
})
597+
const updateConfigRequest = lambda.updateFunctionConfiguration(functionConfigParams)
598+
updateConfigRequest.on('retry', (response) => {
599+
console.log(response.error.message)
600+
console.log('=> Retrying')
601+
})
602+
const updateConfigResponse = await updateConfigRequest.promise()
607603

608-
updateCodeRequest.on('retry', (response) => {
609-
console.log(response.error.message)
610-
console.log('=> Retrying')
611-
})
612-
}
613-
)
604+
// Wait for the `Configuration.LastUpdateStatus` to change from `InProgress` to `Successful`.
605+
for (let i = 0; i < 10; i++) {
606+
const data = await lambda.getFunction({ FunctionName: params.FunctionName }).promise()
607+
if (data.Configuration.LastUpdateStatus === 'Successful') {
608+
break
609+
}
610+
await new Promise((resolve) => setTimeout(resolve, 3000))
611+
}
614612

615-
updateConfigRequest.on('retry', (response) => {
616-
console.log(response.error.message)
617-
console.log('=> Retrying')
618-
})
613+
const updateCodeRequest = lambda.updateFunctionCode(functionCodeParams)
614+
updateCodeRequest.on('retry', (response) => {
615+
console.log(response.error.message)
616+
console.log('=> Retrying')
619617
})
618+
await updateCodeRequest.promise()
619+
620+
return updateConfigResponse
620621
}
621622

622623
_uploadNew (lambda, params) {

test/main.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ const lambdaMockSettings = {
5252
addPermission: {},
5353
getFunction: {
5454
Code: {},
55-
Configuration: {},
55+
Configuration: { LastUpdateStatus: 'Successful' },
5656
FunctionArn: 'Lambda.getFunction.mock.FunctionArn'
5757
},
5858
createFunction: {

0 commit comments

Comments
 (0)