-
Notifications
You must be signed in to change notification settings - Fork 3.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(core): allow cross region export provider to delete more than 10 exports #23969
Closed
joshbalfour
wants to merge
5
commits into
aws:main
from
joshbalfour:fix/cross-region-ssm-writer-deleteParameters-call
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
2b9a7a3
chore(@aws-cdk/core): add failing test
joshbalfour 1dd1eb0
fix(@aws-cdk/core): batch deleteParameters to SSM
joshbalfour 6d1b873
start writing integration test
joshbalfour 9ddfe61
Merge branch 'main' into fix/cross-region-ssm-writer-deleteParameters…
joshbalfour 7129a80
Merge branch 'main' into fix/cross-region-ssm-writer-deleteParameters…
joshbalfour File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
/*eslint-disable no-console*/ | ||
/* eslint-disable import/no-extraneous-dependencies */ | ||
import { SSM } from 'aws-sdk'; | ||
import { AWSError, SSM } from 'aws-sdk'; | ||
import { CrossRegionExports, ExportWriterCRProps } from '../types'; | ||
|
||
export async function handler(event: AWSLambda.CloudFormationCustomResourceEvent) { | ||
|
@@ -33,9 +33,7 @@ export async function handler(event: AWSLambda.CloudFormationCustomResourceEvent | |
// skip if no export names are to be deleted | ||
const removedExportsNames = Object.keys(removedExports); | ||
if (removedExportsNames.length > 0) { | ||
await ssm.deleteParameters({ | ||
Names: removedExportsNames, | ||
}).promise(); | ||
await deleteParameters(ssm, removedExportsNames); | ||
} | ||
|
||
// also throw an error if we are creating a new export that already exists for some reason | ||
|
@@ -48,9 +46,7 @@ export async function handler(event: AWSLambda.CloudFormationCustomResourceEvent | |
// the stack deletion. | ||
await throwIfAnyInUse(ssm, exports); | ||
// if none are in use then delete all of them | ||
await ssm.deleteParameters({ | ||
Names: Object.keys(exports), | ||
}).promise(); | ||
await deleteParameters(ssm, Object.keys(exports)); | ||
return; | ||
default: | ||
return; | ||
|
@@ -61,6 +57,26 @@ export async function handler(event: AWSLambda.CloudFormationCustomResourceEvent | |
} | ||
}; | ||
|
||
function chunkArray<T>(arr: T[], size: number): T[][] { | ||
return arr.length > size | ||
? [arr.slice(0, size), ...chunkArray(arr.slice(size), size)] | ||
: [arr]; | ||
} | ||
|
||
async function deleteParameters(ssm: SSM, names: string[]): Promise<void> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you add a comment here explaining why we are splitting it into groups of 10? |
||
if (names.length === 0) { | ||
return; | ||
} | ||
|
||
await Promise.all( | ||
chunkArray(names, 10) | ||
.map((namesChunk) => { | ||
return ssm.deleteParameters({ | ||
Names: namesChunk, | ||
}).promise(); | ||
})); | ||
} | ||
|
||
/** | ||
* Create parameters for existing exports | ||
*/ | ||
|
@@ -113,7 +129,7 @@ async function isInUse(ssm: SSM, parameterName: string): Promise<Set<string>> { | |
} catch (e) { | ||
// an InvalidResourceId means that the parameter doesn't exist | ||
// which we should ignore since that means it's not in use | ||
if (e.code === 'InvalidResourceId') { | ||
if ((e as AWSError).code === 'InvalidResourceId') { | ||
return new Set(); | ||
} | ||
throw e; | ||
|
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You may need to add a new integration test (new file and everything) since this test scenario will probably conflict with the one above.
To test this scenario you can probably do a very similar test to the one above, but just invert the scenario. Instead of deleting the producer stack, delete the consumer and then perform a describeStacks and expect the delete to have succeeded.