aws / aws/aws-cdk

(core): Ability to set Stack termination protection on Stage level (and inherit by Stacks within it)

Open
#31,449 3 comments 11 reactions 0 assignees View on GitHub
@aws-cdk/core effort/medium feature-request p2
Dominant language
TypeScript
Stars
12.9k
Forks
4.6k
Avg merge
1d 19h
Merged PRs (30d)
71

Description

### Describe the feature

Add ability to define `Stack` (default) **termination protection** via (parent) `Stage`, as demonstrated by an example test scenario:

```ts
test('Stack inherits termination protection from Stage', () => {
// GIVEN
const app = new App();
const stage = new Stage(app, 'Stage', {
terminationProtection: true,
});

// WHEN
const stack1 = new Stack(stage, 'Stack1');

// THEN
expect(stack1.terminationProtection).toEqual(true);
});
```

Additionally any inner `Stage` and/or `Stack` within the (parent) Stage scope can override the inherited value.

### Use Case

Stages are a mechanism to "group" Stacks together. There are multiple use cases / reasons to group stacks using Stages, but one of them is to define an "application environment" (not to be confused with `cdk.Environment`) via a Stage: Think of `development` or `production` Stage etc.

The specific Stage use case / reason, doesn't matter that much, but often it would be handy to be able to define Termination Protection for all Stacks within the scope of specific Stage.

#### Not doable via Aspects

One could make the statement, that this is achievable via Aspects, but as stacks default to having `terminationProtection: false`, there is no reasonable way (at least AFAIK) to detect on a stack level if termination protection is set to `false` explicitly through props or implicitly by the stack constructor. Hence the idea of a feature where one can define (the default) termination protection on Stage level, which is then inherited by all its child Stages (if any) and finally by the stacks within that Stage scope; Additionally one should be able to override the Stage-level (inherited) termination protection on Stack level.

### Proposed Solution

1. Introduce optional `terminationProtection` into `StageProps`
2. In `Stage` constructor, either use the termination protection value from its props (if set) or inherit from parent stage
3. In `Stack` constructor, either use the termination protection value from its props (if set), or inherit from parent stage, or finally default to `false` (as before)

Basically, allow this:
```ts
new Stage(app, 'Stage', {
terminationProtection: true,
});
```

~~See PR #31450.~~ (Outdated)
**See PR #33239** (New pull request, passing all the checks and quality gates)

### Other Information

I think this feature counts as a "non-breaking new feature", as this has not been possible before.

### Acknowledgements

- [X] I may be able to implement this feature request (see PR #33239)
- [ ] This feature might incur a breaking change

### CDK version used

v2.158.0

### Environment details (OS name and version, etc.)

macOS 14.6.1

Contributor guide

Open the contributing guide

Research direction

Start with the Stage and Stack constructors and the provided “Stack inherits termination protection from Stage” test scenario. Verify inheritance through nested Stages, Stack-level overrides, and the existing false default; done means the example and override behavior pass while existing behavior remains unchanged.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
infrastructure
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.