Skip to content

Commit e0d5cad

Browse files
committed
Add RetainExceptOnCreate removal policy
1 parent 02e04a6 commit e0d5cad

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

packages/aws-cdk-lib/core/lib/removal-policy.ts

+10
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,16 @@ export enum RemovalPolicy {
4848
* @see https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-attribute-deletionpolicy.html#aws-attribute-deletionpolicy-options
4949
*/
5050
SNAPSHOT = 'snapshot',
51+
52+
/**
53+
* RetainExceptOnCreate behaves like Retain for stack operations, except for the stack operation that initially created the resource.
54+
* If the stack operation that created the resource is rolled back, CloudFormation deletes the resource. For all other stack operations,
55+
* such as stack deletion, CloudFormation retains the resource and its contents. The result is that new, empty, and unused resources are deleted,
56+
* while in-use resources and their data are retained.
57+
*
58+
* @see https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-attribute-deletionpolicy.html#aws-attribute-deletionpolicy-options
59+
*/
60+
RETAIN_EXCEPT_ON_CREATE = 'RetainExceptOnCreate',
5161
}
5262

5363
export interface RemovalPolicyOptions {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { toCloudFormation } from './util';
2+
import { CfnResource, Stack, RemovalPolicy } from '../lib';
3+
4+
describe('removal policy', () => {
5+
const removalPolicies = [
6+
RemovalPolicy.RETAIN,
7+
RemovalPolicy.DESTROY,
8+
RemovalPolicy.SNAPSHOT,
9+
RemovalPolicy.RETAIN_EXCEPT_ON_CREATE,
10+
];
11+
removalPolicies.forEach((policy) => {
12+
test(`should handle RemovalPolicy.${policy}`, () => {
13+
const stack = new Stack();
14+
15+
new CfnResource(stack, 'Resource', {
16+
type: 'MOCK',
17+
properties: {
18+
RemovalPolicy: policy,
19+
},
20+
});
21+
22+
expect(toCloudFormation(stack)).toEqual({
23+
Resources: {
24+
Resource: {
25+
Type: 'MOCK',
26+
Properties: { RemovalPolicy: policy.valueOf() },
27+
},
28+
},
29+
});
30+
});
31+
});
32+
});

0 commit comments

Comments
 (0)