Skip to content
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

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

Open
jadamariano opened this issue Feb 9, 2024 · 3 comments
Labels
@aws-cdk/aws-docdb Related to Amazon DocumentDB bug This issue is a bug. effort/medium Medium work item – several days of effort p3

Comments

@jadamariano
Copy link

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,
        }
    }
});
  1. Deploy the cluster
  2. Delete the CloudFormation stack
  3. The CloudFormation stack will fail to delete

Possible Solution

This same bug was happening for the aws-rds DatabaseCluster (issue attached here), until this fix 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

@jadamariano jadamariano added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Feb 9, 2024
@github-actions github-actions bot added the @aws-cdk/aws-docdb Related to Amazon DocumentDB label Feb 9, 2024
@pahud
Copy link
Contributor

pahud commented Feb 13, 2024

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

Looks like it could be a possible fix. Thank you for your PR.

@pahud pahud added p2 effort/medium Medium work item – several days of effort and removed needs-triage This issue or PR still needs to be triaged. labels Feb 13, 2024
@jadamariano
Copy link
Author

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

Looks like it could be a possible fix. Thank you for your PR.

@pahud Thank you! I'm working on getting all of the PR checks to pass but am having some trouble. Are you able to provide some guidance? Here are the issues I'm having:

  1. request-cli-integ-test / cli-changes (pull_request_target) is failing due to it not being able to find the changed files. I took a look at the Workflow file and am wondering if it is because I don't have any files changed under these directories: packages/aws-cdk/bin/**, packages/aws-cdk/lib/**, packages/aws-cdk/test/**.
  2. AWS CodeBuild us-east-1 is failing due to my newly added integration test failing. I think it is failing because there is currently no snapshot for the test in the main branch, and as the error message suggests, integ-runner --update-on-failed should be run. I'm not quite sure how to resolve this or if that is even the cause of the error.

@pahud
Copy link
Contributor

pahud commented Jun 3, 2024

@jadamariano please reach out to me on cdk.dev if you are still working on the PR. Thanks.

@pahud pahud added p3 and removed p2 labels Jun 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/aws-docdb Related to Amazon DocumentDB bug This issue is a bug. effort/medium Medium work item – several days of effort p3
Projects
None yet
2 participants