Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Lambda image not updating while stack is deployed (#116)
Use a simpler method to avoid conflicts when updating the function code after the image is updated. Instead of looking for a running stack operation, just try again in a loop every 10 seconds. The current code assumed some special handling in CloudFormation that would update the function later. It wasn't working. I believe it was broken by #103.
- Loading branch information
Showing
10 changed files
with
707 additions
and
97 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,39 @@ | ||
/* eslint-disable-next-line import/no-extraneous-dependencies */ | ||
import { ResourceConflictException } from '@aws-sdk/client-lambda'; | ||
/* eslint-disable-next-line import/no-extraneous-dependencies */ | ||
import * as AWS from 'aws-sdk'; | ||
|
||
const cfn = new AWS.CloudFormation(); | ||
const lambda = new AWS.Lambda(); | ||
|
||
interface Input { | ||
readonly lambdaName: string; | ||
readonly repositoryUri: string; | ||
readonly repositoryTag: string; | ||
readonly stackName: string; | ||
} | ||
|
||
function sleep(ms: number) { | ||
return new Promise(resolve => setTimeout(resolve, ms)); | ||
} | ||
|
||
export async function handler(event: Input) { | ||
console.log(JSON.stringify(event)); | ||
|
||
const stacks = await cfn.describeStacks({ | ||
StackName: event.stackName, | ||
}).promise(); | ||
|
||
if (stacks.Stacks?.length != 1) { | ||
console.error(`Unable to find stack ${event.stackName}`); | ||
return; | ||
while (true) { | ||
try { | ||
await lambda.updateFunctionCode({ | ||
FunctionName: event.lambdaName, | ||
ImageUri: `${event.repositoryUri}:${event.repositoryTag}`, | ||
Publish: true, | ||
}).promise(); | ||
break; | ||
} catch (e) { | ||
if (e instanceof ResourceConflictException) { | ||
// keep trying if function is already being updated by CloudFormation | ||
// this can happen if we update some settings on the function and the image code at the same time | ||
await sleep(10000); | ||
} else { | ||
throw e; | ||
} | ||
} | ||
} | ||
|
||
if (stacks.Stacks[0].StackStatus.endsWith('_IN_PROGRESS')) { | ||
console.error(`Stack ${event.stackName} is already in progress, skipping Lambda update as the stack will do it for us. If we update here, it may conflict with the stack operation.`); | ||
return; | ||
} | ||
|
||
await lambda.updateFunctionCode({ | ||
FunctionName: event.lambdaName, | ||
ImageUri: `${event.repositoryUri}:${event.repositoryTag}`, | ||
Publish: true, | ||
}).promise(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.