Skip to content

Commit 3e40edc

Browse files
authored
feat(cli): cdk rollback (#31684)
This is a re-draft of #31407. All description and motivation of the previous PR still apply. The previous PR caused a regression because some `CREATE_IN_PROGRESS` events for CloudFormation do not have a `PhysicalResourceId`. Fix that issue in this PR. Update the existing unit test that was supposed to catch this issue previously, it did not set a `ResourceStatus` which caused the event to be skipped for the wrong reason. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent e8dc7bb commit 3e40edc

21 files changed

+1142
-227
lines changed

Diff for: packages/@aws-cdk-testing/cli-integ/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ Test suites are written as a collection of Jest tests, and they are run using Je
3737

3838
### Setup
3939

40-
Building the @aws-cdk-testing package is not very different from building the rest of the CDK. However, If you are having issues with the tests, you can ensure your enviornment is built properly by following the steps below:
40+
Building the @aws-cdk-testing package is not very different from building the rest of the CDK. However, If you are having issues with the tests, you can ensure your environment is built properly by following the steps below:
4141

4242
```shell
4343
yarn install # Install dependencies

Diff for: packages/@aws-cdk-testing/cli-integ/lib/with-cdk-app.ts

+17-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ export const EXTENDED_TEST_TIMEOUT_S = 30 * 60;
2424
* For backwards compatibility with existing tests (so we don't have to change
2525
* too much) the inner block is expected to take a `TestFixture` object.
2626
*/
27-
export function withCdkApp(
27+
export function withSpecificCdkApp(
28+
appName: string,
2829
block: (context: TestFixture) => Promise<void>,
2930
): (context: TestContext & AwsContext & DisableBootstrapContext) => Promise<void> {
3031
return async (context: TestContext & AwsContext & DisableBootstrapContext) => {
@@ -36,7 +37,7 @@ export function withCdkApp(
3637
context.output.write(` Test directory: ${integTestDir}\n`);
3738
context.output.write(` Region: ${context.aws.region}\n`);
3839

39-
await cloneDirectory(path.join(RESOURCES_DIR, 'cdk-apps', 'app'), integTestDir, context.output);
40+
await cloneDirectory(path.join(RESOURCES_DIR, 'cdk-apps', appName), integTestDir, context.output);
4041
const fixture = new TestFixture(
4142
integTestDir,
4243
stackNamePrefix,
@@ -87,6 +88,16 @@ export function withCdkApp(
8788
};
8889
}
8990

91+
/**
92+
* Like `withSpecificCdkApp`, but uses the default integration testing app with a million stacks in it
93+
*/
94+
export function withCdkApp(
95+
block: (context: TestFixture) => Promise<void>,
96+
): (context: TestContext & AwsContext & DisableBootstrapContext) => Promise<void> {
97+
// 'app' is the name of the default integration app in the `cdk-apps` directory
98+
return withSpecificCdkApp('app', block);
99+
}
100+
90101
export function withCdkMigrateApp<A extends TestContext>(language: string, block: (context: TestFixture) => Promise<void>) {
91102
return async (context: A) => {
92103
const stackName = `cdk-migrate-${language}-integ-${context.randomString}`;
@@ -188,6 +199,10 @@ export function withDefaultFixture(block: (context: TestFixture) => Promise<void
188199
return withAws(withTimeout(DEFAULT_TEST_TIMEOUT_S, withCdkApp(block)));
189200
}
190201

202+
export function withSpecificFixture(appName: string, block: (context: TestFixture) => Promise<void>) {
203+
return withAws(withTimeout(DEFAULT_TEST_TIMEOUT_S, withSpecificCdkApp(appName, block)));
204+
}
205+
191206
export function withExtendedTimeoutFixture(block: (context: TestFixture) => Promise<void>) {
192207
return withAws(withTimeout(EXTENDED_TEST_TIMEOUT_S, withCdkApp(block)));
193208
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
const cdk = require('aws-cdk-lib');
2+
const lambda = require('aws-cdk-lib/aws-lambda');
3+
const cr = require('aws-cdk-lib/custom-resources');
4+
5+
/**
6+
* This stack will be deployed in multiple phases, to achieve a very specific effect
7+
*
8+
* It contains resources r1 and r2, where r1 gets deployed first.
9+
*
10+
* - PHASE = 1: both resources deploy regularly.
11+
* - PHASE = 2a: r1 gets updated, r2 will fail to update
12+
* - PHASE = 2b: r1 gets updated, r2 will fail to update, and r1 will fail its rollback.
13+
*
14+
* To exercise this app:
15+
*
16+
* ```
17+
* env PHASE=1 npx cdk deploy
18+
* env PHASE=2b npx cdk deploy --no-rollback
19+
* # This will leave the stack in UPDATE_FAILED
20+
*
21+
* env PHASE=2b npx cdk rollback
22+
* # This will start a rollback that will fail because r1 fails its rollabck
23+
*
24+
* env PHASE=2b npx cdk rollback --force
25+
* # This will retry the rollabck and skip r1
26+
* ```
27+
*/
28+
class RollbacktestStack extends cdk.Stack {
29+
constructor(scope, id, props) {
30+
super(scope, id, props);
31+
32+
let r1props = {};
33+
let r2props = {};
34+
35+
const phase = process.env.PHASE;
36+
switch (phase) {
37+
case '1':
38+
// Normal deployment
39+
break;
40+
case '2a':
41+
// r1 updates normally, r2 fails updating
42+
r2props.FailUpdate = true;
43+
break;
44+
case '2b':
45+
// r1 updates normally, r2 fails updating, r1 fails rollback
46+
r1props.FailRollback = true;
47+
r2props.FailUpdate = true;
48+
break;
49+
}
50+
51+
const fn = new lambda.Function(this, 'Fun', {
52+
runtime: lambda.Runtime.NODEJS_LATEST,
53+
code: lambda.Code.fromInline(`exports.handler = async function(event, ctx) {
54+
const key = \`Fail\${event.RequestType}\`;
55+
if (event.ResourceProperties[key]) {
56+
throw new Error(\`\${event.RequestType} fails!\`);
57+
}
58+
if (event.OldResourceProperties?.FailRollback) {
59+
throw new Error('Failing rollback!');
60+
}
61+
return {};
62+
}`),
63+
handler: 'index.handler',
64+
timeout: cdk.Duration.minutes(1),
65+
});
66+
const provider = new cr.Provider(this, "MyProvider", {
67+
onEventHandler: fn,
68+
});
69+
70+
const r1 = new cdk.CustomResource(this, 'r1', {
71+
serviceToken: provider.serviceToken,
72+
properties: r1props,
73+
});
74+
const r2 = new cdk.CustomResource(this, 'r2', {
75+
serviceToken: provider.serviceToken,
76+
properties: r2props,
77+
});
78+
r2.node.addDependency(r1);
79+
}
80+
}
81+
82+
const app = new cdk.App({
83+
context: {
84+
'@aws-cdk/core:assetHashSalt': process.env.CODEBUILD_BUILD_ID, // Force all assets to be unique, but consistent in one build
85+
},
86+
});
87+
88+
const defaultEnv = {
89+
account: process.env.CDK_DEFAULT_ACCOUNT,
90+
region: process.env.CDK_DEFAULT_REGION
91+
};
92+
93+
const stackPrefix = process.env.STACK_NAME_PREFIX;
94+
if (!stackPrefix) {
95+
throw new Error(`the STACK_NAME_PREFIX environment variable is required`);
96+
}
97+
98+
// Sometimes we don't want to synthesize all stacks because it will impact the results
99+
new RollbacktestStack(app, `${stackPrefix}-test-rollback`, { env: defaultEnv });
100+
app.synth();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"app": "node app.js",
3+
"versionReporting": false,
4+
"context": {
5+
"aws-cdk:enableDiffNoFail": "true"
6+
}
7+
}

