Skip to content

Commit

Permalink
Add RetainExceptOnCreate removal policy
Browse files Browse the repository at this point in the history
  • Loading branch information
pahud committed Aug 1, 2023
1 parent 02e04a6 commit e0d5cad
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
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() },
},
},
});
});
});
});

0 comments on commit e0d5cad

Please sign in to comment.