Skip to content

Commit

Permalink
feat(core): allow setting termination protection on stacks
Browse files Browse the repository at this point in the history
This commit adds a getter and setter for Stack termination
protection. This allows termination protection to be configured
after the Stack has been constructed.

fixes aws#14463
  • Loading branch information
cjihrig committed May 16, 2021
1 parent fbaeaba commit a1ccfa4
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 6 deletions.
3 changes: 3 additions & 0 deletions packages/@aws-cdk/core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -864,6 +864,9 @@ on a stack by setting the `terminationProtection` prop to `true`.
const stack = new Stack(app, 'StackName', {
terminationProtection: true,
});
// or
stack.terminationProtection = true;
```

By default, termination protection is disabled.
Expand Down
23 changes: 21 additions & 2 deletions packages/@aws-cdk/core/lib/stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ export class Stack extends CoreConstruct implements ITaggable {
/**
* Whether termination protection is enabled for this stack.
*/
public readonly terminationProtection?: boolean;
private _terminationProtection: boolean;

/**
* If this is a nested stack, this represents its `AWS::CloudFormation::Stack`
Expand Down Expand Up @@ -358,7 +358,7 @@ export class Stack extends CoreConstruct implements ITaggable {
this.account = account;
this.region = region;
this.environment = environment;
this.terminationProtection = props.terminationProtection;
this._terminationProtection = !!props.terminationProtection;

if (props.description !== undefined) {
// Max length 1024 bytes
Expand Down Expand Up @@ -689,6 +689,25 @@ export class Stack extends CoreConstruct implements ITaggable {
return this.nestedStackParent;
}

/**
* Whether termination protection is enabled for this stack.
*
* @returns The current termination protection value for this stack.
*/
public get terminationProtection(): boolean {
return this._terminationProtection;
}

/**
* Enables or disables termination protection for this stack.
*
* @param enableTerminationProtection The new termination protection value
* for this stack.
*/
public set terminationProtection(enableTerminationProtection: boolean) {
this._terminationProtection = enableTerminationProtection;
}

/**
* Add a Transform to this stack. A Transform is a macro that AWS
* CloudFormation uses to process your template.
Expand Down
25 changes: 21 additions & 4 deletions packages/@aws-cdk/core/test/stack.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1095,12 +1095,29 @@ describe('stack', () => {
test('Termination Protection is reflected in Cloud Assembly artifact', () => {
// if the root is an app, invoke "synth" to avoid double synthesis
const app = new App();
const stack = new Stack(app, 'Stack', { terminationProtection: true });
const stack1 = new Stack(app, 'Stack1');
const stack2 = new Stack(app, 'Stack2', { terminationProtection: false });
const stack3 = new Stack(app, 'Stack3', { terminationProtection: true });
const stack4 = new Stack(app, 'Stack4');

const assembly = app.synth();
const artifact = assembly.getStackArtifact(stack.artifactId);
expect(stack1.terminationProtection).toEqual(false);
expect(stack2.terminationProtection).toEqual(false);
expect(stack3.terminationProtection).toEqual(true);
expect(stack4.terminationProtection).toEqual(false);

expect(artifact.terminationProtection).toEqual(true);
stack4.terminationProtection = true;
expect(stack4.terminationProtection).toEqual(true);

const assembly = app.synth();
const artifact1 = assembly.getStackArtifact(stack1.artifactId);
const artifact2 = assembly.getStackArtifact(stack2.artifactId);
const artifact3 = assembly.getStackArtifact(stack3.artifactId);
const artifact4 = assembly.getStackArtifact(stack4.artifactId);

expect(artifact1.terminationProtection).toEqual(false);
expect(artifact2.terminationProtection).toEqual(false);
expect(artifact3.terminationProtection).toEqual(true);
expect(artifact4.terminationProtection).toEqual(true);


});
Expand Down
1 change: 1 addition & 0 deletions packages/@aws-cdk/core/test/synthesis.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ nodeunitShim({
environment: 'aws://unknown-account/unknown-region',
properties: {
templateFile: 'one-stack.template.json',
terminationProtection: false,
validateOnSynth: false,
},
displayName: 'one-stack',
Expand Down

0 comments on commit a1ccfa4

Please sign in to comment.