Diff for: packages/@aws-cdk-testing/cli-integ/tests/cli-integ-tests/cli.integtest.ts

+79
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import {
3232
withCDKMigrateFixture,
3333
withExtendedTimeoutFixture,
3434
randomString,
35+
withSpecificFixture,
3536
withoutBootstrap,
3637
} from '../../lib';
3738

@@ -2325,6 +2326,84 @@ integTest(
23252326
}),
23262327
);
23272328

2329+
integTest(
2330+
'test cdk rollback',
2331+
withSpecificFixture('rollback-test-app', async (fixture) => {
2332+
let phase = '1';
2333+
2334+
// Should succeed
2335+
await fixture.cdkDeploy('test-rollback', {
2336+
options: ['--no-rollback'],
2337+
modEnv: { PHASE: phase },
2338+
verbose: false,
2339+
});
2340+
try {
2341+
phase = '2a';
2342+
2343+
// Should fail
2344+
const deployOutput = await fixture.cdkDeploy('test-rollback', {
2345+
options: ['--no-rollback'],
2346+
modEnv: { PHASE: phase },
2347+
verbose: false,
2348+
allowErrExit: true,
2349+
});
2350+
expect(deployOutput).toContain('UPDATE_FAILED');
2351+
2352+
// Rollback
2353+
await fixture.cdk(['rollback'], {
2354+
modEnv: { PHASE: phase },
2355+
verbose: false,
2356+
});
2357+
} finally {
2358+
await fixture.cdkDestroy('test-rollback');
2359+
}
2360+
}),
2361+
);
2362+
2363+
integTest(
2364+
'test cdk rollback --force',
2365+
withSpecificFixture('rollback-test-app', async (fixture) => {
2366+
let phase = '1';
2367+
2368+
// Should succeed
2369+
await fixture.cdkDeploy('test-rollback', {
2370+
options: ['--no-rollback'],
2371+
modEnv: { PHASE: phase },
2372+
verbose: false,
2373+
});
2374+
try {
2375+
phase = '2b'; // Fail update and also fail rollback
2376+
2377+
// Should fail
2378+
const deployOutput = await fixture.cdkDeploy('test-rollback', {
2379+
options: ['--no-rollback'],
2380+
modEnv: { PHASE: phase },
2381+
verbose: false,
2382+
allowErrExit: true,
2383+
});
2384+
2385+
expect(deployOutput).toContain('UPDATE_FAILED');
2386+
2387+
// Should still fail
2388+
const rollbackOutput = await fixture.cdk(['rollback'], {
2389+
modEnv: { PHASE: phase },
2390+
verbose: false,
2391+
allowErrExit: true,
2392+
});
2393+
2394+
expect(rollbackOutput).toContain('Failing rollback');
2395+
2396+
// Rollback and force cleanup
2397+
await fixture.cdk(['rollback', '--force'], {
2398+
modEnv: { PHASE: phase },
2399+
verbose: false,
2400+
});
2401+
} finally {
2402+
await fixture.cdkDestroy('test-rollback');
2403+
}
2404+
}),
2405+
);
2406+
23282407
integTest('cdk notices are displayed correctly', withDefaultFixture(async (fixture) => {
23292408

23302409
const cache = {

Diff for: packages/aws-cdk-lib/cx-api/FEATURE_FLAGS.md

+8-8
Original file line numberDiff line numberDiff line change
@@ -1143,7 +1143,7 @@ shipped as part of the runtime environment.
11431143

11441144
*When enabled, will always use the arn for identifiers for CfnSourceApiAssociation in the GraphqlApi construct rather than id.* (fix)
11451145

1146-
When this feature flag is enabled, we use the IGraphqlApi ARN rather than ID when creating or updating CfnSourceApiAssociation in
1146+
When this feature flag is enabled, we use the IGraphqlApi ARN rather than ID when creating or updating CfnSourceApiAssociation in
11471147
the GraphqlApi construct. Using the ARN allows the association to support an association with a source api or merged api in another account.
11481148
Note that for existing source api associations created with this flag disabled, enabling the flag will lead to a resource replacement.
11491149

@@ -1200,7 +1200,7 @@ database cluster from a snapshot.
12001200

12011201
*When enabled, the CodeCommit source action is using the default branch name 'main'.* (fix)
12021202

1203-
When setting up a CodeCommit source action for the source stage of a pipeline, please note that the
1203+
When setting up a CodeCommit source action for the source stage of a pipeline, please note that the
12041204
default branch is 'master'.
12051205
However, with the activation of this feature flag, the default branch is updated to 'main'.
12061206

@@ -1378,7 +1378,7 @@ Other notifications that are not managed by this stack will be kept.
13781378
Currently, 'inputPath' and 'outputPath' from the TaskStateBase Props is being used under BedrockInvokeModelProps to define S3URI under 'input' and 'output' fields
13791379
of State Machine Task definition.
13801380

1381-
When this feature flag is enabled, specify newly introduced props 's3InputUri' and
1381+
When this feature flag is enabled, specify newly introduced props 's3InputUri' and
13821382
's3OutputUri' to populate S3 uri under input and output fields in state machine task definition for Bedrock invoke model.
13831383

13841384

@@ -1413,7 +1413,7 @@ When this feature flag is enabled, we will only grant the necessary permissions
14131413
*When enabled, initOptions.timeout and resourceSignalTimeout values will be summed together.* (fix)
14141414

14151415
Currently is both initOptions.timeout and resourceSignalTimeout are both specified in the options for creating an EC2 Instance,
1416-
only the value from 'resourceSignalTimeout' will be used.
1416+
only the value from 'resourceSignalTimeout' will be used.
14171417

14181418
When this feature flag is enabled, if both initOptions.timeout and resourceSignalTimeout are specified, the values will to be summed together.
14191419

@@ -1428,11 +1428,11 @@ When this feature flag is enabled, if both initOptions.timeout and resourceSigna
14281428

14291429
*When enabled, a Lambda authorizer Permission created when using GraphqlApi will be properly scoped with a SourceArn.* (fix)
14301430

1431-
Currently, when using a Lambda authorizer with an AppSync GraphQL API, the AWS CDK automatically generates the necessary AWS::Lambda::Permission
1432-
to allow the AppSync API to invoke the Lambda authorizer. This permission is overly permissive because it lacks a SourceArn, meaning
1431+
Currently, when using a Lambda authorizer with an AppSync GraphQL API, the AWS CDK automatically generates the necessary AWS::Lambda::Permission
1432+
to allow the AppSync API to invoke the Lambda authorizer. This permission is overly permissive because it lacks a SourceArn, meaning
14331433
it allows invocations from any source.
14341434

1435-
When this feature flag is enabled, the AWS::Lambda::Permission will be properly scoped with the SourceArn corresponding to the
1435+
When this feature flag is enabled, the AWS::Lambda::Permission will be properly scoped with the SourceArn corresponding to the
14361436
specific AppSync GraphQL API.
14371437

14381438

@@ -1446,7 +1446,7 @@ specific AppSync GraphQL API.
14461446

14471447
*When enabled, both `@aws-sdk` and `@smithy` packages will be excluded from the Lambda Node.js 18.x runtime to prevent version mismatches in bundled applications.* (fix)
14481448

1449-
Currently, when bundling Lambda functions with the non-latest runtime that supports AWS SDK JavaScript (v3), only the '@aws-sdk/*' packages are excluded by default.
1449+
Currently, when bundling Lambda functions with the non-latest runtime that supports AWS SDK JavaScript (v3), only the '@aws-sdk/*' packages are excluded by default.
14501450
However, this can cause version mismatches between the '@aws-sdk/*' and '@smithy/*' packages, as they are tightly coupled dependencies in AWS SDK v3.
14511451

14521452
When this feature flag is enabled, both '@aws-sdk/*' and '@smithy/*' packages will be excluded during the bundling process. This ensures that no mismatches

0 commit comments

Comments
 (0)