feat(cli): introduce 'cdk resources' command for listing stack resources#963
Open
lousydropout wants to merge 5 commits intoaws:mainfrom
Open
feat(cli): introduce 'cdk resources' command for listing stack resources#963lousydropout wants to merge 5 commits intoaws:mainfrom
lousydropout wants to merge 5 commits intoaws:mainfrom
Conversation
Add a new CLI command `cdk resources <stack>` that lists all CloudFormation resources synthesized by CDK for a given stack. Features: - Summary view (default): resource counts by type - Long mode (--long): full list grouped by type - Type filter (--type): case-insensitive partial match - Hidden resources (--all): show Lambda::Permission - JSON output (--json): for scripting - Explain mode (--explain): detailed info for specific resource Includes comprehensive unit tests for listResources, explainResource, and CdkToolkit.resources() integration.
Change JSON output format from flat array with repeated stackId
to nested structure: [{stackId, resources: [...]}]
This reduces redundancy and provides cleaner output for scripting.
- Change STACK positional to variadic STACKS for multiple stack selection - Fix "undefined" stack name in summary output - Use DefaultSelection.AllStacks when no stacks specified
Add case-insensitive matching for stack name patterns using -i/--ignore-case flag.
Uses minimatch with {nocase: true} for pattern matching when enabled.
- Fix dead code in extractImportValues (array check before object check) - Extract normalizeDependsOn helper to reduce duplication - Use RESOURCE_SUFFIX constant instead of magic number - Use ToolkitError instead of Error for consistency
|
@lousydropout This looks great - any chance of progressing this? |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
This PR adds
cdk resources, a new CLI command that lets users quickly see which resources their stacks define without digging throughcdk.out/or deploying. It supports summary, detailed, and JSON modes, along with filtering and wildcard stack selection.Reason for this change
CDK users currently have no built-in way to inspect the synthesized CloudFormation resources in their stacks without:
cdk.outtemplatesA feature I, and I suspect others as well, have often wanted is a simple CLI command that answers:
This PR introduces a first-class solution:
cdk resources.Description of changes
This PR adds a new CLI command,
cdk resources, which lists CloudFormation resources from one or more synthesized stacks. It supports summary output, long-form detailed output, type filtering, wildcard stack selection, JSON mode, and resource explanation. This makes it significantly easier for developers to understand infrastructure generated by CDK apps.How it works
Command Usage:
Key behaviors:
Supports wildcard stack name selection (via
minimatch)Summary mode shows resource counts grouped by type
Long/detail mode (
--longor-l) shows full logical IDs under each typeJSON mode (
--jsonor-j) returns machine-readableResourceDetails[]--type <pattern>(or-t <pattern>) filters resources by type (case-insensitive)--allshows normally hidden resource types (e.g.,Lambda::Permission)--explain <LogicalId>produces deep, structured resource detail (single-stack only)Automatically handles:
DependsOnarraysFn::ImportValue)Case-insensitive matching supported with
--ignore-case(or-i)Files changed
Implementation:
cli-config.ts— command definition and option parsingcli.ts— dispatch to toolkitcdk-toolkit.ts— newCdkToolkit.resources()methodcommands/list-resources.ts— core logic (listResources,explainResource)@aws-cdk/toolkit-lib/.../resource-details.ts— shared type definitionsAuto-generated (via
yarn pre-compile):cli-type-registry.jsonparse-command-line-arguments.tsconvert-to-user-input.tsuser-input.tsTests:
test/commands/list-resources.test.ts— 17 unit teststest/cli/cdk-toolkit.test.ts— 10 integration testsDescribe any new or updated permissions being added
N/A — No new AWS permissions required.
This command operates purely on synthesized CloudFormation templates already present in the local Cloud Assembly.
Description of how you validated changes
17 unit tests covering resource extraction, filtering, metadata handling, hidden resources, import detection, removalPolicy mapping, sorting, explain mode, and wildcard matching
10 integration tests covering summary mode, long/detail mode, JSON output, explain mode, filtering, and CLI behavior
Manual testing against real CDK apps to validate:
Verified parser regeneration with
yarn pre-compileCI:
yarn buildand full Jest suite passUsage Example
Summary (default):
Long mode:
JSON mode:
[ { "stackId": "WebhookDeliveryStack", "resources": [ { "logicalId": "ApiLambda", "type": "AWS::Lambda::Function", "constructPath": "ApiLambda", "dependsOn": [], "imports": [], "removalPolicy": "destroy" } ] } ]Explain mode:
{ "stackId": "MyStack", "logicalId": "MyBucket", "type": "AWS::S3::Bucket", "constructPath": "MyBucket", "dependsOn": [], "imports": [], "removalPolicy": "retain", "condition": "CreateBucket" }By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license