Skip to content

Commit 0755561

Browse files
authored
feat(cli): cdk rollback (#31407)
Add a CLI feature to roll a stuck change back. This is mostly useful for deployments performed using `--no-rollback`: if a failure occurs, the stack gets stuck in an `UPDATE_FAILED` state from which there are 2 options: - Try again using a new template - Roll back to the last stable state There used to be no way to perform the second operation using the CDK CLI, but there now is. `cdk rollback` works in 2 situations: - A paused fail state; it will initiating a fresh rollback (on `CREATE_FAILED`, `UPDATE_FAILED`). - A paused rollback state; it will retry the rollback, optionally skipping some resources (on `UPDATE_ROLLBACK_FAILED` -- it seems there is no way to continue a rollback in `ROLLBACK_FAILED` state). `cdk rollback --orphan <logicalid>` can be used to skip resource rollbacks that are causing problems. `cdk rollback --force` will look up all failed resources and continue skipping them until the rollback has finished. This change requires new bootstrap permissions, so the bootstrap stack is updated to add the following IAM permissions to the `deploy-action` role: ``` - cloudformation:RollbackStack - cloudformation:ContinueUpdateRollback ``` These are necessary to call the 2 CloudFormation APIs that start and continue a rollback. Relates to (but does not close yet) #30546. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 7e80cc9 commit 0755561

20 files changed

+1121
-218
lines changed

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

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+
}

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

@@ -2260,6 +2261,84 @@ integTest(
22602261
}),
22612262
);
22622263

2264+
integTest(
2265+
'test cdk rollback',
2266+
withSpecificFixture('rollback-test-app', async (fixture) => {
2267+
let phase = '1';
2268+
2269+
// Should succeed
2270+
await fixture.cdkDeploy('test-rollback', {
2271+
options: ['--no-rollback'],
2272+
modEnv: { PHASE: phase },
2273+
verbose: false,
2274+
});
2275+
try {
2276+
phase = '2a';
2277+
2278+
// Should fail
2279+
const deployOutput = await fixture.cdkDeploy('test-rollback', {
2280+
options: ['--no-rollback'],
2281+
modEnv: { PHASE: phase },
2282+
verbose: false,
2283+
allowErrExit: true,
2284+
});
2285+
expect(deployOutput).toContain('UPDATE_FAILED');
2286+
2287+
// Rollback
2288+
await fixture.cdk(['rollback'], {
2289+
modEnv: { PHASE: phase },
2290+
verbose: false,
2291+
});
2292+
} finally {
2293+
await fixture.cdkDestroy('test-rollback');
2294+
}
2295+
}),
2296+
);
2297+
2298+
integTest(
2299+
'test cdk rollback --force',
2300+
withSpecificFixture('rollback-test-app', async (fixture) => {
2301+
let phase = '1';
2302+
2303+
// Should succeed
2304+
await fixture.cdkDeploy('test-rollback', {
2305+
options: ['--no-rollback'],
2306+
modEnv: { PHASE: phase },
2307+
verbose: false,
2308+
});
2309+
try {
2310+
phase = '2b'; // Fail update and also fail rollback
2311+
2312+
// Should fail
2313+
const deployOutput = await fixture.cdkDeploy('test-rollback', {
2314+
options: ['--no-rollback'],
2315+
modEnv: { PHASE: phase },
2316+
verbose: false,
2317+
allowErrExit: true,
2318+
});
2319+
2320+
expect(deployOutput).toContain('UPDATE_FAILED');
2321+
2322+
// Should still fail
2323+
const rollbackOutput = await fixture.cdk(['rollback'], {
2324+
modEnv: { PHASE: phase },
2325+
verbose: false,
2326+
allowErrExit: true,
2327+
});
2328+
2329+
expect(rollbackOutput).toContain('Failing rollback');
2330+
2331+
// Rollback and force cleanup
2332+
await fixture.cdk(['rollback', '--force'], {
2333+
modEnv: { PHASE: phase },
2334+
verbose: false,
2335+
});
2336+
} finally {
2337+
await fixture.cdkDestroy('test-rollback');
2338+
}
2339+
}),
2340+
);
2341+
22632342
integTest('cdk notices are displayed correctly', withDefaultFixture(async (fixture) => {
22642343

22652344
const cache = {

0 commit comments

Comments
 (0)