Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

fix(docdb): cannot delete a stack with DatabaseCluster removal_policy set to 'Retain' #29059

Closed
3 changes: 3 additions & 0 deletions packages/aws-cdk-lib/aws-docdb/lib/cluster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,9 @@ export class DatabaseCluster extends DatabaseClusterBase {
dbSubnetGroupDescription: `Subnets for ${id} database`,
subnetIds,
});
if (props.removalPolicy === RemovalPolicy.RETAIN) {
subnetGroup.applyRemovalPolicy(RemovalPolicy.RETAIN);
}

// Create the security group for the DB cluster
let securityGroup: ec2.ISecurityGroup;
Expand Down
20 changes: 20 additions & 0 deletions packages/aws-cdk-lib/aws-docdb/test/cluster.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,26 @@ describe('DatabaseCluster', () => {
});
});

test('cluster with retain policy applies retain policy to DBSubnetGroup', () => {
// GIVEN
const stack = testStack();
const vpc = new ec2.Vpc(stack, 'VPC');

// WHEN
new DatabaseCluster(stack, 'Database', {
masterUser: { username: 'admin' },
instanceType: ec2.InstanceType.of(ec2.InstanceClass.BURSTABLE2, ec2.InstanceSize.SMALL),
vpc,
removalPolicy: cdk.RemovalPolicy.RETAIN,
});

// THEN
Template.fromStack(stack).hasResourceProperties('AWS::DocDB::DBSubnetGroup', {
DeletionPolicy: 'Retain',
UpdateReplacePolicy: 'Retain',
});
});

test('creates a secret when master credentials are not specified', () => {
// GIVEN
const stack = testStack();
Expand Down
Loading