Skip to content

fix(rds): propagate RemovalPolicy.RETAIN_ON_UPDATE_OR_DELETE to helper resources#37856

Open
jasdeepbhalla wants to merge 3 commits into
aws:mainfrom
jasdeepbhalla:rds/fix-retain-on-update-or-delete-propagation
Open

fix(rds): propagate RemovalPolicy.RETAIN_ON_UPDATE_OR_DELETE to helper resources#37856
jasdeepbhalla wants to merge 3 commits into
aws:mainfrom
jasdeepbhalla:rds/fix-retain-on-update-or-delete-propagation

Conversation

@jasdeepbhalla
Copy link
Copy Markdown
Contributor

Issue

Closes #37780.

Reason for this change

RemovalPolicy.RETAIN_ON_UPDATE_OR_DELETE was not propagated to the helper resources (subnet groups, cluster instances) created by DatabaseCluster, DatabaseInstance, and ServerlessCluster. Three places in the RDS private utilities compared against RemovalPolicy.RETAIN only — RETAIN_ON_UPDATE_OR_DELETE silently fell through to DESTROY, leaving those child resources with no explicit deletion policy at all (CloudFormation default: Delete).

Description of changes

packages/aws-cdk-lib/aws-rds/lib/private/util.ts

  • helperRemovalPolicy: extended the condition to pass RETAIN_ON_UPDATE_OR_DELETE through as-is instead of collapsing it to DESTROY. RetainExceptOnCreate is correct here: it still permits cleanup of the helper if its own creation fails during a rollback, while retaining it on normal updates and deletes.
  • defaultDeletionProtection: extended the condition to also auto-enable deletion protection when removalPolicy is RETAIN_ON_UPDATE_OR_DELETE, consistent with the existing RETAIN behaviour.

packages/aws-cdk-lib/aws-rds/lib/serverless-cluster.ts

  • Replaced the inline props.removalPolicy === RemovalPolicy.RETAIN ? props.removalPolicy : undefined check with renderUnless(helperRemovalPolicy(props.removalPolicy), RemovalPolicy.DESTROY), bringing it in line with DatabaseCluster and DatabaseInstance. Also added the missing helperRemovalPolicy and renderUnless imports.

Describe any new or updated permissions being added

None. This is a pure bug fix with no IAM or AWS service interaction.

Description of how you validated changes

Unit tests (packages/aws-cdk-lib/aws-rds/test/):

  • cluster.test.ts: added RETAIN_ON_UPDATE_OR_DELETE row to both existing test.each blocks that cover DeletionPolicy/UpdateReplacePolicy on the cluster, instance, and subnet group. Added a new test.each block verifying DeletionProtection is auto-enabled for both RETAIN and RETAIN_ON_UPDATE_OR_DELETE.
  • instance.test.ts: same additions — RETAIN_ON_UPDATE_OR_DELETE row in the existing policy propagation test, plus a new DeletionProtection auto-enable test.
  • serverless-cluster.test.ts: new test asserting the subnet group gets RetainExceptOnCreate when the cluster is created with RETAIN_ON_UPDATE_OR_DELETE.

Integration test (packages/@aws-cdk-testing/framework-integ/test/aws-rds/test/integ.cluster-instance-removal-policy.ts):

Creates a DatabaseCluster and a DatabaseInstance each with removalPolicy: RemovalPolicy.RETAIN_ON_UPDATE_OR_DELETE and verifies CDK synthesis succeeds. The snapshot needs to be generated by a maintainer using cdk-integ — I was unable to run a live deployment to produce it. Please see CONTRIBUTING.md for instructions.

Note: Because RETAIN_ON_UPDATE_OR_DELETE (CloudFormation RetainExceptOnCreate) retains resources on stack deletion, deployed resources from this integ test will need manual cleanup after the test run.

Checklist


By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license

Three places in the RDS private utilities compared removal policies against
RemovalPolicy.RETAIN only, silently dropping RETAIN_ON_UPDATE_OR_DELETE
through to DESTROY for subnet groups and cluster instances.

- helperRemovalPolicy: pass RETAIN_ON_UPDATE_OR_DELETE through rather than
  collapsing to DESTROY. RetainExceptOnCreate still allows cleanup if the
  helper own creation fails during a rollback.
- defaultDeletionProtection: extend the condition to also enable deletion
  protection when removalPolicy is RETAIN_ON_UPDATE_OR_DELETE, consistent
  with RETAIN behaviour.
- serverless-cluster.ts: replace the inline comparison with the shared
  helperRemovalPolicy + renderUnless helpers, consistent with DatabaseCluster
  and DatabaseInstance.

closes aws#37780
@github-actions github-actions Bot added valued-contributor [Pilot] contributed between 6-12 PRs to the CDK bug This issue is a bug. effort/medium Medium work item – several days of effort p2 labels May 13, 2026
Copy link
Copy Markdown
Collaborator

@aws-cdk-automation aws-cdk-automation left a 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 fails with the following errors:

❌ Fixes must contain a change to an integration test file and the resulting snapshot.

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.

…TE_OR_DELETE tests

CDK core sets DeletionPolicy=RetainExceptOnCreate but UpdateReplacePolicy=Retain
for RemovalPolicy.RETAIN_ON_UPDATE_OR_DELETE. The test assertions in cluster,
instance, and serverless-cluster tests incorrectly expected RetainExceptOnCreate
for both policies.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug This issue is a bug. effort/medium Medium work item – several days of effort p2 valued-contributor [Pilot] contributed between 6-12 PRs to the CDK

Projects

None yet

Development

Successfully merging this pull request may close these issues.

rds: RemovalPolicy.RETAIN_ON_UPDATE_OR_DELETE not propagated to child resources in RDS constructs

2 participants