-
Notifications
You must be signed in to change notification settings - Fork 4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(cli): cdk rollback
#31407
Merged
Merged
feat(cli): cdk rollback
#31407
Changes from 12 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
537cb02
feat(cli): `cdk rollback`
rix0rrr c16c7a8
Fix tests
rix0rrr 99bc2d9
Merge remote-tracking branch 'origin/main' into huijbers/cli-rollback
rix0rrr 4245ff5
Unit tests
rix0rrr be22319
Remove unused imports
rix0rrr c6d9b19
Fix fake class incompatibilities
rix0rrr bf7bef3
Initial set of review comments
rix0rrr 55f7800
Merge remote-tracking branch 'origin/main' into huijbers/cli-rollback
rix0rrr 75acb87
Add integ test without --force
rix0rrr 453bb89
Prevent infinite loop
rix0rrr ae05b6f
Make ROLLBACK_FAILED an error
rix0rrr 3d3e97d
No error
rix0rrr 18c7ea6
Update docs
rix0rrr 9fcab7c
Merge branch 'main' into huijbers/cli-rollback
mergify[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
packages/@aws-cdk-testing/cli-integ/resources/cdk-apps/rollback-test-app/app.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
const cdk = require('aws-cdk-lib'); | ||
const lambda = require('aws-cdk-lib/aws-lambda'); | ||
const cr = require('aws-cdk-lib/custom-resources'); | ||
|
||
/** | ||
* This stack will be deployed in multiple phases, to achieve a very specific effect | ||
* | ||
* It contains resources r1 and r2, where r1 gets deployed first. | ||
* | ||
* - PHASE = 1: both resources deploy regularly. | ||
* - PHASE = 2: r1 gets updated, r2 will fail to update, and r1 will fail its rollback. | ||
* | ||
* To exercise this app: | ||
* | ||
* ``` | ||
* env PHASE=1 npx cdk deploy | ||
* env PHASE=2 npx cdk deploy --no-rollback | ||
* # This will leave the stack in UPDATE_FAILED | ||
* | ||
* env PHASE=2 npx cdk rollback | ||
* # This will start a rollback that will fail because r1 fails its rollabck | ||
* | ||
* env PHASE=2 npx cdk rollback --force | ||
* # This will retry the rollabck and skip r1 | ||
* ``` | ||
*/ | ||
class RollbacktestStack extends cdk.Stack { | ||
constructor(scope, id, props) { | ||
super(scope, id, props); | ||
|
||
let r1props = {}; | ||
let r2props = {}; | ||
|
||
const phase = process.env.PHASE; | ||
switch (phase) { | ||
case '1': | ||
// Normal deployment | ||
break; | ||
case '2a': | ||
// r1 updates normally, r2 fails updating | ||
r2props.FailUpdate = true; | ||
break; | ||
case '2b': | ||
// r1 updates normally, r2 fails updating, r1 fails rollback | ||
r1props.FailRollback = true; | ||
r2props.FailUpdate = true; | ||
break; | ||
} | ||
|
||
const fn = new lambda.Function(this, 'Fun', { | ||
runtime: lambda.Runtime.NODEJS_LATEST, | ||
code: lambda.Code.fromInline(`exports.handler = async function(event, ctx) { | ||
const key = \`Fail\${event.RequestType}\`; | ||
if (event.ResourceProperties[key]) { | ||
throw new Error(\`\${event.RequestType} fails!\`); | ||
} | ||
if (event.OldResourceProperties?.FailRollback) { | ||
throw new Error('Failing rollback!'); | ||
} | ||
return {}; | ||
}`), | ||
handler: 'index.handler', | ||
timeout: cdk.Duration.minutes(1), | ||
}); | ||
const provider = new cr.Provider(this, "MyProvider", { | ||
onEventHandler: fn, | ||
}); | ||
|
||
const r1 = new cdk.CustomResource(this, 'r1', { | ||
serviceToken: provider.serviceToken, | ||
properties: r1props, | ||
}); | ||
const r2 = new cdk.CustomResource(this, 'r2', { | ||
serviceToken: provider.serviceToken, | ||
properties: r2props, | ||
}); | ||
r2.node.addDependency(r1); | ||
} | ||
} | ||
|
||
const app = new cdk.App({ | ||
context: { | ||
'@aws-cdk/core:assetHashSalt': process.env.CODEBUILD_BUILD_ID, // Force all assets to be unique, but consistent in one build | ||
}, | ||
}); | ||
|
||
const defaultEnv = { | ||
account: process.env.CDK_DEFAULT_ACCOUNT, | ||
region: process.env.CDK_DEFAULT_REGION | ||
}; | ||
|
||
const stackPrefix = process.env.STACK_NAME_PREFIX; | ||
if (!stackPrefix) { | ||
throw new Error(`the STACK_NAME_PREFIX environment variable is required`); | ||
} | ||
|
||
// Sometimes we don't want to synthesize all stacks because it will impact the results | ||
new RollbacktestStack(app, `${stackPrefix}-test-rollback`, { env: defaultEnv }); | ||
app.synth(); |
7 changes: 7 additions & 0 deletions
7
packages/@aws-cdk-testing/cli-integ/resources/cdk-apps/rollback-test-app/cdk.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"app": "node app.js", | ||
"versionReporting": false, | ||
"context": { | ||
"aws-cdk:enableDiffNoFail": "true" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Needs to be adjusted for phases
2a
and2b
. I'm good with just removing this and let the tests speak for themselves.