aws / aws/aws-cdk

(aws-docdb): cannot delete a stack with DatabaseCluster removal_policy set to 'Retain'

Open
#29,054 3 comments 1 reaction 0 assignees View on GitHub
@aws-cdk/aws-docdb bug effort/medium p3
Dominant language
TypeScript
Stars
12.9k
Forks
4.6k
Avg merge
2d 3h
Merged PRs (30d)
83

Description

### Describe the bug

After creating a DocumentDB DatabaseCluster with a removal_policy set to Retain, the CloudFormation stack can not be deleted. The stack can not be deleted because the associated subnet group can not be removed if it points to an existing cluster.

Sample error from CloudFormation:
`Got InvalidDBInstanceStateException with error: Cannot delete the subnet group '{subnet_group_name}' because at least one database instance: {instance_name} is still using it.`

### Expected Behavior

The CloudFormation stack should be able to successfully delete.

### Current Behavior

After creating a DocumentDB DatabaseCluster with a removal_policy set to Retain, the CloudFormation stack can not be deleted. The stack can not be deleted because the associated subnet group can not be removed if it points to an existing cluster.

Sample error from CloudFormation:
`Got InvalidDBInstanceStateException with error: Cannot delete the subnet group '{subnet_group_name}' because at least one database instance: {instance_name} is still using it.`

### Reproduction Steps

1. Create a database cluster using a default configuration (default configuration has removal_policy set to Retain)
```
const database = new rds.DatabaseCluster(this, 'DatabaseCluster', {
defaultDatabaseName: 'test',
masterUser: {
username: 'test',
},
instances: 1,
engine: rds.DatabaseClusterEngine.AURORA,
instanceProps: {
instanceType: ec2.InstanceType.of(ec2.InstanceClass.T3, ec2.InstanceSize.SMALL),
vpc: vpc,
vpcSubnets: {
subnetType: ec2.SubnetType.PRIVATE,
}
}
});
```
2. Deploy the cluster
3. Delete the CloudFormation stack
4. The CloudFormation stack will fail to delete

### Possible Solution

This same bug was happening for the aws-rds DatabaseCluster ([issue attached here](https://github.com/aws/aws-cdk/issues/5282)), until this [fix](https://github.com/skinny85/aws-cdk/commit/3507155a62e04743d29adf602eb14fa143534730) came around. I suggest to make the same fix for the aws-docdb DatabaseCluster, where it sets the retention policy of DbSubnetGroup to 'Retain' if it is 'Retain' on the DatabaseCluster.

I plan on creating a PR using this solution to resolve this issue.

```
if (props.removalPolicy === RemovalPolicy.RETAIN) {
subnetGroup.applyRemovalPolicy(RemovalPolicy.RETAIN);
}
```

### Additional Information/Context

_No response_

### CDK CLI Version

2.126.0

### Framework Version

_No response_

### Node.js Version

v20.11.0

### OS

macOS Sonoma 14.3

### Language

Python

### Language Version

_No response_

### Other information

_No response_

Contributor guide

Open the contributing guide

Research direction

Start at the aws-docdb DatabaseCluster construct and inspect the analogous aws-rds fix linked in the issue. Check how the subnet group removal policy is handled when the cluster uses Retain, then verify that deploying and deleting the reproduced stack succeeds without the subnet-group deletion error.

Written by the indexing model from the issue text.

Assessment

Tech stack
aws, typescript
Domain
cloud, infrastructure
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.