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..fd4ea3e00 --- /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: [ + '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', + }]; + } + 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/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 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