-
Notifications
You must be signed in to change notification settings - Fork 88
feat(cli): cdk orphan command detaches resources from a stack, allowing resource type upgrades (behind --unstable)
#1399
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
Merged
Merged
Changes from 5 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
cca68f1
feat(cli): add cdk orphan command to detach resources from a stack
1ad0186
chore: update yarn.lock for @aws-sdk/client-dynamodb
1f8e71a
chore: run projen to sync lockfile
6c1b04b
Lint
rix0rrr 21b1e3c
Merge branch 'main' into lhnng-cdk-orphan
mrgrain 55f0c1a
Bump only dynamodb
rix0rrr 57cc199
address PR feedback: positional args, remove --force, single message …
1ad4b87
chore: self mutation
github-actions[bot] 3fcf808
rename parseConstructPaths, add wildcard limitation to README
aba1d46
move orphaner and helpers from lib/actions to lib/api for consistency
feaf0ff
chore: self mutation
github-actions[bot] 80d4e46
Merge branch 'main' into lhnng-cdk-orphan
mrgrain 72bd1ff
Use properly primed AWS client
rix0rrr 729ee62
chore: self mutation
github-actions[bot] 46cf86b
fix integ test: use positional args and --yes, fix error message
cce71d9
Merge branch 'main' into lhnng-cdk-orphan
53004c5
fix: `bin` field in generated package.json files serves no purpose (#…
keyboardDrummer-bot 91e1b25
Merge remote-tracking branch 'origin/main' into lhnng-cdk-orphan
mrgrain c6a6891
chore: self mutation
github-actions[bot] 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 hidden or 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.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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
86 changes: 86 additions & 0 deletions
86
...-testing/cli-integ/tests/cli-integ-tests/orphan/cdk-orphan-detaches-resource.integtest.ts
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| import { DescribeStacksCommand, GetTemplateCommand } from '@aws-sdk/client-cloudformation'; | ||
| import { DynamoDBClient, PutItemCommand, GetItemCommand, DeleteTableCommand } from '@aws-sdk/client-dynamodb'; | ||
| import * as yaml from 'yaml'; | ||
| import { integTest, withDefaultFixture } from '../../../lib'; | ||
|
|
||
| integTest( | ||
| 'cdk orphan detaches a resource from the stack without deleting it', | ||
| withDefaultFixture(async (fixture) => { | ||
| const stackName = fixture.fullStackName('orphanable'); | ||
|
|
||
| // Deploy the stack with a DynamoDB table + Lambda consumer | ||
| await fixture.cdkDeploy('orphanable'); | ||
|
|
||
| // Get outputs | ||
| const describeResponse = await fixture.aws.cloudFormation.send( | ||
| new DescribeStacksCommand({ StackName: stackName }), | ||
| ); | ||
| const outputs = describeResponse.Stacks?.[0]?.Outputs ?? []; | ||
| const tableName = outputs.find((o) => o.OutputKey === 'TableName')?.OutputValue; | ||
| expect(tableName).toBeDefined(); | ||
|
|
||
| const dynamodb = new DynamoDBClient({ region: fixture.aws.region }); | ||
|
|
||
| try { | ||
| // Verify the table resource exists in the template before orphaning | ||
| const templateBefore = await fixture.aws.cloudFormation.send( | ||
| new GetTemplateCommand({ StackName: stackName }), | ||
| ); | ||
| const templateBodyBefore = yaml.parse(templateBefore.TemplateBody!); | ||
| expect(templateBodyBefore.Resources).toHaveProperty('MyTable794EDED1'); | ||
|
|
||
| // Put an item in the table before orphan | ||
| await dynamodb.send(new PutItemCommand({ | ||
| TableName: tableName!, | ||
| Item: { PK: { S: 'before-orphan' } }, | ||
| })); | ||
|
|
||
| // Orphan the table | ||
| const orphanOutput = await fixture.cdk([ | ||
| 'orphan', | ||
| '--path', `${stackName}/MyTable`, | ||
| '--unstable=orphan', | ||
| '--force', | ||
| ]); | ||
|
|
||
| // Verify the output contains a resource mapping for import | ||
| expect(orphanOutput).toContain('resource-mapping-inline'); | ||
| expect(orphanOutput).toContain('TableName'); | ||
|
|
||
| // Verify the template after orphan: table gone, Lambda env vars replaced with literals | ||
| const templateAfter = await fixture.aws.cloudFormation.send( | ||
| new GetTemplateCommand({ StackName: stackName }), | ||
| ); | ||
| const templateBody = yaml.parse(templateAfter.TemplateBody!); | ||
|
|
||
| expect(templateBody.Resources).not.toHaveProperty('MyTable794EDED1'); | ||
| expect(templateBody).toMatchObject({ | ||
| Resources: expect.objectContaining({ | ||
| Consumer8D6BE417: expect.objectContaining({ | ||
| Type: 'AWS::Lambda::Function', | ||
| Properties: expect.objectContaining({ | ||
| Environment: { | ||
| Variables: { | ||
| TABLE_NAME: expect.stringContaining('MyTable'), | ||
| TABLE_ARN: expect.stringContaining('arn:aws:dynamodb'), | ||
| }, | ||
| }, | ||
| }), | ||
| }), | ||
| }), | ||
| }); | ||
|
|
||
| // Verify the table still exists and data is intact (strongly consistent read) | ||
| const getItemResult = await dynamodb.send(new GetItemCommand({ | ||
| TableName: tableName!, | ||
| Key: { PK: { S: 'before-orphan' } }, | ||
| ConsistentRead: true, | ||
| })); | ||
| expect(getItemResult.Item?.PK?.S).toBe('before-orphan'); | ||
| } finally { | ||
| // Clean up the retained table to avoid leaking resources | ||
| await dynamodb.send(new DeleteTableCommand({ TableName: tableName! })).catch(() => { | ||
| }); | ||
| } | ||
| }), | ||
| ); |
This file contains hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| export interface OrphanOptions { | ||
| /** | ||
| * Construct path prefix(es) to orphan. Each path must be in the format | ||
| * `StackName/ConstructPath`, e.g. `MyStack/MyTable`. | ||
| * | ||
| * The stack is derived from the path — all paths must reference the same stack. | ||
| */ | ||
| readonly constructPaths: string[]; | ||
|
|
||
| /** | ||
| * Role to assume in the target environment. | ||
| */ | ||
| readonly roleArn?: string; | ||
|
|
||
| /** | ||
| * Toolkit stack name for bootstrap resources. | ||
| */ | ||
| readonly toolkitStackName?: string; | ||
|
|
||
| /** | ||
| * Whether to execute without prompting for confirmation. | ||
| * | ||
| * @default false | ||
| */ | ||
| readonly force?: boolean; | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.