Skip to content

Commit 80b50cf

Browse files
ysunioSunil Yadav
andauthored
fix(aurora): replace deprecated instanceProps and resolve critical issues (#1208)
* fix: resolve critical Aurora CDK issues - Add validation for required vpcId and subnetIds props - Set default username to 'clusteradmin' when not provided - Update to latest Aurora engine versions (PostgreSQL 15.4, MySQL 3.04.0) - Fix typo in secret description (Crendetials -> Credentials) - Use validated instanceType variable in cluster creation - Add app.synth() call to generate CloudFormation templates - Update example with realistic placeholder values * fix: replace deprecated instanceProps with writer/readers - Use new writer and readers properties instead of deprecated instanceProps - Create writer instance and reader instances based on replicaInstances count - Move vpc, vpcSubnets, and securityGroups to cluster level - Replace instanceIdentifierBase with clusterIdentifier * fix: update subnet_id to avoid construct conflict --------- Co-authored-by: Sunil Yadav <ysunio@amazon.com>
1 parent 5143d2f commit 80b50cf

File tree

1 file changed

+28
-16
lines changed

1 file changed

+28
-16
lines changed

typescript/rds/aurora/aurora.ts

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -137,12 +137,18 @@ export class Aurora extends Stack {
137137
//export class Aurora extends Construct {
138138
constructor(scope: Construct, id: string, props:AuroraProps) {
139139
//constructor(scope: Construct, id: string, props?: cdk.StackProps) {
140-
super(scope, id);
140+
super(scope, id, props);
141+
142+
// Validate required props
143+
if (!props.vpcId || !props.subnetIds?.length) {
144+
throw new Error('vpcId and subnetIds are required');
145+
}
141146

142147
let subnetIds = props.subnetIds;
143148
let instanceType = props.instanceType;
144149
let replicaInstances = props.replicaInstances ?? 1;
145150
let backupRetentionDays = props.backupRetentionDays ?? 14;
151+
let auroraClusterUsername = props.auroraClusterUsername ?? 'clusteradmin';
146152

147153
let ingressSources = [];
148154
if (typeof props.ingressSources !== 'undefined') {
@@ -223,12 +229,12 @@ export class Aurora extends Stack {
223229

224230
// Declaring postgres engine
225231
let auroraEngine = rds.DatabaseClusterEngine.auroraPostgres({
226-
version: rds.AuroraPostgresEngineVersion.VER_13_4,
232+
version: rds.AuroraPostgresEngineVersion.VER_15_4,
227233
});
228234

229235
if (props.engine == 'mysql') {
230236
auroraEngine = rds.DatabaseClusterEngine.auroraMysql({
231-
version: rds.AuroraMysqlEngineVersion.VER_2_10_1,
237+
version: rds.AuroraMysqlEngineVersion.VER_3_04_0,
232238
});
233239
}
234240

@@ -254,20 +260,20 @@ export class Aurora extends Stack {
254260
'AuroraClusterCredentials',
255261
{
256262
secretName: props.dbName + 'AuroraClusterCredentials',
257-
description: props.dbName + 'AuroraClusterCrendetials',
263+
description: props.dbName + 'AuroraClusterCredentials',
258264
generateSecretString: {
259265
excludeCharacters: "\"@/\\ '",
260266
generateStringKey: 'password',
261267
passwordLength: 30,
262-
secretStringTemplate: JSON.stringify({username: props.auroraClusterUsername}),
268+
secretStringTemplate: JSON.stringify({username: auroraClusterUsername}),
263269
},
264270
},
265271
);
266272

267273
// aurora credentials
268274
const auroraClusterCrendentials= rds.Credentials.fromSecret(
269275
auroraClusterSecret,
270-
props.auroraClusterUsername,
276+
auroraClusterUsername,
271277
);
272278

273279
if (instanceType == null || instanceType == undefined) {
@@ -296,7 +302,17 @@ export class Aurora extends Stack {
296302
retention: Duration.days(backupRetentionDays),
297303
},
298304
parameterGroup: auroraParameterGroup,
299-
instances: replicaInstances,
305+
writer: rds.ClusterInstance.provisioned('writer', {
306+
instanceType: instanceType,
307+
}),
308+
readers: Array.from({ length: replicaInstances - 1 }, (_, i) =>
309+
rds.ClusterInstance.provisioned(`reader${i + 1}`, {
310+
instanceType: instanceType,
311+
})
312+
),
313+
vpc: vpc,
314+
vpcSubnets: vpcSubnets,
315+
securityGroups: [dbsg],
300316
iamAuthentication: true,
301317
storageEncrypted: true,
302318
storageEncryptionKey: kmsKey,
@@ -306,13 +322,7 @@ export class Aurora extends Stack {
306322
cloudwatchLogsExports: cloudwatchLogsExports,
307323
cloudwatchLogsRetention: logs.RetentionDays.ONE_MONTH,
308324
preferredMaintenanceWindow: props.preferredMaintenanceWindow,
309-
instanceIdentifierBase: props.dbName,
310-
instanceProps: {
311-
instanceType: props.instanceType,
312-
vpcSubnets: vpcSubnets,
313-
vpc: vpc,
314-
securityGroups: [dbsg],
315-
},
325+
clusterIdentifier: props.dbName,
316326
});
317327

318328
aurora_cluster.applyRemovalPolicy(RemovalPolicy.RETAIN);
@@ -515,11 +525,13 @@ const app = new App();
515525

516526
new Aurora(app, 'AuroraStack', {
517527
env:{region:"us-east-2"}, description:"Aurora Stack",
518-
vpcId:"vpc-xxx",
519-
subnetIds:["subnet-xxx", "subnet-xxxxSS"],
528+
vpcId:"vpc-xxxxxxxxxx",
529+
subnetIds:["subnet-xxxxxx", "subnet-yyyyyyyy"],
520530
dbName:"sampledb",
521531
engine:"postgresql"
522532
});
523533

534+
app.synth();
535+
524536

525537

0 commit comments

Comments
 (0)