-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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-rds: export value from network-stack cannot be deleted as it is in use by database-stack #26135
Comments
Looks like your database stack is importing an export value from network stack and your network stack is trying to update that export value, which is prohibited in cloudformation. A common fix is to create an intrinsic function in your consuming stack(i.e. database-stack in your case) and point the import value to a static value, which means the exported value will not be consumed, and you should be able to update your network stack export value. Check out the discussion in this repost: |
thanks @pahud , that was very helpful... now i understand the issue better.
|
@mdesousa As I can't see your source code I am not sure the details. But basically you probably should just export Vpc from your network stack. I will investigate this and provide a suggested sample for you shortly. |
Alright. Let's consider this example below: export class NetworkStack extends cdk.Stack {
readonly vpc: ec2.IVpc;
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props)
this.vpc = new ec2.Vpc(this, 'Vpc', { maxAzs: 3, natGateways: 1})
}
}
export interface DatabaseStackProps extends cdk.StackProps {
readonly vpc: ec2.IVpc;
}
export class DatabaseStack extends cdk.Stack {
constructor(scope: Construct, id: string, props: DatabaseStackProps) {
super(scope, id, props)
// create the cluster
new rds.DatabaseCluster(this, 'Database', {
engine: rds.DatabaseClusterEngine.auroraMysql({ version: rds.AuroraMysqlEngineVersion.VER_3_03_0 }),
writer: rds.ClusterInstance.provisioned('writer'),
vpcSubnets: { subnetType: ec2.SubnetType.PRIVATE_WITH_EGRESS },
vpc: props.vpc,
});
}
} And const app = new cdk.App();
const env = { region: process.env.CDK_DEFAULT_REGION, account: process.env.CDK_DEFAULT_ACCOUNT };
const networkStack = new NetworkStack(app, 'Network', { env });
new DatabaseStack(app, 'DatabaseStack', {
env,
vpc: networkStack.vpc,
}); When we first deploy the two stacks, the database stack will have some imports from the network stack:
And if we change the vpcSubnets of DatabaseCuster using new rds.DatabaseCluster(this, 'Database', {
engine: rds.DatabaseClusterEngine.auroraMysql({ version: rds.AuroraMysqlEngineVersion.VER_3_03_0 }),
writer: rds.ClusterInstance.provisioned('writer'),
vpcSubnets: { subnetType: ec2.SubnetType.PUBLIC },
vpc: props.vpc,
}); Let's synth again
As you notice, the first 3 imports have changed from private to public and the network stack Export will change from
to
And if you run
It will remove 3 exports which are in used. This not allowed in cloudformation because the private subnet exports are in used by the consumer stack and can't be modified or deleted. Now, this is our hack. Let's use export class NetworkStack extends cdk.Stack {
readonly vpc: ec2.IVpc;
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props)
this.vpc = new ec2.Vpc(this, 'Vpc', { maxAzs: 3, natGateways: 1})
this.vpc.selectSubnets({ subnetType: ec2.SubnetType.PRIVATE_WITH_EGRESS}).subnetIds.map(
x => this.exportValue(x)
)
}
} Now, run
YES! No difference. Let's check out all the exports:
We will export 7 values(3 public, 3 private(dummy) and 1 vpc) OK let's Now, let's
This is because we can't in-place replace the SubnetGroup to remove subnets which are in-used by the DB. We need to create a separate subnetGroup like this and specify it in the DatabaseCluster property. export class DatabaseStack extends cdk.Stack {
constructor(scope: Construct, id: string, props: DatabaseStackProps) {
super(scope, id, props)
// create a new subnetGroup
const subnetGroup = new rds.SubnetGroup(this, 'SubnetGroup', {
vpc: props.vpc,
vpcSubnets: { subnetType: ec2.SubnetType.PUBLIC },
description: 'public subnet group',
})
// create the cluster
new rds.DatabaseCluster(this, 'Database', {
engine: rds.DatabaseClusterEngine.auroraMysql({ version: rds.AuroraMysqlEngineVersion.VER_3_03_0 }),
writer: rds.ClusterInstance.provisioned('writer'),
// vpcSubnets: { subnetType: ec2.SubnetType.PUBLIC },
subnetGroup,
vpc: props.vpc,
});
}
} Now it deploys and our database cluster should be running in public subnets now. Last but not least, we should remove the This is a little bit complicated but I hope my sample helps you understand how to deal with this problem in CDK. |
thanks @pahud ! this was a very thorough and clear explanation. |
quick update: it appears that changing the vpcSubnets congifuration for the subnet group result in the "Some of the subnets to be deleted are currently in use" error. if i create a new subnet group with a different id the database gets dropped and recreated. |
Describe the bug
i have created a network stack with 3 subnets: one public, one private with egress, and one isolated.
in a separate database stack i'm creating a database cluster (Aurora Serverless V2 Postgres) and initially used the public subnet by setting
vpcSubnets: { subnetType: SubnetType.PUBLIC }
.Now I would like to move the database to the isolated subnet by setting
vpcSubnets: { subnetType: SubnetType.PRIVATE_ISOLATED }
, however this results in an error: ExportsOutputRefnetworkstackvpcingressSubnet3SubnetCD2EA007C4DD55EA cannot be deleted as it is in use by database-stack.I have tried doing a deployment that adds the new isolated subnets without removing the public ones, and that succeeded. But when I try to remove the public subnets I face the same error.
Expected Behavior
the cdk should be able to reassign the subnets without errors
Current Behavior
deployment fails with an error: ExportsOutputRefnetworkstackvpcingressSubnet3SubnetCD2EA007C4DD55EA cannot be deleted as it is in use by database-stack.
Reproduction Steps
create a vpc with public and isolated subnets.
create a database cluster in a vpc with
vpcSubnets: { subnetType: SubnetType.PUBLIC }
.then deploy again with
vpcSubnets: { subnetType: SubnetType.PRIVATE_ISOLATED }
Possible Solution
No response
Additional Information/Context
No response
CDK CLI Version
2.85.0
Framework Version
No response
Node.js Version
18.15.0
OS
macOS
Language
Typescript
Language Version
TypeScript (5.1.3)
Other information
No response
The text was updated successfully, but these errors were encountered: