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): new RemovalPolicy.RETAIN_EXCEPT_ON_CREATE to only retain resources that have been successfully created #26602

Merged
merged 5 commits into from
Aug 3, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
8 changes: 8 additions & 0 deletions packages/aws-cdk-lib/core/lib/cfn-resource-policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,14 @@ export enum CfnDeletionPolicy {
*/
RETAIN = 'Retain',

/**
* 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.
*/
RETAIN_EXCEPT_ON_CREATE = 'RetainExceptOnCreate',

/**
* For resources that support snapshots (AWS::EC2::Volume, AWS::ElastiCache::CacheCluster, AWS::ElastiCache::ReplicationGroup,
* AWS::RDS::DBInstance, AWS::RDS::DBCluster, and AWS::Redshift::Cluster), AWS CloudFormation creates a snapshot for the
Expand Down
11 changes: 10 additions & 1 deletion packages/aws-cdk-lib/core/lib/cfn-resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,14 +120,22 @@ export class CfnResource extends CfnRefElement {
policy = policy || options.default || RemovalPolicy.RETAIN;

let deletionPolicy;
let updateReplacePolicy;

switch (policy) {
case RemovalPolicy.DESTROY:
deletionPolicy = CfnDeletionPolicy.DELETE;
updateReplacePolicy = CfnDeletionPolicy.DELETE;
break;

case RemovalPolicy.RETAIN:
deletionPolicy = CfnDeletionPolicy.RETAIN;
updateReplacePolicy = CfnDeletionPolicy.RETAIN;
break;

case RemovalPolicy.RETAIN_EXISTING:
mrgrain marked this conversation as resolved.
Show resolved Hide resolved
deletionPolicy = CfnDeletionPolicy.RETAIN_EXCEPT_ON_CREATE;
updateReplacePolicy = CfnDeletionPolicy.RETAIN;
break;

case RemovalPolicy.SNAPSHOT:
Expand All @@ -153,6 +161,7 @@ export class CfnResource extends CfnRefElement {
}

deletionPolicy = CfnDeletionPolicy.SNAPSHOT;
updateReplacePolicy = CfnDeletionPolicy.SNAPSHOT;
break;

default:
Expand All @@ -161,7 +170,7 @@ export class CfnResource extends CfnRefElement {

this.cfnOptions.deletionPolicy = deletionPolicy;
if (options.applyToUpdateReplacePolicy !== false) {
this.cfnOptions.updateReplacePolicy = deletionPolicy;
this.cfnOptions.updateReplacePolicy = updateReplacePolicy;
}
}

Expand Down
13 changes: 13 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,19 @@ export enum RemovalPolicy {
* @see https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-attribute-deletionpolicy.html#aws-attribute-deletionpolicy-options
*/
SNAPSHOT = 'snapshot',

/**
* This uses the 'RetainExceptOnCreate' DeletionPolicy which behaves like Retain for stack operations,
* except for the stack operation that initially created the resource.
* When `applyToUpdateReplacePolicy` is set, this uses the 'Retain' UpdateReplacePolicy.
*
* If the stack operation that created the resource is rolled back, it will delete the resource.
* For all other stack operations, such as stack deletion, it 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= 'retain-except-on-create',
mrgrain marked this conversation as resolved.
Show resolved Hide resolved
}

export interface RemovalPolicyOptions {
Expand Down
48 changes: 48 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,48 @@
import { toCloudFormation } from './util';
import { CfnResource, Stack, RemovalPolicy } from '../lib';

describe('removal policy', () => {
test.each([
[RemovalPolicy.RETAIN, 'Retain'],
[RemovalPolicy.DESTROY, 'Delete'],
[RemovalPolicy.SNAPSHOT, 'Snapshot'],
[RemovalPolicy.RETAIN_EXCEPT_ON_CREATE, 'RetainExceptOnCreate'],
])('should set correct DeletionPolicy for RemovalPolicy.%s', (removalPolicy: RemovalPolicy, deletionPolicy: string) => {
const stack = new Stack();

const resource = new CfnResource(stack, 'Resource', { type: 'MOCK' });
resource.applyRemovalPolicy(removalPolicy);

expect(toCloudFormation(stack)).toEqual({
Resources: {
Resource: expect.objectContaining({
Type: 'MOCK',
DeletionPolicy: deletionPolicy,
}),
},
});
});

test.each([
[RemovalPolicy.RETAIN, 'Retain'],
[RemovalPolicy.DESTROY, 'Delete'],
[RemovalPolicy.SNAPSHOT, 'Snapshot'],
[RemovalPolicy.RETAIN_EXCEPT_ON_CREATE, 'Retain'],
])('should set correct UpdateReplacePolicy for RemovalPolicy.%s', (removalPolicy: RemovalPolicy, updateReplacePolicy: string) => {
const stack = new Stack();

const resource = new CfnResource(stack, 'Resource', { type: 'MOCK' });
resource.applyRemovalPolicy(removalPolicy, {
applyToUpdateReplacePolicy: true,
});

expect(toCloudFormation(stack)).toEqual({
Resources: {
Resource: expect.objectContaining({
Type: 'MOCK',
UpdateReplacePolicy: updateReplacePolicy,
}),
},
});
});
});
Loading