Skip to content

Commit

Permalink
fix(core): termination protection not updated when change set has no …
Browse files Browse the repository at this point in the history
…changes

Move termination protection before early return when change set has no changes
  • Loading branch information
jogold committed May 29, 2020
1 parent 9ee61eb commit 3eccb05
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 18 deletions.
22 changes: 11 additions & 11 deletions packages/aws-cdk/lib/api/deploy-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,17 @@ export async function deployStack(options: DeployStackOptions): Promise<DeploySt
debug('Initiated creation of changeset: %s; waiting for it to finish creating...', changeSet.Id);
const changeSetDescription = await waitForChangeSet(cfn, deployName, changeSetName);

// Update termination protection only if it has changed.
const terminationProtection = stackArtifact.terminationProtection ?? false;
if (cloudFormationStack.terminationProtection !== terminationProtection) {
debug('Updating termination protection from %s to %s for stack %s', cloudFormationStack.terminationProtection, terminationProtection, deployName);
await cfn.updateTerminationProtection({
StackName: deployName,
EnableTerminationProtection: terminationProtection,
}).promise();
debug('Termination protection updated to %s for stack %s', terminationProtection, deployName);
}

if (changeSetHasNoChanges(changeSetDescription)) {
debug('No changes are to be performed on %s.', deployName);
await cfn.deleteChangeSet({ StackName: deployName, ChangeSetName: changeSetName }).promise();
Expand Down Expand Up @@ -262,17 +273,6 @@ export async function deployStack(options: DeployStackOptions): Promise<DeploySt
print('Changeset %s created and waiting in review for manual execution (--no-execute)', changeSetName);
}

// Update termination protection only if it has changed.
const terminationProtection = stackArtifact.terminationProtection ?? false;
if (cloudFormationStack.terminationProtection !== terminationProtection) {
debug('Updating termination protection from %s to %s for stack %s', cloudFormationStack.terminationProtection, terminationProtection, deployName);
await cfn.updateTerminationProtection({
StackName: deployName,
EnableTerminationProtection: terminationProtection,
}).promise();
debug('Termination protection updated to %s for stack %s', terminationProtection, deployName);
}

return { noOp: false, outputs: cloudFormationStack.outputs, stackArn: changeSet.StackId!, stackArtifact };
}

Expand Down
2 changes: 1 addition & 1 deletion packages/aws-cdk/test/integ/cli/app/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ new StackWithNestedStack(app, `${stackPrefix}-with-nested-stack`);
new StackWithNestedStackUsingParameters(app, `${stackPrefix}-with-nested-stack-using-parameters`);

new YourStack(app, `${stackPrefix}-termination-protection`, {
terminationProtection: true,
terminationProtection: process.env.NO_TERMINATION_PROTECTION ? false : true,
});

app.synth();
12 changes: 6 additions & 6 deletions packages/aws-cdk/test/integ/cli/cli.integtest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@ test('Two ways of shoing the version', async () => {
});

test('Termination protection', async () => {
await cdkDeploy('termination-protection');
const stackName = 'termination-protection';
await cdkDeploy(stackName);

// Try a destroy that should fail
await expect(cdkDestroy('termination-protection')).rejects.toThrow('exited with error');
await expect(cdkDestroy(stackName)).rejects.toThrow('exited with error');

await cloudFormation('updateTerminationProtection', {
EnableTerminationProtection: false,
StackName: fullStackName('termination-protection'),
});
// Can update termination protection even though the change set doesn't contain changes
await cdkDeploy(stackName, { modEnv: { NO_TERMINATION_PROTECTION: 'TRUE' } });
await cdkDestroy(stackName);
});

test('cdk synth', async () => {
Expand Down

0 comments on commit 3eccb05

Please sign in to comment.