From 2718357da77a0c6819e414e2f77aaf6b80fdc9df Mon Sep 17 00:00:00 2001 From: Diego Ortiz Date: Wed, 2 Dec 2026 11:47:22 -0500 Subject: [PATCH 1/4] feat(cli): `--no-include-nested-stacks` option to create deploy change sets without nested stacks Since #1531, deploy change sets are created with `IncludeNestedStacks: true`. For a large nested stack hierarchy, change set creation then fails with "Limit on the number of resources in a single stack operation exceeded". Add an `includeNestedStacks` option to `ChangeSetDeployment` in toolkit-lib (default `true`) and expose it on `cdk deploy` as `--[no-]include-nested-stacks` and as `includeNestedStacks` in `cdk.json`. Closes #2191 Co-Authored-By: Claude Opus 5.5 --- .../toolkit-lib/lib/actions/deploy/index.ts | 14 ++++++ .../lib/api/deployments/deploy-stack.ts | 12 ++++-- .../test/api/deployments/deploy-stack.test.ts | 22 ++++++++++ packages/aws-cdk/README.md | 19 ++++++++ packages/aws-cdk/lib/cli/cli-config.ts | 5 +++ .../aws-cdk/lib/cli/cli-type-registry.json | 4 ++ packages/aws-cdk/lib/cli/cli.ts | 4 ++ .../aws-cdk/lib/cli/convert-to-user-input.ts | 2 + .../lib/cli/parse-command-line-arguments.ts | 5 +++ .../aws-cdk/lib/cli/user-configuration.ts | 1 + packages/aws-cdk/lib/cli/user-input.ts | 7 +++ .../aws-cdk/test/cli/cli-arguments.test.ts | 1 + .../aws-cdk/test/cli/deploy-options.test.ts | 43 +++++++++++++++++++ 13 files changed, 136 insertions(+), 3 deletions(-) diff --git a/packages/@aws-cdk/toolkit-lib/lib/actions/deploy/index.ts b/packages/@aws-cdk/toolkit-lib/lib/actions/deploy/index.ts index ece5a6be9..4ad265772 100644 --- a/packages/@aws-cdk/toolkit-lib/lib/actions/deploy/index.ts +++ b/packages/@aws-cdk/toolkit-lib/lib/actions/deploy/index.ts @@ -42,6 +42,20 @@ export interface ChangeSetDeployment { * @default false */ readonly revertDrift?: boolean; + + /** + * Whether the change set also covers the stack's nested stacks. + * + * When enabled, CloudFormation creates a change set for every nested stack in the hierarchy, + * which reports nested stack errors and changes before execution. Disable this for a nested stack + * hierarchy whose change set exceeds CloudFormation's quota of resources per stack operation. + * Problems in nested stacks then only surface while the change set executes. + * + * Has no effect on change sets that import resources or revert drift, which never cover nested stacks. + * + * @default true + */ + readonly includeNestedStacks?: boolean; } /** diff --git a/packages/@aws-cdk/toolkit-lib/lib/api/deployments/deploy-stack.ts b/packages/@aws-cdk/toolkit-lib/lib/api/deployments/deploy-stack.ts index 9a8ced3e3..2d3f6027e 100644 --- a/packages/@aws-cdk/toolkit-lib/lib/api/deployments/deploy-stack.ts +++ b/packages/@aws-cdk/toolkit-lib/lib/api/deployments/deploy-stack.ts @@ -479,7 +479,8 @@ class FullCloudFormationDeployment { const execute = deploymentMethod.execute ?? true; const importExistingResources = deploymentMethod.importExistingResources ?? false; const revertDrift = deploymentMethod.revertDrift ?? false; - const changeSetReport = await this.createChangeSet(changeSetName, importExistingResources, revertDrift); + const includeNestedStacks = deploymentMethod.includeNestedStacks ?? true; + const changeSetReport = await this.createChangeSet(changeSetName, importExistingResources, revertDrift, includeNestedStacks); const changeSetDescription = changeSetReport.changeSet; await this.updateTerminationProtection(); @@ -625,7 +626,12 @@ class FullCloudFormationDeployment { return this.monitorDeployment(changeSet.CreationTime!, changeSet.StackId!, changeSetLength); } - private async createChangeSet(changeSetName: string, importExistingResources: boolean, revertDrift: boolean): Promise { + private async createChangeSet( + changeSetName: string, + importExistingResources: boolean, + revertDrift: boolean, + includeNestedStacks: boolean, + ): Promise { await this.cleanupOldChangeset(changeSetName); await this.ioHelper.defaults.debug(`Attempting to create ChangeSet with name ${changeSetName} to ${this.verb} stack ${this.stackName}`); @@ -639,7 +645,7 @@ class FullCloudFormationDeployment { ClientToken: `create${this.uuid}`, ImportExistingResources: importExistingResources, DeploymentMode: revertDrift ? 'REVERT_DRIFT' : undefined, - IncludeNestedStacks: (this.options.resourcesToImport || revertDrift) ? undefined : true, + IncludeNestedStacks: (this.options.resourcesToImport || revertDrift || !includeNestedStacks) ? undefined : true, ...this.commonPrepareOptions(), DeploymentConfig: this.deployConfig(), }); diff --git a/packages/@aws-cdk/toolkit-lib/test/api/deployments/deploy-stack.test.ts b/packages/@aws-cdk/toolkit-lib/test/api/deployments/deploy-stack.test.ts index 02bc8c3c2..8a4a2c2fe 100644 --- a/packages/@aws-cdk/toolkit-lib/test/api/deployments/deploy-stack.test.ts +++ b/packages/@aws-cdk/toolkit-lib/test/api/deployments/deploy-stack.test.ts @@ -2207,6 +2207,28 @@ test('passes IncludeNestedStacks to get nested stack error reporting', async () expect(calls[0].args[0].input).toHaveProperty('IncludeNestedStacks', true); }); +test('does not pass IncludeNestedStacks when includeNestedStacks is false', async () => { + await testDeployStack({ + ...standardDeployStackArguments(FAKE_STACK_WITH_NESTED_STACK), + deploymentMethod: { method: 'change-set', includeNestedStacks: false }, + }); + + const calls = mockCloudFormationClient.commandCalls(CreateChangeSetCommand); + expect(calls.length).toBeGreaterThan(0); + expect(calls[0].args[0].input.IncludeNestedStacks).toBeUndefined(); +}); + +test('passes IncludeNestedStacks when includeNestedStacks is true', async () => { + await testDeployStack({ + ...standardDeployStackArguments(FAKE_STACK_WITH_NESTED_STACK), + deploymentMethod: { method: 'change-set', includeNestedStacks: true }, + }); + + const calls = mockCloudFormationClient.commandCalls(CreateChangeSetCommand); + expect(calls.length).toBeGreaterThan(0); + expect(calls[0].args[0].input).toHaveProperty('IncludeNestedStacks', true); +}); + test('does not pass IncludeNestedStacks for import changesets', async () => { givenStackExists({ StackName: 'withnestedstack' }); await testDeployStack({ diff --git a/packages/aws-cdk/README.md b/packages/aws-cdk/README.md index e6871dfb3..c06e6deeb 100644 --- a/packages/aws-cdk/README.md +++ b/packages/aws-cdk/README.md @@ -618,6 +618,25 @@ When using `--method=execute-change-set`, the options `--force`, `--parameters`, `--import-existing-resources`, and `--revert-drift` cannot be used since the change set has already been created. +For a stack with nested stacks, the change set also covers the nested stacks +(the CloudFormation `IncludeNestedStacks` option), so that problems in nested +stacks are reported before the change set is executed. CloudFormation limits how +many resources a single operation on a nested stack hierarchy can create, update, +or delete, and the change set of a very large hierarchy can exceed that quota. +To create the change set for the root stack only, pass `--no-include-nested-stacks`: + +```console +$ cdk deploy --no-include-nested-stacks +``` + +To make this the default for a project, set `includeNestedStacks` in `cdk.json`: + +```json +{ + "includeNestedStacks": false +} +``` + #### Import existing resources You can utilize the AWS CloudFormation diff --git a/packages/aws-cdk/lib/cli/cli-config.ts b/packages/aws-cdk/lib/cli/cli-config.ts index 39cf254cf..e35a35fef 100644 --- a/packages/aws-cdk/lib/cli/cli-config.ts +++ b/packages/aws-cdk/lib/cli/cli-config.ts @@ -230,6 +230,11 @@ export async function makeConfig(): Promise { 'asset-prebuild': { type: 'boolean', desc: 'Whether to build all assets before deploying the first stack (useful for failing Docker builds)', default: true }, 'ignore-no-stacks': { type: 'boolean', desc: 'Whether to deploy if the app contains no stacks', default: false }, 'revert-drift': { type: 'boolean', desc: 'Create a drift-aware change set that brings actual resource states in line with template definitions', default: false }, + 'include-nested-stacks': { + type: 'boolean', + desc: "Whether the change set also covers nested stacks. Defaults to 'true'. " + + 'Use --no-include-nested-stacks when the change set of a nested stack hierarchy exceeds the CloudFormation quota of resources per stack operation', + }, 'express': { type: 'boolean', desc: 'Perform the CloudFormation deployment using Express Mode, a faster mode of deployment which skips stabilization and has automatic rollback disabled by default', default: false }, }, arg: { diff --git a/packages/aws-cdk/lib/cli/cli-type-registry.json b/packages/aws-cdk/lib/cli/cli-type-registry.json index c55b279ab..294d0f66a 100644 --- a/packages/aws-cdk/lib/cli/cli-type-registry.json +++ b/packages/aws-cdk/lib/cli/cli-type-registry.json @@ -633,6 +633,10 @@ "desc": "Create a drift-aware change set that brings actual resource states in line with template definitions", "default": false }, + "include-nested-stacks": { + "type": "boolean", + "desc": "Whether the change set also covers nested stacks. Defaults to 'true'. Use --no-include-nested-stacks when the change set of a nested stack hierarchy exceeds the CloudFormation quota of resources per stack operation" + }, "express": { "type": "boolean", "desc": "Perform the CloudFormation deployment using Express Mode, a faster mode of deployment which skips stabilization and has automatic rollback disabled by default", diff --git a/packages/aws-cdk/lib/cli/cli.ts b/packages/aws-cdk/lib/cli/cli.ts index fd5d670db..c25186bd2 100644 --- a/packages/aws-cdk/lib/cli/cli.ts +++ b/packages/aws-cdk/lib/cli/cli.ts @@ -889,6 +889,7 @@ function parseParameters(parameters: unknown[]): { [name: string]: string | unde function determineDeploymentMethod(args: any, configuration: Configuration, watch?: boolean): DeploymentMethod { let deploymentMethod: ChangeSetDeployment | DirectDeployment | undefined; + const includeNestedStacks: boolean | undefined = configuration.settings.get(['includeNestedStacks']); switch (args.method) { case 'execute-change-set': if (!args.STACKS || args.STACKS.length !== 1) { @@ -922,6 +923,7 @@ function determineDeploymentMethod(args: any, configuration: Configuration, watc changeSetName: args.changeSetName, importExistingResources: args.importExistingResources, revertDrift: args.revertDrift, + includeNestedStacks, }; break; case 'prepare-change-set': @@ -931,6 +933,7 @@ function determineDeploymentMethod(args: any, configuration: Configuration, watc changeSetName: args.changeSetName, importExistingResources: args.importExistingResources, revertDrift: args.revertDrift, + includeNestedStacks, }; break; case undefined: @@ -941,6 +944,7 @@ function determineDeploymentMethod(args: any, configuration: Configuration, watc changeSetName: args.changeSetName, importExistingResources: args.importExistingResources, revertDrift: args.revertDrift, + includeNestedStacks, }; break; } diff --git a/packages/aws-cdk/lib/cli/convert-to-user-input.ts b/packages/aws-cdk/lib/cli/convert-to-user-input.ts index a93900dfd..d3cbd0c89 100644 --- a/packages/aws-cdk/lib/cli/convert-to-user-input.ts +++ b/packages/aws-cdk/lib/cli/convert-to-user-input.ts @@ -156,6 +156,7 @@ export function convertYargsToUserInput(args: any): UserInput { assetPrebuild: args.assetPrebuild, ignoreNoStacks: args.ignoreNoStacks, revertDrift: args.revertDrift, + includeNestedStacks: args.includeNestedStacks, express: args.express, STACKS: args.STACKS, }; @@ -514,6 +515,7 @@ export function convertConfigToUserInput(config: any): UserInput { assetPrebuild: config.deploy?.assetPrebuild, ignoreNoStacks: config.deploy?.ignoreNoStacks, revertDrift: config.deploy?.revertDrift, + includeNestedStacks: config.deploy?.includeNestedStacks, express: config.deploy?.express, }; const validateOptions = { diff --git a/packages/aws-cdk/lib/cli/parse-command-line-arguments.ts b/packages/aws-cdk/lib/cli/parse-command-line-arguments.ts index 45e3655fb..1c3edd1f1 100644 --- a/packages/aws-cdk/lib/cli/parse-command-line-arguments.ts +++ b/packages/aws-cdk/lib/cli/parse-command-line-arguments.ts @@ -671,6 +671,11 @@ export function parseCommandLineArguments(args: Array): any { type: 'boolean', desc: 'Create a drift-aware change set that brings actual resource states in line with template definitions', }) + .option('include-nested-stacks', { + default: undefined, + type: 'boolean', + desc: "Whether the change set also covers nested stacks. Defaults to 'true'. Use --no-include-nested-stacks when the change set of a nested stack hierarchy exceeds the CloudFormation quota of resources per stack operation", + }) .option('express', { default: false, type: 'boolean', diff --git a/packages/aws-cdk/lib/cli/user-configuration.ts b/packages/aws-cdk/lib/cli/user-configuration.ts index 4a54d0b84..a7539776e 100644 --- a/packages/aws-cdk/lib/cli/user-configuration.ts +++ b/packages/aws-cdk/lib/cli/user-configuration.ts @@ -360,6 +360,7 @@ export async function commandLineArgumentsToSettings(ioHelper: IoHelper, argv: A assetBuildConcurrency: argv['asset-build-concurrency'], assetPrebuild: argv['asset-prebuild'], ignoreNoStacks: argv['ignore-no-stacks'], + includeNestedStacks: argv.includeNestedStacks, hotswap: { ecs: { minimumHealthyPercent: argv.hotswapEcsMinimumHealthyPercent, diff --git a/packages/aws-cdk/lib/cli/user-input.ts b/packages/aws-cdk/lib/cli/user-input.ts index 08a8e46cd..df51e6843 100644 --- a/packages/aws-cdk/lib/cli/user-input.ts +++ b/packages/aws-cdk/lib/cli/user-input.ts @@ -1059,6 +1059,13 @@ export interface DeployOptions { */ readonly revertDrift?: boolean; + /** + * Whether the change set also covers nested stacks. Defaults to 'true'. Use --no-include-nested-stacks when the change set of a nested stack hierarchy exceeds the CloudFormation quota of resources per stack operation + * + * @default - undefined + */ + readonly includeNestedStacks?: boolean; + /** * Perform the CloudFormation deployment using Express Mode, a faster mode of deployment which skips stabilization and has automatic rollback disabled by default * diff --git a/packages/aws-cdk/test/cli/cli-arguments.test.ts b/packages/aws-cdk/test/cli/cli-arguments.test.ts index 60777148f..b2a9665b0 100644 --- a/packages/aws-cdk/test/cli/cli-arguments.test.ts +++ b/packages/aws-cdk/test/cli/cli-arguments.test.ts @@ -61,6 +61,7 @@ describe('yargs', () => { hotswapFallback: undefined, ignoreNoStacks: false, importExistingResources: false, + includeNestedStacks: undefined, logs: true, method: undefined, monitoringTimeMinutes: undefined, diff --git a/packages/aws-cdk/test/cli/deploy-options.test.ts b/packages/aws-cdk/test/cli/deploy-options.test.ts index 2c68be206..c09674cdf 100644 --- a/packages/aws-cdk/test/cli/deploy-options.test.ts +++ b/packages/aws-cdk/test/cli/deploy-options.test.ts @@ -97,6 +97,49 @@ describe('deploy --method=execute-change-set', () => { }); }); +describe('deploy --include-nested-stacks', () => { + test('leaves includeNestedStacks to the toolkit default when not given', async () => { + await exec(['deploy', '--app', 'echo', 'MyStack']); + + expect(deploySpy).toHaveBeenCalledWith(expect.objectContaining({ + deploymentMethod: expect.objectContaining({ method: 'change-set', includeNestedStacks: undefined }), + })); + }); + + test.each([ + ['default', []], + ['change-set', ['--method=change-set']], + ['prepare-change-set', ['--method=prepare-change-set']], + ])('--no-include-nested-stacks is passed on with the %s method', async (_name, flags) => { + await exec(['deploy', '--app', 'echo', ...flags, '--no-include-nested-stacks', 'MyStack']); + + expect(deploySpy).toHaveBeenCalledWith(expect.objectContaining({ + deploymentMethod: expect.objectContaining({ method: 'change-set', includeNestedStacks: false }), + })); + }); + + test.each([ + [[], false], + [['--include-nested-stacks'], true], + ])('reads includeNestedStacks from cdk.json with flags %j', async (flags, expected) => { + const oldDir = process.cwd(); + const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'cdk-deploy-nested-')); + try { + fs.writeJsonSync(path.join(tempDir, 'cdk.json'), { app: 'echo', includeNestedStacks: false }); + process.chdir(tempDir); + + await exec(['deploy', ...flags, 'MyStack']); + + expect(deploySpy).toHaveBeenCalledWith(expect.objectContaining({ + deploymentMethod: expect.objectContaining({ method: 'change-set', includeNestedStacks: expected }), + })); + } finally { + process.chdir(oldDir); + fs.removeSync(tempDir); + } + }); +}); + describe('deploy rollback triggers', () => { test('--rollback-trigger-alarm-arns builds a rollback configuration', async () => { await exec(['deploy', '--app', 'echo', '--rollback-trigger-alarm-arns', 'arn:aws:cloudwatch:us-east-1:123456789012:alarm:MyAlarm', 'MyStack']); From 70745d7efa54144aec5648ec15846d89365aa568 Mon Sep 17 00:00:00 2001 From: Diego Ortiz Date: Wed, 2 Dec 2026 11:59:11 -0500 Subject: [PATCH 2/4] fix(cli): apply review feedback for --no-include-nested-stacks - Pass the cdk.json setting to the follow-up deploy of `cdk import`. - Describe the quota error and the cost of opting out (late nested stack errors, nested stacks always reported as changed) in JSDoc, flag help and README, and say which commands the option affects. - Test --hotswap-fallback, cdk watch and cdk import; drop a test that repeated the default case. Co-Authored-By: Claude Opus 5.5 --- .../toolkit-lib/lib/actions/deploy/index.ts | 7 +- .../test/api/deployments/deploy-stack.test.ts | 11 ---- packages/aws-cdk/README.md | 15 +++-- packages/aws-cdk/lib/cli/cli-config.ts | 2 +- .../aws-cdk/lib/cli/cli-type-registry.json | 2 +- packages/aws-cdk/lib/cli/cli.ts | 1 + .../lib/cli/parse-command-line-arguments.ts | 2 +- packages/aws-cdk/lib/cli/user-input.ts | 2 +- .../aws-cdk/test/cli/deploy-options.test.ts | 66 +++++++++++++++---- 9 files changed, 73 insertions(+), 35 deletions(-) diff --git a/packages/@aws-cdk/toolkit-lib/lib/actions/deploy/index.ts b/packages/@aws-cdk/toolkit-lib/lib/actions/deploy/index.ts index 4ad265772..15c26fa77 100644 --- a/packages/@aws-cdk/toolkit-lib/lib/actions/deploy/index.ts +++ b/packages/@aws-cdk/toolkit-lib/lib/actions/deploy/index.ts @@ -47,9 +47,10 @@ export interface ChangeSetDeployment { * Whether the change set also covers the stack's nested stacks. * * When enabled, CloudFormation creates a change set for every nested stack in the hierarchy, - * which reports nested stack errors and changes before execution. Disable this for a nested stack - * hierarchy whose change set exceeds CloudFormation's quota of resources per stack operation. - * Problems in nested stacks then only surface while the change set executes. + * which reports nested stack errors before execution and detects nested stacks that have no changes. + * Disable this when creating the change set for a large nested stack hierarchy fails with a + * CloudFormation resource quota error. Problems in nested stacks then only surface while the + * change set executes, and CloudFormation reports nested stacks as changed even when they are not. * * Has no effect on change sets that import resources or revert drift, which never cover nested stacks. * diff --git a/packages/@aws-cdk/toolkit-lib/test/api/deployments/deploy-stack.test.ts b/packages/@aws-cdk/toolkit-lib/test/api/deployments/deploy-stack.test.ts index 8a4a2c2fe..764174bd7 100644 --- a/packages/@aws-cdk/toolkit-lib/test/api/deployments/deploy-stack.test.ts +++ b/packages/@aws-cdk/toolkit-lib/test/api/deployments/deploy-stack.test.ts @@ -2218,17 +2218,6 @@ test('does not pass IncludeNestedStacks when includeNestedStacks is false', asyn expect(calls[0].args[0].input.IncludeNestedStacks).toBeUndefined(); }); -test('passes IncludeNestedStacks when includeNestedStacks is true', async () => { - await testDeployStack({ - ...standardDeployStackArguments(FAKE_STACK_WITH_NESTED_STACK), - deploymentMethod: { method: 'change-set', includeNestedStacks: true }, - }); - - const calls = mockCloudFormationClient.commandCalls(CreateChangeSetCommand); - expect(calls.length).toBeGreaterThan(0); - expect(calls[0].args[0].input).toHaveProperty('IncludeNestedStacks', true); -}); - test('does not pass IncludeNestedStacks for import changesets', async () => { givenStackExists({ StackName: 'withnestedstack' }); await testDeployStack({ diff --git a/packages/aws-cdk/README.md b/packages/aws-cdk/README.md index c06e6deeb..a4f09abe0 100644 --- a/packages/aws-cdk/README.md +++ b/packages/aws-cdk/README.md @@ -619,16 +619,23 @@ When using `--method=execute-change-set`, the options `--force`, `--parameters`, since the change set has already been created. For a stack with nested stacks, the change set also covers the nested stacks -(the CloudFormation `IncludeNestedStacks` option), so that problems in nested -stacks are reported before the change set is executed. CloudFormation limits how -many resources a single operation on a nested stack hierarchy can create, update, -or delete, and the change set of a very large hierarchy can exceed that quota. +(the CloudFormation `IncludeNestedStacks` option). This reports problems in nested +stacks before the change set is executed, and lets CloudFormation tell which nested +stacks have no changes. For a large nested stack hierarchy, creating that change set +can fail with `Limit on the number of resources in a single stack operation exceeded`. To create the change set for the root stack only, pass `--no-include-nested-stacks`: ```console $ cdk deploy --no-include-nested-stacks ``` +Problems in nested stacks then only surface while the change set executes, and +CloudFormation reports nested stacks as changed even when they are not. The option +only applies to the change sets that `cdk deploy`, `cdk watch` and `cdk import` +create to deploy; it has no effect with `--method=direct` or +`--method=execute-change-set`, and the change sets that `cdk diff` creates always +include nested stacks. + To make this the default for a project, set `includeNestedStacks` in `cdk.json`: ```json diff --git a/packages/aws-cdk/lib/cli/cli-config.ts b/packages/aws-cdk/lib/cli/cli-config.ts index e35a35fef..9a5961ff5 100644 --- a/packages/aws-cdk/lib/cli/cli-config.ts +++ b/packages/aws-cdk/lib/cli/cli-config.ts @@ -233,7 +233,7 @@ export async function makeConfig(): Promise { 'include-nested-stacks': { type: 'boolean', desc: "Whether the change set also covers nested stacks. Defaults to 'true'. " + - 'Use --no-include-nested-stacks when the change set of a nested stack hierarchy exceeds the CloudFormation quota of resources per stack operation', + 'Use --no-include-nested-stacks when creating the change set for a large nested stack hierarchy fails with a CloudFormation resource quota error', }, 'express': { type: 'boolean', desc: 'Perform the CloudFormation deployment using Express Mode, a faster mode of deployment which skips stabilization and has automatic rollback disabled by default', default: false }, }, diff --git a/packages/aws-cdk/lib/cli/cli-type-registry.json b/packages/aws-cdk/lib/cli/cli-type-registry.json index 294d0f66a..10863ad8d 100644 --- a/packages/aws-cdk/lib/cli/cli-type-registry.json +++ b/packages/aws-cdk/lib/cli/cli-type-registry.json @@ -635,7 +635,7 @@ }, "include-nested-stacks": { "type": "boolean", - "desc": "Whether the change set also covers nested stacks. Defaults to 'true'. Use --no-include-nested-stacks when the change set of a nested stack hierarchy exceeds the CloudFormation quota of resources per stack operation" + "desc": "Whether the change set also covers nested stacks. Defaults to 'true'. Use --no-include-nested-stacks when creating the change set for a large nested stack hierarchy fails with a CloudFormation resource quota error" }, "express": { "type": "boolean", diff --git a/packages/aws-cdk/lib/cli/cli.ts b/packages/aws-cdk/lib/cli/cli.ts index c25186bd2..35149e21c 100644 --- a/packages/aws-cdk/lib/cli/cli.ts +++ b/packages/aws-cdk/lib/cli/cli.ts @@ -575,6 +575,7 @@ export async function exec(args: string[], synthesizer?: Synthesizer): Promise): any { .option('include-nested-stacks', { default: undefined, type: 'boolean', - desc: "Whether the change set also covers nested stacks. Defaults to 'true'. Use --no-include-nested-stacks when the change set of a nested stack hierarchy exceeds the CloudFormation quota of resources per stack operation", + desc: "Whether the change set also covers nested stacks. Defaults to 'true'. Use --no-include-nested-stacks when creating the change set for a large nested stack hierarchy fails with a CloudFormation resource quota error", }) .option('express', { default: false, diff --git a/packages/aws-cdk/lib/cli/user-input.ts b/packages/aws-cdk/lib/cli/user-input.ts index df51e6843..ac5a01c36 100644 --- a/packages/aws-cdk/lib/cli/user-input.ts +++ b/packages/aws-cdk/lib/cli/user-input.ts @@ -1060,7 +1060,7 @@ export interface DeployOptions { readonly revertDrift?: boolean; /** - * Whether the change set also covers nested stacks. Defaults to 'true'. Use --no-include-nested-stacks when the change set of a nested stack hierarchy exceeds the CloudFormation quota of resources per stack operation + * Whether the change set also covers nested stacks. Defaults to 'true'. Use --no-include-nested-stacks when creating the change set for a large nested stack hierarchy fails with a CloudFormation resource quota error * * @default - undefined */ diff --git a/packages/aws-cdk/test/cli/deploy-options.test.ts b/packages/aws-cdk/test/cli/deploy-options.test.ts index c09674cdf..fdc68a90e 100644 --- a/packages/aws-cdk/test/cli/deploy-options.test.ts +++ b/packages/aws-cdk/test/cli/deploy-options.test.ts @@ -118,28 +118,68 @@ describe('deploy --include-nested-stacks', () => { })); }); + test('--no-include-nested-stacks is passed on to the --hotswap-fallback change set', async () => { + await exec(['deploy', '--app', 'echo', '--hotswap-fallback', '--no-include-nested-stacks', 'MyStack']); + + expect(deploySpy).toHaveBeenCalledWith(expect.objectContaining({ + deploymentMethod: expect.objectContaining({ + method: 'hotswap', + fallback: expect.objectContaining({ method: 'change-set', includeNestedStacks: false }), + }), + })); + }); + test.each([ [[], false], [['--include-nested-stacks'], true], ])('reads includeNestedStacks from cdk.json with flags %j', async (flags, expected) => { - const oldDir = process.cwd(); - const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'cdk-deploy-nested-')); - try { - fs.writeJsonSync(path.join(tempDir, 'cdk.json'), { app: 'echo', includeNestedStacks: false }); - process.chdir(tempDir); - + await withCdkJson({ app: 'echo', includeNestedStacks: false }, async () => { await exec(['deploy', ...flags, 'MyStack']); + }); - expect(deploySpy).toHaveBeenCalledWith(expect.objectContaining({ - deploymentMethod: expect.objectContaining({ method: 'change-set', includeNestedStacks: expected }), - })); - } finally { - process.chdir(oldDir); - fs.removeSync(tempDir); - } + expect(deploySpy).toHaveBeenCalledWith(expect.objectContaining({ + deploymentMethod: expect.objectContaining({ method: 'change-set', includeNestedStacks: expected }), + })); + }); + + test('cdk watch reads includeNestedStacks from cdk.json', async () => { + const watchSpy = jest.spyOn(cdkToolkitModule.CdkToolkit.prototype, 'watch').mockResolvedValue(); + + await withCdkJson({ app: 'echo', includeNestedStacks: false }, async () => { + await exec(['watch', '--no-hotswap', 'MyStack']); + }); + + expect(watchSpy).toHaveBeenCalledWith(expect.objectContaining({ + deploymentMethod: expect.objectContaining({ method: 'change-set', includeNestedStacks: false }), + })); + }); + + test('cdk import reads includeNestedStacks from cdk.json for the follow-up deploy', async () => { + const importSpy = jest.spyOn(cdkToolkitModule.CdkToolkit.prototype, 'import').mockResolvedValue(); + + await withCdkJson({ app: 'echo', includeNestedStacks: false }, async () => { + await exec(['import', 'MyStack']); + }); + + expect(importSpy).toHaveBeenCalledWith(expect.objectContaining({ + deploymentMethod: expect.objectContaining({ method: 'change-set', includeNestedStacks: false }), + })); }); }); +async function withCdkJson(config: Record, block: () => Promise) { + const oldDir = process.cwd(); + const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'cdk-deploy-nested-')); + try { + fs.writeJsonSync(path.join(tempDir, 'cdk.json'), config); + process.chdir(tempDir); + await block(); + } finally { + process.chdir(oldDir); + fs.removeSync(tempDir); + } +} + describe('deploy rollback triggers', () => { test('--rollback-trigger-alarm-arns builds a rollback configuration', async () => { await exec(['deploy', '--app', 'echo', '--rollback-trigger-alarm-arns', 'arn:aws:cloudwatch:us-east-1:123456789012:alarm:MyAlarm', 'MyStack']); From 6bec60599103784a0d58c68d06f5780034a1a199 Mon Sep 17 00:00:00 2001 From: Diego Ortiz Date: Wed, 2 Dec 2026 13:10:55 -0500 Subject: [PATCH 3/4] feat(toolkit-lib): explain the CloudFormation resources per operation quota error Testing showed that `--no-include-nested-stacks` does not avoid the 2,500 resources per operation quota: CloudFormation counts the resources that change in the deployment either way, and without nested change sets the failure only moves to execution, followed by a rollback. Remove the option again. Instead, explain the quota error when it shows up. The error is reported against the first nested stack that hit it, which reads like a problem with that stack. Add context saying the quota covers the whole deployment, counts changed resources, cannot be raised, and that the change must be split into smaller deployments. Closes #2191 Co-Authored-By: Claude Opus 5.5 --- .../toolkit-lib/lib/actions/deploy/index.ts | 15 ---- .../lib/api/deployments/deploy-stack.ts | 12 +-- .../lib/api/diagnosing/known-errors.ts | 27 ++++++ .../lib/api/diagnosing/stack-diagnoser.ts | 2 + .../test/api/deployments/deploy-stack.test.ts | 11 --- .../test/api/diagnosing/known-errors.test.ts | 34 ++++++++ .../api/diagnosing/stack-diagnoser.test.ts | 72 ++++++++++++++++ packages/aws-cdk/README.md | 26 ------ packages/aws-cdk/lib/cli/cli-config.ts | 5 -- .../aws-cdk/lib/cli/cli-type-registry.json | 4 - packages/aws-cdk/lib/cli/cli.ts | 5 -- .../aws-cdk/lib/cli/convert-to-user-input.ts | 2 - .../lib/cli/parse-command-line-arguments.ts | 5 -- .../aws-cdk/lib/cli/user-configuration.ts | 1 - packages/aws-cdk/lib/cli/user-input.ts | 7 -- .../aws-cdk/test/cli/cli-arguments.test.ts | 1 - .../aws-cdk/test/cli/deploy-options.test.ts | 83 ------------------- 17 files changed, 138 insertions(+), 174 deletions(-) create mode 100644 packages/@aws-cdk/toolkit-lib/lib/api/diagnosing/known-errors.ts create mode 100644 packages/@aws-cdk/toolkit-lib/test/api/diagnosing/known-errors.test.ts diff --git a/packages/@aws-cdk/toolkit-lib/lib/actions/deploy/index.ts b/packages/@aws-cdk/toolkit-lib/lib/actions/deploy/index.ts index 15c26fa77..ece5a6be9 100644 --- a/packages/@aws-cdk/toolkit-lib/lib/actions/deploy/index.ts +++ b/packages/@aws-cdk/toolkit-lib/lib/actions/deploy/index.ts @@ -42,21 +42,6 @@ export interface ChangeSetDeployment { * @default false */ readonly revertDrift?: boolean; - - /** - * Whether the change set also covers the stack's nested stacks. - * - * When enabled, CloudFormation creates a change set for every nested stack in the hierarchy, - * which reports nested stack errors before execution and detects nested stacks that have no changes. - * Disable this when creating the change set for a large nested stack hierarchy fails with a - * CloudFormation resource quota error. Problems in nested stacks then only surface while the - * change set executes, and CloudFormation reports nested stacks as changed even when they are not. - * - * Has no effect on change sets that import resources or revert drift, which never cover nested stacks. - * - * @default true - */ - readonly includeNestedStacks?: boolean; } /** diff --git a/packages/@aws-cdk/toolkit-lib/lib/api/deployments/deploy-stack.ts b/packages/@aws-cdk/toolkit-lib/lib/api/deployments/deploy-stack.ts index 2d3f6027e..9a8ced3e3 100644 --- a/packages/@aws-cdk/toolkit-lib/lib/api/deployments/deploy-stack.ts +++ b/packages/@aws-cdk/toolkit-lib/lib/api/deployments/deploy-stack.ts @@ -479,8 +479,7 @@ class FullCloudFormationDeployment { const execute = deploymentMethod.execute ?? true; const importExistingResources = deploymentMethod.importExistingResources ?? false; const revertDrift = deploymentMethod.revertDrift ?? false; - const includeNestedStacks = deploymentMethod.includeNestedStacks ?? true; - const changeSetReport = await this.createChangeSet(changeSetName, importExistingResources, revertDrift, includeNestedStacks); + const changeSetReport = await this.createChangeSet(changeSetName, importExistingResources, revertDrift); const changeSetDescription = changeSetReport.changeSet; await this.updateTerminationProtection(); @@ -626,12 +625,7 @@ class FullCloudFormationDeployment { return this.monitorDeployment(changeSet.CreationTime!, changeSet.StackId!, changeSetLength); } - private async createChangeSet( - changeSetName: string, - importExistingResources: boolean, - revertDrift: boolean, - includeNestedStacks: boolean, - ): Promise { + private async createChangeSet(changeSetName: string, importExistingResources: boolean, revertDrift: boolean): Promise { await this.cleanupOldChangeset(changeSetName); await this.ioHelper.defaults.debug(`Attempting to create ChangeSet with name ${changeSetName} to ${this.verb} stack ${this.stackName}`); @@ -645,7 +639,7 @@ class FullCloudFormationDeployment { ClientToken: `create${this.uuid}`, ImportExistingResources: importExistingResources, DeploymentMode: revertDrift ? 'REVERT_DRIFT' : undefined, - IncludeNestedStacks: (this.options.resourcesToImport || revertDrift || !includeNestedStacks) ? undefined : true, + IncludeNestedStacks: (this.options.resourcesToImport || revertDrift) ? undefined : true, ...this.commonPrepareOptions(), DeploymentConfig: this.deployConfig(), }); diff --git a/packages/@aws-cdk/toolkit-lib/lib/api/diagnosing/known-errors.ts b/packages/@aws-cdk/toolkit-lib/lib/api/diagnosing/known-errors.ts new file mode 100644 index 000000000..83cf8d3ce --- /dev/null +++ b/packages/@aws-cdk/toolkit-lib/lib/api/diagnosing/known-errors.ts @@ -0,0 +1,27 @@ +import type { AdditionalDiagnosticContext } from '../../actions/diagnose'; +import type { ResourceError } from '../stack-events/resource-errors'; + +const RESOURCES_PER_OPERATION_QUOTA_EXCEEDED = 'Limit on the number of resources in a single stack operation exceeded'; + +const CLOUDFORMATION_QUOTAS_URL = 'https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/cloudformation-limits.html'; + +/** + * Explain CloudFormation errors whose message alone doesn't tell the user what to do. + * + * Returns additional diagnostic context for the error, or an empty array if the error is not one we know. + */ +export function explainKnownError(err: ResourceError): AdditionalDiagnosticContext[] { + if (err.message.includes(RESOURCES_PER_OPERATION_QUOTA_EXCEEDED)) { + return [{ + source: 'CloudFormation quota', + messages: [ + 'CloudFormation can create, update or delete at most 2,500 resources in a single operation on a stack and its nested stacks,', + 'and this deployment changes more than that. The quota applies to the whole deployment, not only to the stack named above,', + 'and cannot be raised. Split the change into several smaller deployments.', + ], + link: CLOUDFORMATION_QUOTAS_URL, + linkLabel: 'Quotas', + }]; + } + return []; +} diff --git a/packages/@aws-cdk/toolkit-lib/lib/api/diagnosing/stack-diagnoser.ts b/packages/@aws-cdk/toolkit-lib/lib/api/diagnosing/stack-diagnoser.ts index b56b88e5f..4534bcbcc 100644 --- a/packages/@aws-cdk/toolkit-lib/lib/api/diagnosing/stack-diagnoser.ts +++ b/packages/@aws-cdk/toolkit-lib/lib/api/diagnosing/stack-diagnoser.ts @@ -4,6 +4,7 @@ import type { ChangeSetResourceError } from './changeset-error-fetcher'; import { ChangeSetResourceErrorFetcher } from './changeset-error-fetcher'; import { attributedCloudTrailContexts, investigateStackViaCloudTrail } from './cloudtrail-investigation'; import { Diagnosis } from './diagnosis'; +import { explainKnownError } from './known-errors'; import { investigateResource } from './resource-investigation'; import type { AdditionalDiagnosticContext, StackProblemSource, TracedResourceError } from '../../actions/diagnose'; import type { ICloudFormationClient, SDK } from '../aws-auth/sdk'; @@ -464,6 +465,7 @@ export class CloudFormationStackDiagnoser { const allContext = [ ...hookContext, + ...explainKnownError(err), ...additionalContext, ...(err.warnings?.length ? [{ source: 'Warnings', messages: err.warnings }] : []), ]; diff --git a/packages/@aws-cdk/toolkit-lib/test/api/deployments/deploy-stack.test.ts b/packages/@aws-cdk/toolkit-lib/test/api/deployments/deploy-stack.test.ts index 764174bd7..02bc8c3c2 100644 --- a/packages/@aws-cdk/toolkit-lib/test/api/deployments/deploy-stack.test.ts +++ b/packages/@aws-cdk/toolkit-lib/test/api/deployments/deploy-stack.test.ts @@ -2207,17 +2207,6 @@ test('passes IncludeNestedStacks to get nested stack error reporting', async () expect(calls[0].args[0].input).toHaveProperty('IncludeNestedStacks', true); }); -test('does not pass IncludeNestedStacks when includeNestedStacks is false', async () => { - await testDeployStack({ - ...standardDeployStackArguments(FAKE_STACK_WITH_NESTED_STACK), - deploymentMethod: { method: 'change-set', includeNestedStacks: false }, - }); - - const calls = mockCloudFormationClient.commandCalls(CreateChangeSetCommand); - expect(calls.length).toBeGreaterThan(0); - expect(calls[0].args[0].input.IncludeNestedStacks).toBeUndefined(); -}); - test('does not pass IncludeNestedStacks for import changesets', async () => { givenStackExists({ StackName: 'withnestedstack' }); await testDeployStack({ diff --git a/packages/@aws-cdk/toolkit-lib/test/api/diagnosing/known-errors.test.ts b/packages/@aws-cdk/toolkit-lib/test/api/diagnosing/known-errors.test.ts new file mode 100644 index 000000000..b32f875fe --- /dev/null +++ b/packages/@aws-cdk/toolkit-lib/test/api/diagnosing/known-errors.test.ts @@ -0,0 +1,34 @@ +import { explainKnownError } from '../../../lib/api/diagnosing/known-errors'; +import type { ResourceError } from '../../../lib/api/stack-events/resource-errors'; + +function resourceError(message: string): ResourceError { + return { + stackArn: 'NestedStack', + parentStackLogicalIds: ['NestedStackResource'], + resourceType: 'AWS::CloudFormation::Stack', + message, + }; +} + +test('explains the resources per stack operation quota', () => { + const context = explainKnownError(resourceError('Limit on the number of resources in a single stack operation exceeded')); + + expect(context).toEqual([expect.objectContaining({ + source: 'CloudFormation quota', + messages: expect.arrayContaining([expect.stringContaining('Split the change into several smaller deployments')]), + link: 'https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/cloudformation-limits.html', + })]); +}); + +test('explains the quota when it is part of a longer status reason', () => { + const context = explainKnownError(resourceError( + 'Embedded stack NestedStack was not successfully updated. ' + + 'Currently in UPDATE_ROLLBACK_IN_PROGRESS with reason: Limit on the number of resources in a single stack operation exceeded', + )); + + expect(context).toHaveLength(1); +}); + +test('does not explain other errors', () => { + expect(explainKnownError(resourceError('Resource handler returned message: "Access Denied"'))).toEqual([]); +}); diff --git a/packages/@aws-cdk/toolkit-lib/test/api/diagnosing/stack-diagnoser.test.ts b/packages/@aws-cdk/toolkit-lib/test/api/diagnosing/stack-diagnoser.test.ts index 557d8a9d0..648b35c35 100644 --- a/packages/@aws-cdk/toolkit-lib/test/api/diagnosing/stack-diagnoser.test.ts +++ b/packages/@aws-cdk/toolkit-lib/test/api/diagnosing/stack-diagnoser.test.ts @@ -296,6 +296,78 @@ describe('CloudFormationStackDiagnoser', () => { expect(fakeTracer.stackCalls.length + fakeTracer.resourceCalls.length).toBeGreaterThan(0); }); + test('explains a nested change set that exceeds the resources per operation quota', async () => { + fakeCfn.createStackSync({ StackName: 'ParentStack', StackStatus: 'UPDATE_COMPLETE' }); + fakeCfn.createStackSync({ StackName: 'NestedStack', StackStatus: 'UPDATE_COMPLETE' }); + fakeCfn.createChangeSetSync({ + StackName: 'NestedStack', + ChangeSetName: 'nested-cs', + Status: 'FAILED', + StatusReason: 'Limit on the number of resources in a single stack operation exceeded', + }); + + const nestedCsId = fakeCfn.accessStack('NestedStack').changeSets[0].id; + + fakeCfn.createChangeSetSync({ + StackName: 'ParentStack', + ChangeSetName: 'parent-cs', + Status: 'FAILED', + StatusReason: `Nested change set ${nestedCsId} was not successfully created: Currently in FAILED.`, + Changes: [{ + Type: 'Resource', + ResourceChange: { + Action: 'Modify', + LogicalResourceId: 'NestedStackResource', + ResourceType: 'AWS::CloudFormation::Stack', + PhysicalResourceId: 'NestedStack', + ChangeSetId: nestedCsId, + }, + }], + }); + + const diagnosis = await makeDiagnoser().diagnoseFromFresh('ParentStack'); + + const problem = assertProblem(diagnosis); + expect(problem.problems[0].additionalContext).toEqual([ + expect.objectContaining({ source: 'CloudFormation quota' }), + ]); + expect(() => diagnosis.throwOnError()).toThrow(/Split the change into several smaller deployments/); + }); + + test('does not add quota context to other nested change set failures', async () => { + fakeCfn.createStackSync({ StackName: 'ParentStack', StackStatus: 'UPDATE_COMPLETE' }); + fakeCfn.createStackSync({ StackName: 'NestedStack', StackStatus: 'UPDATE_COMPLETE' }); + fakeCfn.createChangeSetSync({ + StackName: 'NestedStack', + ChangeSetName: 'nested-cs', + Status: 'FAILED', + StatusReason: 'Some nested error', + }); + + const nestedCsId = fakeCfn.accessStack('NestedStack').changeSets[0].id; + + fakeCfn.createChangeSetSync({ + StackName: 'ParentStack', + ChangeSetName: 'parent-cs', + Status: 'FAILED', + StatusReason: `Nested change set ${nestedCsId} failed`, + Changes: [{ + Type: 'Resource', + ResourceChange: { + Action: 'Modify', + LogicalResourceId: 'NestedStackResource', + ResourceType: 'AWS::CloudFormation::Stack', + PhysicalResourceId: 'NestedStack', + ChangeSetId: nestedCsId, + }, + }], + }); + + const problem = assertProblem(await makeDiagnoser().diagnoseFromFresh('ParentStack')); + + expect(problem.problems[0].additionalContext).toBeUndefined(); + }); + test('finds the failed nested change set on a later page, describing without property values', async () => { // GIVEN - the parent change set lists the failed nested stack on its second page fakeCfn.createStackSync({ StackName: 'ParentStack', StackStatus: 'CREATE_COMPLETE' }); diff --git a/packages/aws-cdk/README.md b/packages/aws-cdk/README.md index a4f09abe0..e6871dfb3 100644 --- a/packages/aws-cdk/README.md +++ b/packages/aws-cdk/README.md @@ -618,32 +618,6 @@ When using `--method=execute-change-set`, the options `--force`, `--parameters`, `--import-existing-resources`, and `--revert-drift` cannot be used since the change set has already been created. -For a stack with nested stacks, the change set also covers the nested stacks -(the CloudFormation `IncludeNestedStacks` option). This reports problems in nested -stacks before the change set is executed, and lets CloudFormation tell which nested -stacks have no changes. For a large nested stack hierarchy, creating that change set -can fail with `Limit on the number of resources in a single stack operation exceeded`. -To create the change set for the root stack only, pass `--no-include-nested-stacks`: - -```console -$ cdk deploy --no-include-nested-stacks -``` - -Problems in nested stacks then only surface while the change set executes, and -CloudFormation reports nested stacks as changed even when they are not. The option -only applies to the change sets that `cdk deploy`, `cdk watch` and `cdk import` -create to deploy; it has no effect with `--method=direct` or -`--method=execute-change-set`, and the change sets that `cdk diff` creates always -include nested stacks. - -To make this the default for a project, set `includeNestedStacks` in `cdk.json`: - -```json -{ - "includeNestedStacks": false -} -``` - #### Import existing resources You can utilize the AWS CloudFormation diff --git a/packages/aws-cdk/lib/cli/cli-config.ts b/packages/aws-cdk/lib/cli/cli-config.ts index 9a5961ff5..39cf254cf 100644 --- a/packages/aws-cdk/lib/cli/cli-config.ts +++ b/packages/aws-cdk/lib/cli/cli-config.ts @@ -230,11 +230,6 @@ export async function makeConfig(): Promise { 'asset-prebuild': { type: 'boolean', desc: 'Whether to build all assets before deploying the first stack (useful for failing Docker builds)', default: true }, 'ignore-no-stacks': { type: 'boolean', desc: 'Whether to deploy if the app contains no stacks', default: false }, 'revert-drift': { type: 'boolean', desc: 'Create a drift-aware change set that brings actual resource states in line with template definitions', default: false }, - 'include-nested-stacks': { - type: 'boolean', - desc: "Whether the change set also covers nested stacks. Defaults to 'true'. " + - 'Use --no-include-nested-stacks when creating the change set for a large nested stack hierarchy fails with a CloudFormation resource quota error', - }, 'express': { type: 'boolean', desc: 'Perform the CloudFormation deployment using Express Mode, a faster mode of deployment which skips stabilization and has automatic rollback disabled by default', default: false }, }, arg: { diff --git a/packages/aws-cdk/lib/cli/cli-type-registry.json b/packages/aws-cdk/lib/cli/cli-type-registry.json index 10863ad8d..c55b279ab 100644 --- a/packages/aws-cdk/lib/cli/cli-type-registry.json +++ b/packages/aws-cdk/lib/cli/cli-type-registry.json @@ -633,10 +633,6 @@ "desc": "Create a drift-aware change set that brings actual resource states in line with template definitions", "default": false }, - "include-nested-stacks": { - "type": "boolean", - "desc": "Whether the change set also covers nested stacks. Defaults to 'true'. Use --no-include-nested-stacks when creating the change set for a large nested stack hierarchy fails with a CloudFormation resource quota error" - }, "express": { "type": "boolean", "desc": "Perform the CloudFormation deployment using Express Mode, a faster mode of deployment which skips stabilization and has automatic rollback disabled by default", diff --git a/packages/aws-cdk/lib/cli/cli.ts b/packages/aws-cdk/lib/cli/cli.ts index 35149e21c..fd5d670db 100644 --- a/packages/aws-cdk/lib/cli/cli.ts +++ b/packages/aws-cdk/lib/cli/cli.ts @@ -575,7 +575,6 @@ export async function exec(args: string[], synthesizer?: Synthesizer): Promise): any { type: 'boolean', desc: 'Create a drift-aware change set that brings actual resource states in line with template definitions', }) - .option('include-nested-stacks', { - default: undefined, - type: 'boolean', - desc: "Whether the change set also covers nested stacks. Defaults to 'true'. Use --no-include-nested-stacks when creating the change set for a large nested stack hierarchy fails with a CloudFormation resource quota error", - }) .option('express', { default: false, type: 'boolean', diff --git a/packages/aws-cdk/lib/cli/user-configuration.ts b/packages/aws-cdk/lib/cli/user-configuration.ts index a7539776e..4a54d0b84 100644 --- a/packages/aws-cdk/lib/cli/user-configuration.ts +++ b/packages/aws-cdk/lib/cli/user-configuration.ts @@ -360,7 +360,6 @@ export async function commandLineArgumentsToSettings(ioHelper: IoHelper, argv: A assetBuildConcurrency: argv['asset-build-concurrency'], assetPrebuild: argv['asset-prebuild'], ignoreNoStacks: argv['ignore-no-stacks'], - includeNestedStacks: argv.includeNestedStacks, hotswap: { ecs: { minimumHealthyPercent: argv.hotswapEcsMinimumHealthyPercent, diff --git a/packages/aws-cdk/lib/cli/user-input.ts b/packages/aws-cdk/lib/cli/user-input.ts index ac5a01c36..08a8e46cd 100644 --- a/packages/aws-cdk/lib/cli/user-input.ts +++ b/packages/aws-cdk/lib/cli/user-input.ts @@ -1059,13 +1059,6 @@ export interface DeployOptions { */ readonly revertDrift?: boolean; - /** - * Whether the change set also covers nested stacks. Defaults to 'true'. Use --no-include-nested-stacks when creating the change set for a large nested stack hierarchy fails with a CloudFormation resource quota error - * - * @default - undefined - */ - readonly includeNestedStacks?: boolean; - /** * Perform the CloudFormation deployment using Express Mode, a faster mode of deployment which skips stabilization and has automatic rollback disabled by default * diff --git a/packages/aws-cdk/test/cli/cli-arguments.test.ts b/packages/aws-cdk/test/cli/cli-arguments.test.ts index b2a9665b0..60777148f 100644 --- a/packages/aws-cdk/test/cli/cli-arguments.test.ts +++ b/packages/aws-cdk/test/cli/cli-arguments.test.ts @@ -61,7 +61,6 @@ describe('yargs', () => { hotswapFallback: undefined, ignoreNoStacks: false, importExistingResources: false, - includeNestedStacks: undefined, logs: true, method: undefined, monitoringTimeMinutes: undefined, diff --git a/packages/aws-cdk/test/cli/deploy-options.test.ts b/packages/aws-cdk/test/cli/deploy-options.test.ts index fdc68a90e..2c68be206 100644 --- a/packages/aws-cdk/test/cli/deploy-options.test.ts +++ b/packages/aws-cdk/test/cli/deploy-options.test.ts @@ -97,89 +97,6 @@ describe('deploy --method=execute-change-set', () => { }); }); -describe('deploy --include-nested-stacks', () => { - test('leaves includeNestedStacks to the toolkit default when not given', async () => { - await exec(['deploy', '--app', 'echo', 'MyStack']); - - expect(deploySpy).toHaveBeenCalledWith(expect.objectContaining({ - deploymentMethod: expect.objectContaining({ method: 'change-set', includeNestedStacks: undefined }), - })); - }); - - test.each([ - ['default', []], - ['change-set', ['--method=change-set']], - ['prepare-change-set', ['--method=prepare-change-set']], - ])('--no-include-nested-stacks is passed on with the %s method', async (_name, flags) => { - await exec(['deploy', '--app', 'echo', ...flags, '--no-include-nested-stacks', 'MyStack']); - - expect(deploySpy).toHaveBeenCalledWith(expect.objectContaining({ - deploymentMethod: expect.objectContaining({ method: 'change-set', includeNestedStacks: false }), - })); - }); - - test('--no-include-nested-stacks is passed on to the --hotswap-fallback change set', async () => { - await exec(['deploy', '--app', 'echo', '--hotswap-fallback', '--no-include-nested-stacks', 'MyStack']); - - expect(deploySpy).toHaveBeenCalledWith(expect.objectContaining({ - deploymentMethod: expect.objectContaining({ - method: 'hotswap', - fallback: expect.objectContaining({ method: 'change-set', includeNestedStacks: false }), - }), - })); - }); - - test.each([ - [[], false], - [['--include-nested-stacks'], true], - ])('reads includeNestedStacks from cdk.json with flags %j', async (flags, expected) => { - await withCdkJson({ app: 'echo', includeNestedStacks: false }, async () => { - await exec(['deploy', ...flags, 'MyStack']); - }); - - expect(deploySpy).toHaveBeenCalledWith(expect.objectContaining({ - deploymentMethod: expect.objectContaining({ method: 'change-set', includeNestedStacks: expected }), - })); - }); - - test('cdk watch reads includeNestedStacks from cdk.json', async () => { - const watchSpy = jest.spyOn(cdkToolkitModule.CdkToolkit.prototype, 'watch').mockResolvedValue(); - - await withCdkJson({ app: 'echo', includeNestedStacks: false }, async () => { - await exec(['watch', '--no-hotswap', 'MyStack']); - }); - - expect(watchSpy).toHaveBeenCalledWith(expect.objectContaining({ - deploymentMethod: expect.objectContaining({ method: 'change-set', includeNestedStacks: false }), - })); - }); - - test('cdk import reads includeNestedStacks from cdk.json for the follow-up deploy', async () => { - const importSpy = jest.spyOn(cdkToolkitModule.CdkToolkit.prototype, 'import').mockResolvedValue(); - - await withCdkJson({ app: 'echo', includeNestedStacks: false }, async () => { - await exec(['import', 'MyStack']); - }); - - expect(importSpy).toHaveBeenCalledWith(expect.objectContaining({ - deploymentMethod: expect.objectContaining({ method: 'change-set', includeNestedStacks: false }), - })); - }); -}); - -async function withCdkJson(config: Record, block: () => Promise) { - const oldDir = process.cwd(); - const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'cdk-deploy-nested-')); - try { - fs.writeJsonSync(path.join(tempDir, 'cdk.json'), config); - process.chdir(tempDir); - await block(); - } finally { - process.chdir(oldDir); - fs.removeSync(tempDir); - } -} - describe('deploy rollback triggers', () => { test('--rollback-trigger-alarm-arns builds a rollback configuration', async () => { await exec(['deploy', '--app', 'echo', '--rollback-trigger-alarm-arns', 'arn:aws:cloudwatch:us-east-1:123456789012:alarm:MyAlarm', 'MyStack']); From 97fed8aa826cdd549e61a45ab30d5e7822da395d Mon Sep 17 00:00:00 2001 From: Diego Ortiz Date: Wed, 2 Dec 2026 13:13:49 -0500 Subject: [PATCH 4/4] docs(cli): document the nested stack resource quota for deploy change sets Also align the hint with the wording of the CloudFormation quotas page, which does not say whether the quota can be raised. Co-Authored-By: Claude Opus 5.5 --- .../toolkit-lib/lib/api/diagnosing/known-errors.ts | 6 +++--- packages/aws-cdk/README.md | 9 +++++++++ 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/packages/@aws-cdk/toolkit-lib/lib/api/diagnosing/known-errors.ts b/packages/@aws-cdk/toolkit-lib/lib/api/diagnosing/known-errors.ts index 83cf8d3ce..fd4ea3e00 100644 --- a/packages/@aws-cdk/toolkit-lib/lib/api/diagnosing/known-errors.ts +++ b/packages/@aws-cdk/toolkit-lib/lib/api/diagnosing/known-errors.ts @@ -15,9 +15,9 @@ export function explainKnownError(err: ResourceError): AdditionalDiagnosticConte return [{ source: 'CloudFormation quota', messages: [ - 'CloudFormation can create, update or delete at most 2,500 resources in a single operation on a stack and its nested stacks,', - 'and this deployment changes more than that. The quota applies to the whole deployment, not only to the stack named above,', - 'and cannot be raised. Split the change into several smaller deployments.', + 'A deployment of a stack with nested stacks can create, update or delete at most 2,500 resources, and this deployment changes more.', + 'The quota applies to the whole stack hierarchy, not only to the nested stack named above.', + 'Split the change into several smaller deployments.', ], link: CLOUDFORMATION_QUOTAS_URL, linkLabel: 'Quotas', diff --git a/packages/aws-cdk/README.md b/packages/aws-cdk/README.md index e6871dfb3..561f496e7 100644 --- a/packages/aws-cdk/README.md +++ b/packages/aws-cdk/README.md @@ -618,6 +618,15 @@ When using `--method=execute-change-set`, the options `--force`, `--parameters`, `--import-existing-resources`, and `--revert-drift` cannot be used since the change set has already been created. +Change sets created by `cdk deploy` include nested stacks, so CloudFormation +checks the whole stack hierarchy before it changes anything. A deployment of a +stack with nested stacks can create, update or delete at most 2,500 resources +(see [CloudFormation quotas](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/cloudformation-limits.html)). +If a deployment changes more, creating the change set fails with +`Limit on the number of resources in a single stack operation exceeded`, even +though the error names a single nested stack. Split the change into several +smaller deployments, for example by upgrading the CDK library in smaller steps. + #### Import existing resources You can utilize the AWS CloudFormation