Skip to content

Commit 34c547c

Browse files
authored
feat(core): RemovalPolicies.of(scope) (#32283)
## Issue # (if applicable) N/A - New feature proposal ## Reason for this change Currently, applying removal policies to multiple resources requires setting them individually or using Tags as a workaround. This change introduces a new RemovalPolicies module that provides a more intuitive and type-safe way to manage removal policies across multiple resources, similar to the existing Tags API. ## Description of changes Added a new RemovalPolicies module that provides: - A similar interface to Tags.of() for managing removal policies - Type-safe resource type specifications using CloudFormation resource type strings - Ability to include or exclude specific resource types - Convenient methods for common removal policies (destroy, retain, snapshot, retainOnUpdateOrDelete) Example usage: ```ts // Using CloudFormation resource type strings RemovalPolicies.of(scope).retain({ applyToResourceTypes: ['AWS::S3::Bucket', 'AWS::DynamoDB::Table'] }); const bucket = new s3.Bucket(scope, 'bucket') // Using CDK resource classes (type-safe) RemovalPolicies.of(scope).retain({ applyToResourceTypes: [ bucket.cfnResourceType, CfnTable.CFN_RESOURCE_TYPE_NAME, ] }); // Mixed usage RemovalPolicies.of(scope).retain({ applyToResourceTypes: [bucket.cfnResourceType, 'AWS::DynamoDB::Table'] }); ``` ## Description of how you validated changes TBD ## Checklist [x] My code adheres to the [CONTRIBUTING GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and [DESIGN GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md) --- By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license
1 parent 3ed7c4d commit 34c547c

14 files changed

+1665
-1
lines changed

packages/@aws-cdk-testing/framework-integ/test/core/test/integ.removal-policies.js.snapshot/RemovalPoliciesTestDefaultTestDeployAssertF0ACFC0A.assets.json

+19
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk-testing/framework-integ/test/core/test/integ.removal-policies.js.snapshot/RemovalPoliciesTestDefaultTestDeployAssertF0ACFC0A.template.json

+36
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk-testing/framework-integ/test/core/test/integ.removal-policies.js.snapshot/TestStack.assets.json

+19
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
{
2+
"Resources": {
3+
"TestBucket560B80BC": {
4+
"Type": "AWS::S3::Bucket",
5+
"UpdateReplacePolicy": "Retain",
6+
"DeletionPolicy": "Retain"
7+
},
8+
"TestTable5769773A": {
9+
"Type": "AWS::DynamoDB::Table",
10+
"Properties": {
11+
"AttributeDefinitions": [
12+
{
13+
"AttributeName": "id",
14+
"AttributeType": "S"
15+
}
16+
],
17+
"KeySchema": [
18+
{
19+
"AttributeName": "id",
20+
"KeyType": "HASH"
21+
}
22+
],
23+
"ProvisionedThroughput": {
24+
"ReadCapacityUnits": 5,
25+
"WriteCapacityUnits": 5
26+
}
27+
},
28+
"UpdateReplacePolicy": "Retain",
29+
"DeletionPolicy": "Retain"
30+
},
31+
"TestUser6A619381": {
32+
"Type": "AWS::IAM::User",
33+
"UpdateReplacePolicy": "Retain",
34+
"DeletionPolicy": "Retain"
35+
},
36+
"DestroyBucket924C7F03": {
37+
"Type": "AWS::S3::Bucket",
38+
"UpdateReplacePolicy": "Delete",
39+
"DeletionPolicy": "Delete"
40+
},
41+
"MissingPoliciesTestPreConfigured993B6B53": {
42+
"Type": "AWS::S3::Bucket",
43+
"UpdateReplacePolicy": "Retain",
44+
"DeletionPolicy": "Retain"
45+
},
46+
"MissingPoliciesTestNotConfiguredECEB0D31": {
47+
"Type": "AWS::S3::Bucket",
48+
"UpdateReplacePolicy": "Retain",
49+
"DeletionPolicy": "Retain"
50+
},
51+
"FilteredMissingPoliciesTestBucketToRetainB723E6AC": {
52+
"Type": "AWS::S3::Bucket",
53+
"UpdateReplacePolicy": "Retain",
54+
"DeletionPolicy": "Retain"
55+
},
56+
"FilteredMissingPoliciesTestTableToSkip835B5C39": {
57+
"Type": "AWS::DynamoDB::Table",
58+
"Properties": {
59+
"AttributeDefinitions": [
60+
{
61+
"AttributeName": "id",
62+
"AttributeType": "S"
63+
}
64+
],
65+
"KeySchema": [
66+
{
67+
"AttributeName": "id",
68+
"KeyType": "HASH"
69+
}
70+
],
71+
"ProvisionedThroughput": {
72+
"ReadCapacityUnits": 5,
73+
"WriteCapacityUnits": 5
74+
}
75+
},
76+
"UpdateReplacePolicy": "Retain",
77+
"DeletionPolicy": "Retain"
78+
}
79+
},
80+
"Parameters": {
81+
"BootstrapVersion": {
82+
"Type": "AWS::SSM::Parameter::Value<String>",
83+
"Default": "/cdk-bootstrap/hnb659fds/version",
84+
"Description": "Version of the CDK Bootstrap resources in this environment, automatically retrieved from SSM Parameter Store. [cdk:skip]"
85+
}
86+
},
87+
"Rules": {
88+
"CheckBootstrapVersion": {
89+
"Assertions": [
90+
{
91+
"Assert": {
92+
"Fn::Not": [
93+
{
94+
"Fn::Contains": [
95+
[
96+
"1",
97+
"2",
98+
"3",
99+
"4",
100+
"5"
101+
],
102+
{
103+
"Ref": "BootstrapVersion"
104+
}
105+
]
106+
}
107+
]
108+
},
109+
"AssertDescription": "CDK bootstrap stack version 6 required. Please run 'cdk bootstrap' with a recent version of the CDK CLI."
110+
}
111+
]
112+
}
113+
}
114+
}

packages/@aws-cdk-testing/framework-integ/test/core/test/integ.removal-policies.js.snapshot/cdk.out

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk-testing/framework-integ/test/core/test/integ.removal-policies.js.snapshot/integ.json

+12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)