feat(cli): introduce 'cdk resources' command for listing stack resources #963
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