Skip to content

Commit

Permalink
wait for until update completes on each update
Browse files Browse the repository at this point in the history
  • Loading branch information
tmokmss committed Jan 12, 2022
1 parent e7c9206 commit bb4eb4d
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 7 deletions.
17 changes: 10 additions & 7 deletions packages/aws-cdk/lib/api/hotswap/lambda-functions.ts
Expand Up @@ -216,23 +216,26 @@ class LambdaFunctionHotswapOperation implements HotswapOperation {
const operations: Promise<any>[] = [];

if (resource.code !== undefined) {
const updateFunctionCodePromise = lambda.updateFunctionCode({
await lambda.updateFunctionCode({
FunctionName: this.lambdaFunctionResource.physicalName,
S3Bucket: resource.code.s3Bucket,
S3Key: resource.code.s3Key,
ImageUri: resource.code.imageUri,
}).promise();

// we need to wait for LastUpdateStatus to be successful
// otherwise users will see old Lambda code working event after hotswap completed
const waitForFunctionUpdatedPromise = lambda.waitFor('functionUpdated', {
FunctionName: this.lambdaFunctionResource.physicalName,
}).promise();

// only if the code changed is there any point in publishing a new Version
if (this.lambdaFunctionResource.publishVersion) {
// we need to wait for the code update to be done before publishing a new Version
await updateFunctionCodePromise;
// wait for the code update to be done before publishing a new Version
// if we don't wait for the Function to finish updating,
// we can get a "The operation cannot be performed at this time. An update is in progress for resource:"
// error when publishing a new Version
await lambda.waitFor('functionUpdated', {
FunctionName: this.lambdaFunctionResource.physicalName,
}).promise();
await waitForFunctionUpdatedPromise;

const publishVersionPromise = lambda.publishVersion({
FunctionName: this.lambdaFunctionResource.physicalName,
Expand All @@ -253,7 +256,7 @@ class LambdaFunctionHotswapOperation implements HotswapOperation {
operations.push(publishVersionPromise);
}
} else {
operations.push(updateFunctionCodePromise);
operations.push(waitForFunctionUpdatedPromise);
}
}

Expand Down
Expand Up @@ -15,6 +15,7 @@ beforeEach(() => {
updateFunctionCode: mockUpdateLambdaCode,
tagResource: mockTagResource,
untagResource: mockUntagResource,
waitFor: jest.fn(),
});
});

Expand Down
Expand Up @@ -15,6 +15,7 @@ beforeEach(() => {
updateFunctionCode: mockUpdateLambdaCode,
tagResource: mockTagResource,
untagResource: mockUntagResource,
waitFor: jest.fn(),
});
});

Expand Down

0 comments on commit bb4eb4d

Please sign in to comment.