Skip to content
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(core): Add RetainExceptOnCreate removal policy #26595

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions packages/aws-cdk-lib/core/lib/removal-policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,16 @@ export enum RemovalPolicy {
* @see https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-attribute-deletionpolicy.html#aws-attribute-deletionpolicy-options
*/
SNAPSHOT = 'snapshot',

/**
* RetainExceptOnCreate behaves like Retain for stack operations, except for the stack operation that initially created the resource.
* If the stack operation that created the resource is rolled back, CloudFormation deletes the resource. For all other stack operations,
* such as stack deletion, CloudFormation retains the resource and its contents. The result is that new, empty, and unused resources are deleted,
* while in-use resources and their data are retained.
*
* @see https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-attribute-deletionpolicy.html#aws-attribute-deletionpolicy-options
*/
RETAIN_EXCEPT_ON_CREATE = 'RetainExceptOnCreate',
}

export interface RemovalPolicyOptions {
Expand Down
32 changes: 32 additions & 0 deletions packages/aws-cdk-lib/core/test/removal-policy.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { toCloudFormation } from './util';
import { CfnResource, Stack, RemovalPolicy } from '../lib';

describe('removal policy', () => {
const removalPolicies = [
RemovalPolicy.RETAIN,
RemovalPolicy.DESTROY,
RemovalPolicy.SNAPSHOT,
RemovalPolicy.RETAIN_EXCEPT_ON_CREATE,
];
removalPolicies.forEach((policy) => {
test(`should handle RemovalPolicy.${policy}`, () => {
const stack = new Stack();

new CfnResource(stack, 'Resource', {
type: 'MOCK',
properties: {
RemovalPolicy: policy,
},
});

expect(toCloudFormation(stack)).toEqual({
Resources: {
Resource: {
Type: 'MOCK',
Properties: { RemovalPolicy: policy.valueOf() },
},
},
});
});
});
});
Loading