Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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 [];
}
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -464,6 +465,7 @@ export class CloudFormationStackDiagnoser {

const allContext = [
...hookContext,
...explainKnownError(err),
...additionalContext,
...(err.warnings?.length ? [{ source: 'Warnings', messages: err.warnings }] : []),
];
Expand Down
Original file line number Diff line number Diff line change
@@ -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([]);
});
Original file line number Diff line number Diff line change
Expand Up @@ -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' });
Expand Down
9 changes: 9 additions & 0 deletions packages/aws-cdk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down