-
Notifications
You must be signed in to change notification settings - Fork 4k
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): RemovalPolicies.of(scope)
#32283
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The pull request linter has failed. See the aws-cdk-automation comment below for failure reasons. If you believe this pull request should receive an exemption, please comment and provide a justification.
A comment requesting an exemption should contain the text Exemption Request
. Additionally, if clarification is needed add Clarification Request
to a comment.
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #32283 +/- ##
==========================================
+ Coverage 77.46% 79.47% +2.00%
==========================================
Files 105 108 +3
Lines 7168 7158 -10
Branches 1314 1320 +6
==========================================
+ Hits 5553 5689 +136
+ Misses 1433 1285 -148
- Partials 182 184 +2
Flags with carried forward coverage won't be shown. Click here to find out more.
|
RemovalPolicys.of(scope)
This PR has been in the CHANGES REQUESTED state for 3 weeks, and looks abandoned. To keep this PR from being closed, please continue work on it. If not, it will automatically be closed in a week. |
✅ Updated pull request passes all PRLinter validations. Dismissing previous PRLinter review.
* or a class that extends CfnResource (e.g., TestBucketResource). | ||
* @default - apply to all resources | ||
*/ | ||
readonly applyToResourceTypes?: (string | typeof CfnResource)[]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This feature is very helpful for me (or us)!
But I'm not sure if we should use an Enum-Like (Union-Like) class instead of an union type in order to generate a public interface for other languages using a jsii transformation, what do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After much deliberation, I decided on string[]
. because I thought it would work well enough.
RemovalPolicies.of(parent).retain({
applyToResourceTypes: [
'AWS::DynamoDB::Table',
bucket.cfnResourceType, // 'AWS::S3::Bucket'
],
});
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm OK! Could you please change the description (Type-safe resource type ...
) and examples in this PR?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We might want to fix that example as cfnResourceType
appears to be a static method. (I often read the PR description to understand its feature, so correcting it would be more useful)
RemovalPolicies.of(scope).retain({
applyToResourceTypes: [
CfnBucket.cfnResourceType,
CfnTable.cfnResourceType,
]
});
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have made the correct Usage.
try { | ||
cfnResource.applyRemovalPolicy(this.policy); | ||
} catch (error) { | ||
// Check if the error is an instance of the built-in Error class | ||
if (error instanceof Error) { | ||
throw new Error(`Failed to apply removal policy to resource type ${resourceType}: ${error.message}`); | ||
} else { | ||
// If it's not an Error instance, convert it to a string for the message | ||
throw new Error(`Failed to apply removal policy to resource type ${resourceType}: ${String(error)}`); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this try/catch necessary? Are there any (bad) effects of not having it?
(IMO, I don't think we need this one.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought about it, and it might not be necessary, so I removed it.
/** | ||
* Properties for applying a removal policy | ||
*/ | ||
export interface RemovalPolicyProps { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be good to add a property priority
to RemovalPolicyProps
? (ref: https://github.com/aws/aws-cdk/blob/v2.172.0/packages/aws-cdk-lib/core/lib/aspect.ts#L45)
(* The priority
was introduced in v2.172.0: https://dev.to/aws-heroes/aws-cdk-aspects-specifications-have-changed-3i75.)
And the apply
method will be changed like:
public apply(policy: RemovalPolicy, props: RemovalPolicyProps = {}) {
Aspects.of(this.scope).add(new RemovalPolicyAspect(policy, props), {
priority: props.priority ?? AspectPriority.MUTATING,
});
}
Since the RemovalPolicy
change is a kind of resource update, I think it is good that the default value should be MUTATING
. (It is the same with the add
and remove
method of the Tags
class.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've only seen halfway through, but I'll leave you with my comments so far.
#### `RemovalPolicyProps` Interface | ||
|
||
Additional configuration options for applying removal policies. | ||
|
||
- **`applyToResourceTypes?`**: _(optional)_ | ||
Array of CloudFormation resource types (e.g., `'AWS::S3::Bucket'`) to which the removal policy should be applied. | ||
Defaults to applying to all resources. | ||
|
||
- **`excludeResourceTypes?`**: _(optional)_ | ||
Array of CloudFormation resource types to exclude from applying the removal policy. | ||
Defaults to no exclusions. | ||
|
||
- **`overwrite?`**: _(optional)_ | ||
If `true`, the removal policy will overwrite any existing policy already set on the resource. Defaults to `false`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wouldn't it be difficult to maintain if all properties were listed on the README? (Because all of them are also in JSDoc...)
RemovalPolicys.of(scope)
RemovalPolicies.of(scope)
new s3.Bucket(stack, 'TestBucket'); | ||
new dynamodb.Table(stack, 'TestTable', { | ||
partitionKey: { name: 'id', type: dynamodb.AttributeType.STRING }, | ||
}); | ||
new iam.User(stack, 'TestUser'); | ||
|
||
// Apply different removal policies to demonstrate functionality | ||
RemovalPolicies.of(stack).destroy(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about creating a custom construct that attaches a retain method? The following situation can be realized:
- Stack (with a destroy method)
- Resource A: DESTROY
- Resource B: DESTROY
- RetainConstruct (with a retain method)
- X: RETAIN
AWS CodeBuild CI Report
Powered by github-codebuild-logs, available on the AWS Serverless Application Repository |
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:
Description of how you validated changes
TBD
Checklist
[x] My code adheres to the CONTRIBUTING GUIDE and DESIGN GUIDELINES
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license