@@ -99,11 +99,14 @@ interface ServerlessClusterNewProps {
99
99
100
100
/**
101
101
* The VPC that this Aurora Serverless cluster has been created in.
102
+ *
103
+ * @default - the default VPC in the account and region will be used
102
104
*/
103
- readonly vpc : ec2 . IVpc ;
105
+ readonly vpc ? : ec2 . IVpc ;
104
106
105
107
/**
106
- * Where to place the instances within the VPC
108
+ * Where to place the instances within the VPC.
109
+ * If provided, the `vpc` property must also be specified.
107
110
*
108
111
* @default - the VPC default strategy if not specified.
109
112
*/
@@ -129,7 +132,8 @@ interface ServerlessClusterNewProps {
129
132
/**
130
133
* Security group.
131
134
*
132
- * @default - a new security group is created.
135
+ * @default - a new security group is created if `vpc` was provided.
136
+ * If the `vpc` property was not provided, no VPC security groups will be associated with the DB cluster.
133
137
*/
134
138
readonly securityGroups ?: ec2 . ISecurityGroup [ ] ;
135
139
@@ -143,7 +147,8 @@ interface ServerlessClusterNewProps {
143
147
/**
144
148
* Existing subnet group for the cluster.
145
149
*
146
- * @default - a new subnet group will be created.
150
+ * @default - a new subnet group is created if `vpc` was provided.
151
+ * If the `vpc` property was not provided, no subnet group will be associated with the DB cluster
147
152
*/
148
153
readonly subnetGroup ?: ISubnetGroup ;
149
154
}
@@ -351,19 +356,42 @@ abstract class ServerlessClusterNew extends ServerlessClusterBase {
351
356
constructor ( scope : Construct , id : string , props : ServerlessClusterNewProps ) {
352
357
super ( scope , id ) ;
353
358
354
- const { subnetIds } = props . vpc . selectSubnets ( props . vpcSubnets ) ;
355
-
356
- // Cannot test whether the subnets are in different AZs, but at least we can test the amount.
357
- if ( subnetIds . length < 2 ) {
358
- Annotations . of ( this ) . addError ( `Cluster requires at least 2 subnets, got ${ subnetIds . length } ` ) ;
359
+ if ( props . vpc === undefined ) {
360
+ if ( props . vpcSubnets !== undefined ) {
361
+ throw new Error ( 'A VPC is required to use vpcSubnets in ServerlessCluster. Please add a VPC or remove vpcSubnets' ) ;
362
+ }
363
+ if ( props . subnetGroup !== undefined ) {
364
+ throw new Error ( 'A VPC is required to use subnetGroup in ServerlessCluster. Please add a VPC or remove subnetGroup' ) ;
365
+ }
366
+ if ( props . securityGroups !== undefined ) {
367
+ throw new Error ( 'A VPC is required to use securityGroups in ServerlessCluster. Please add a VPC or remove securityGroups' ) ;
368
+ }
359
369
}
360
370
361
- const subnetGroup = props . subnetGroup ?? new SubnetGroup ( this , 'Subnets' , {
362
- description : `Subnets for ${ id } database` ,
363
- vpc : props . vpc ,
364
- vpcSubnets : props . vpcSubnets ,
365
- removalPolicy : props . removalPolicy === RemovalPolicy . RETAIN ? props . removalPolicy : undefined ,
366
- } ) ;
371
+ let subnetGroup : ISubnetGroup | undefined = props . subnetGroup ;
372
+ this . securityGroups = props . securityGroups ?? [ ] ;
373
+ if ( props . vpc !== undefined ) {
374
+ const { subnetIds } = props . vpc . selectSubnets ( props . vpcSubnets ) ;
375
+
376
+ // Cannot test whether the subnets are in different AZs, but at least we can test the amount.
377
+ if ( subnetIds . length < 2 ) {
378
+ Annotations . of ( this ) . addError ( `Cluster requires at least 2 subnets, got ${ subnetIds . length } ` ) ;
379
+ }
380
+
381
+ subnetGroup = props . subnetGroup ?? new SubnetGroup ( this , 'Subnets' , {
382
+ description : `Subnets for ${ id } database` ,
383
+ vpc : props . vpc ,
384
+ vpcSubnets : props . vpcSubnets ,
385
+ removalPolicy : props . removalPolicy === RemovalPolicy . RETAIN ? props . removalPolicy : undefined ,
386
+ } ) ;
387
+
388
+ this . securityGroups = props . securityGroups ?? [
389
+ new ec2 . SecurityGroup ( this , 'SecurityGroup' , {
390
+ description : 'RDS security group' ,
391
+ vpc : props . vpc ,
392
+ } ) ,
393
+ ] ;
394
+ }
367
395
368
396
if ( props . backupRetention ) {
369
397
const backupRetentionDays = props . backupRetention . toDays ( ) ;
@@ -379,12 +407,6 @@ abstract class ServerlessClusterNew extends ServerlessClusterBase {
379
407
const clusterParameterGroup = props . parameterGroup ?? clusterEngineBindConfig . parameterGroup ;
380
408
const clusterParameterGroupConfig = clusterParameterGroup ?. bindToCluster ( { } ) ;
381
409
382
- this . securityGroups = props . securityGroups ?? [
383
- new ec2 . SecurityGroup ( this , 'SecurityGroup' , {
384
- description : 'RDS security group' ,
385
- vpc : props . vpc ,
386
- } ) ,
387
- ] ;
388
410
389
411
const clusterIdentifier = FeatureFlags . of ( this ) . isEnabled ( cxapi . RDS_LOWERCASE_DB_IDENTIFIER )
390
412
? props . clusterIdentifier ?. toLowerCase ( )
@@ -395,7 +417,7 @@ abstract class ServerlessClusterNew extends ServerlessClusterBase {
395
417
databaseName : props . defaultDatabaseName ,
396
418
dbClusterIdentifier : clusterIdentifier ,
397
419
dbClusterParameterGroupName : clusterParameterGroupConfig ?. parameterGroupName ,
398
- dbSubnetGroupName : subnetGroup . subnetGroupName ,
420
+ dbSubnetGroupName : subnetGroup ? .subnetGroupName ,
399
421
deletionProtection : defaultDeletionProtection ( props . deletionProtection , props . removalPolicy ) ,
400
422
engine : props . engine . engineType ,
401
423
engineVersion : props . engine . engineVersion ?. fullVersion ,
@@ -476,7 +498,7 @@ export class ServerlessCluster extends ServerlessClusterNew {
476
498
477
499
public readonly secret ?: secretsmanager . ISecret ;
478
500
479
- private readonly vpc : ec2 . IVpc ;
501
+ private readonly vpc ? : ec2 . IVpc ;
480
502
private readonly vpcSubnets ?: ec2 . SubnetSelection ;
481
503
482
504
private readonly singleUserRotationApplication : secretsmanager . SecretRotationApplication ;
@@ -525,6 +547,10 @@ export class ServerlessCluster extends ServerlessClusterNew {
525
547
throw new Error ( 'Cannot add single user rotation for a cluster without secret.' ) ;
526
548
}
527
549
550
+ if ( this . vpc === undefined ) {
551
+ throw new Error ( 'Cannot add single user rotation for a cluster without VPC.' ) ;
552
+ }
553
+
528
554
const id = 'RotationSingleUser' ;
529
555
const existing = this . node . tryFindChild ( id ) ;
530
556
if ( existing ) {
@@ -549,6 +575,11 @@ export class ServerlessCluster extends ServerlessClusterNew {
549
575
if ( ! this . secret ) {
550
576
throw new Error ( 'Cannot add multi user rotation for a cluster without secret.' ) ;
551
577
}
578
+
579
+ if ( this . vpc === undefined ) {
580
+ throw new Error ( 'Cannot add multi user rotation for a cluster without VPC.' ) ;
581
+ }
582
+
552
583
return new secretsmanager . SecretRotation ( this , id , {
553
584
...options ,
554
585
excludeCharacters : options . excludeCharacters ?? DEFAULT_PASSWORD_EXCLUDE_CHARS ,
@@ -680,4 +711,4 @@ export class ServerlessClusterFromSnapshot extends ServerlessClusterNew {
680
711
this . secret = secret . attach ( this ) ;
681
712
}
682
713
}
683
- }
714
+ }
0 commit comments