-
Notifications
You must be signed in to change notification settings - Fork 85
feat(cli): add cdk orphan command to detach resources from a stack
#1324
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
Open
rix0rrr
wants to merge
10
commits into
aws:main
Choose a base branch
from
LeeroyHannigan:lhnng-orphan-resource
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 8 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
97b6d51
feat(cli): add `cdk orphan` command to detach resources from a stack
e4ef113
feat(cli): address PR feedback for cdk orphan command
571dbbc
refactor: use PATH_METADATA_KEY constant instead of string literal
e37d0dd
fix: use ToolkitError instead of Error, fix line length lint
c331a7c
fix: add orphan to cli-arguments test expectations
f108a2e
fix: apply auto-formatting to cli-config.ts
211b87e
refactor: address round 2 PR feedback
616d59a
fix: regenerate yarn.lock with Yarn 4
f576cc1
fix: bump bootstrap stack version to 32 for ListStackResources permis…
f1cbce0
docs: add cdk orphan section to README and remove post-orphan deploy …
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.
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.
Ah yeah? 😅
Bummer. But acceptable for now I guess.