Skip to content

Commit 79881c5

Browse files
authored
feat(custom-resource): expose removalPolicy (#25235)
Added `removalPolicy` property to `AwsCustomResource`. Closes #25220. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent c22bd32 commit 79881c5

File tree

3 files changed

+52
-2
lines changed

3 files changed

+52
-2
lines changed

packages/aws-cdk-lib/custom-resources/README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,7 @@ In both the cases, you will get a synth time error if you attempt to use it in c
513513

514514
### Customizing the Lambda function implementing the custom resource
515515

516-
Use the `role`, `timeout`, `logRetention` and `functionName` properties to customize
516+
Use the `role`, `timeout`, `logRetention`, `functionName` and `removalPolicy` properties to customize
517517
the Lambda function implementing the custom resource:
518518

519519
```ts
@@ -523,6 +523,7 @@ new cr.AwsCustomResource(this, 'Customized', {
523523
timeout: Duration.minutes(10), // defaults to 2 minutes
524524
logRetention: logs.RetentionDays.ONE_WEEK, // defaults to never delete logs
525525
functionName: 'my-custom-name', // defaults to a CloudFormation generated name
526+
removalPolicy: cdk.RemovalPolicy.RETAIN, // defaults to `cdk.RemovalPolicy.DESTROY`
526527
policy: cr.AwsCustomResourcePolicy.fromSdkCalls({
527528
resources: cr.AwsCustomResourcePolicy.ANY_RESOURCE,
528529
}),

packages/aws-cdk-lib/custom-resources/lib/aws-custom-resource/aws-custom-resource.ts

+8
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,13 @@ export interface AwsCustomResourceProps {
325325
*/
326326
readonly functionName?: string;
327327

328+
/**
329+
* The policy to apply when this resource is removed from the application.
330+
*
331+
* @default cdk.RemovalPolicy.Destroy
332+
*/
333+
readonly removalPolicy?: cdk.RemovalPolicy;
334+
328335
/**
329336
* The vpc to provision the lambda function in.
330337
*
@@ -440,6 +447,7 @@ export class AwsCustomResource extends Construct implements iam.IGrantable {
440447
resourceType: props.resourceType || 'Custom::AWS',
441448
serviceToken: provider.functionArn,
442449
pascalCaseProperties: true,
450+
removalPolicy: props.removalPolicy,
443451
properties: {
444452
create: create && this.encodeJson(create),
445453
update: props.onUpdate && this.encodeJson(props.onUpdate),

packages/aws-cdk-lib/custom-resources/test/aws-custom-resource/aws-custom-resource.test.ts

+42-1
Original file line numberDiff line numberDiff line change
@@ -1237,4 +1237,45 @@ test.each([
12371237
Template.fromStack(stack).hasResourceProperties('Custom::LogRetentionPolicy', {
12381238
'InstallLatestAwsSdk': expected,
12391239
});
1240-
});
1240+
});
1241+
1242+
test('default removal policy is DESTROY', () => {
1243+
// GIVEN
1244+
const stack = new cdk.Stack();
1245+
1246+
// WHEN
1247+
new AwsCustomResource(stack, 'AwsSdk', {
1248+
onCreate: {
1249+
service: 'service',
1250+
action: 'action',
1251+
physicalResourceId: PhysicalResourceId.of('id'),
1252+
},
1253+
policy: AwsCustomResourcePolicy.fromSdkCalls({ resources: AwsCustomResourcePolicy.ANY_RESOURCE }),
1254+
});
1255+
1256+
// THEN
1257+
Template.fromStack(stack).hasResource('Custom::AWS', {
1258+
DeletionPolicy: 'Delete',
1259+
});
1260+
});
1261+
1262+
test('can specify removal policy', () => {
1263+
// GIVEN
1264+
const stack = new cdk.Stack();
1265+
1266+
// WHEN
1267+
new AwsCustomResource(stack, 'AwsSdk', {
1268+
onCreate: {
1269+
service: 'service',
1270+
action: 'action',
1271+
physicalResourceId: PhysicalResourceId.of('id'),
1272+
},
1273+
policy: AwsCustomResourcePolicy.fromSdkCalls({ resources: AwsCustomResourcePolicy.ANY_RESOURCE }),
1274+
removalPolicy: cdk.RemovalPolicy.RETAIN,
1275+
});
1276+
1277+
// THEN
1278+
Template.fromStack(stack).hasResource('Custom::AWS', {
1279+
DeletionPolicy: 'Retain',
1280+
});
1281+
});

0 commit comments

Comments
 (0)