-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Description
Describe the bug
When creating a fresh cluster with readers and writer, one of the reader instances is effectively created before the writer and becomes the actual writer. So the opposite of what's written in the CDK code happens. This is a bug and it's reproducible.
Expected Behavior
The expected behavior is that the writer specified in CDK is indeed the writer when the cluster is created.
Current Behavior
The current behavior is that the writer becomes the reader, and viceversa, on a fresh cluster creation, which is the opposite of what's specified in CDK.
The CDK puts the writer instance first in CloudFormation, but that's not enough to guarantee that the first instance being created in the cluster because CloudFormation creates resources in parallel.
Reproduction Steps
Code:
import { App, RemovalPolicy, Stack, StackProps } from 'aws-cdk-lib';
import { InstanceClass, InstanceSize, InstanceType, SubnetType, Vpc } from 'aws-cdk-lib/aws-ec2';
import {
AuroraMysqlEngineVersion,
ClusterInstance,
Credentials,
DatabaseCluster,
DatabaseClusterEngine,
} from 'aws-cdk-lib/aws-rds';
export class RDSReproduceStack extends Stack {
constructor(parent: App, id: string, props: StackProps, vpc: Vpc) {
super(parent, id, props);
const cluster = new DatabaseCluster(this, 'testcluster', {
clusterIdentifier: 'testcluster',
engine: DatabaseClusterEngine.auroraMysql({ version: AuroraMysqlEngineVersion.VER_3_04_1 }),
credentials: Credentials.fromGeneratedSecret('myuser', {}),
writer: ClusterInstance.provisioned('writer', {
instanceIdentifier: 'writer',
instanceType: InstanceType.of(InstanceClass.BURSTABLE4_GRAVITON, InstanceSize.MEDIUM),
publiclyAccessible: false,
}),
serverlessV2MinCapacity: 0.5,
serverlessV2MaxCapacity: 1.0,
readers: [
ClusterInstance.serverlessV2('reader', {
scaleWithWriter: true,
publiclyAccessible: false,
}),
],
vpc: vpc,
vpcSubnets: {
subnetType: SubnetType.PRIVATE_WITH_EGRESS,
},
removalPolicy: RemovalPolicy.DESTROY,
});
}
}
Result, see in the image that the writer is the reader and viceversa, on a freshly created cluster:
Possible Solution
The possible solution is to add a dependency on the writer instance to all the reader instances here: https://github.com/aws/aws-cdk/blob/main/packages/aws-cdk-lib/aws-rds/lib/cluster.ts , to guarantee that the writer instance is the first instance being created in the cluster and thus become the writer.
Additional Information/Context
No response
CDK CLI Version
2.130.0
Framework Version
No response
Node.js Version
18
OS
Ubuntu
Language
TypeScript
Language Version
No response
Other information
No response