-
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 80.64% 80.64%
=======================================
Files 107 107
Lines 6996 6996
Branches 1290 1290
=======================================
Hits 5642 5642
Misses 1175 1175
Partials 179 179
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.
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.
thanks.
dec21e2
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.
Thanks for the change.
If multiple aspects apply conflicting settings, the one with the higher priority wins.
I found the words "the higher priority" and "wins" a little confusing.
The higher priority (lower numeric value) is applied first, and the lower priority (higher numeric value) is applied later. (Complicated explanation...)
In other words, the removal policy will be the value by the former if overwrite
is false, and the value by the latter if true, right?
Can we explain that well?
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.
packages/aws-cdk-lib/core/README.md
Outdated
#### `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...)
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.
clean up this doc.
da6b4c5
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
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 looked at the integ code now; 467ebbd
Bucket and Table default to RETAIN, so with the current stack, all resources are RETAIN. So it would be nice to have a resource that would be DESTROY.
Sorry for repeating.
367ca39
to
da6b4c5
Compare
@@ -220,4 +220,18 @@ describe('removal-policys', () => { | |||
expect(bucket.cfnOptions.deletionPolicy).toBe('Retain'); | |||
expect(table.cfnOptions.deletionPolicy).toBe('RetainExceptOnCreate'); | |||
}); | |||
|
|||
test('higher priority removal policy overrides lower priority removal policy', () => { |
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 new test using priority
and overwrite
with true.
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