Skip to content

Commit d64057f

Browse files
authoredDec 6, 2021
feat(core): add applyRemovalPolicy to IResource (#17746)
The motivation behind this change is in both the linked issue and the added test case: change removal policy of a child resource with an interface type. Thanks @skinny85 for pointing me in the right direction. Closes #17728 ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent a1da772 commit d64057f

File tree

4 files changed

+46
-1
lines changed

4 files changed

+46
-1
lines changed
 

‎packages/@aws-cdk/aws-ec2/test/client-vpn-authorization-rule.test.ts

+4
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ describe('ClientVpnAuthorizationRule constructor', () => {
2323
env: { account: 'myAccount', region: 'us-east-1' },
2424
connections: new Connections(),
2525
node: stack.node,
26+
applyRemovalPolicy: () => { },
2627
};
2728
new ClientVpnAuthorizationRule(stack, 'NormalRule', {
2829
cidr: '10.0.10.0/32',
@@ -50,6 +51,7 @@ describe('ClientVpnAuthorizationRule constructor', () => {
5051
env: { account: 'myAccount', region: 'us-east-1' },
5152
connections: new Connections(),
5253
node: stack.node,
54+
applyRemovalPolicy: () => { },
5355
};
5456
const clientVpnEndpoint: IClientVpnEndpoint = {
5557
endpointId: 'myClientVpnEndpoint',
@@ -58,6 +60,7 @@ describe('ClientVpnAuthorizationRule constructor', () => {
5860
env: { account: 'myAccount', region: 'us-east-1' },
5961
connections: new Connections(),
6062
node: stack.node,
63+
applyRemovalPolicy: () => { },
6164
};
6265
expect(() => {
6366
new ClientVpnAuthorizationRule(stack, 'RuleBothEndointAndEndpoint', {
@@ -88,6 +91,7 @@ describe('ClientVpnAuthorizationRule constructor', () => {
8891
env: { account: 'myAccount', region: 'us-east-1' },
8992
connections: new Connections(),
9093
node: stack.node,
94+
applyRemovalPolicy: () => { },
9195
};
9296
new ClientVpnAuthorizationRule(stack, 'RuleWithEndointTypo', {
9397
cidr: '10.0.10.0/32',

‎packages/@aws-cdk/aws-lambda/lib/function-base.ts

+1
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,7 @@ export abstract class FunctionBase extends Resource implements IFunction, ec2.IC
334334
node: this.node,
335335
stack: this.stack,
336336
env: this.env,
337+
applyRemovalPolicy: this.applyRemovalPolicy,
337338
},
338339
});
339340
this._invocationGrants[identifier] = grant;

‎packages/@aws-cdk/core/lib/resource.ts

+13
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,19 @@ export interface IResource extends IConstruct {
5858
* that might be different than the stack they were imported into.
5959
*/
6060
readonly env: ResourceEnvironment;
61+
62+
/**
63+
* Apply the given removal policy to this resource
64+
*
65+
* The Removal Policy controls what happens to this resource when it stops
66+
* being managed by CloudFormation, either because you've removed it from the
67+
* CDK application or because you've made a change that requires the resource
68+
* to be replaced.
69+
*
70+
* The resource can be deleted (`RemovalPolicy.DESTROY`), or left in your AWS
71+
* account for data recovery and cleanup later (`RemovalPolicy.RETAIN`).
72+
*/
73+
applyRemovalPolicy(policy: RemovalPolicy): void;
6174
}
6275

6376
/**

‎packages/@aws-cdk/core/test/resource.test.ts

+28-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as cxapi from '@aws-cdk/cx-api';
22
import {
33
App, App as Root, CfnCondition,
44
CfnDeletionPolicy, CfnResource, Construct,
5-
Fn, RemovalPolicy, Resource, Stack,
5+
Fn, IResource, RemovalPolicy, Resource, Stack,
66
} from '../lib';
77
import { synthesize } from '../lib/private/synthesis';
88
import { toCloudFormation } from './util';
@@ -318,6 +318,33 @@ describe('resource', () => {
318318

319319
});
320320

321+
test('applyRemovalPolicy available for interface resources', () => {
322+
class Child extends Resource {
323+
constructor(scope: Construct, id: string) {
324+
super(scope, id);
325+
326+
new CfnResource(this, 'Resource', {
327+
type: 'ChildResourceType',
328+
});
329+
}
330+
}
331+
332+
const stack = new Stack();
333+
const child: IResource = new Child(stack, 'Child');
334+
335+
child.applyRemovalPolicy(RemovalPolicy.RETAIN);
336+
337+
expect(toCloudFormation(stack)).toEqual({
338+
Resources: {
339+
ChildDAB30558: {
340+
DeletionPolicy: 'Retain',
341+
Type: 'ChildResourceType',
342+
UpdateReplacePolicy: 'Retain',
343+
},
344+
},
345+
});
346+
});
347+
321348
test('addDependency adds all dependencyElements of dependent constructs', () => {
322349

323350
class C1 extends Construct {

0 commit comments

Comments
 (0)
Please sign in to comment.