@@ -13,7 +13,8 @@ import { IParameterGroup } from './parameter-group';
13
13
import { applyRemovalPolicy , defaultDeletionProtection , setupS3ImportExport } from './private/util' ;
14
14
import { BackupProps , InstanceProps , Login , PerformanceInsightRetention , RotationMultiUserOptions } from './props' ;
15
15
import { DatabaseProxy , DatabaseProxyOptions , ProxyTarget } from './proxy' ;
16
- import { CfnDBCluster , CfnDBClusterProps , CfnDBInstance , CfnDBSubnetGroup } from './rds.generated' ;
16
+ import { CfnDBCluster , CfnDBClusterProps , CfnDBInstance } from './rds.generated' ;
17
+ import { ISubnetGroup , SubnetGroup } from './subnet-group' ;
17
18
18
19
/**
19
20
* Common properties for a new database cluster or cluster from snapshot.
@@ -213,6 +214,13 @@ interface DatabaseClusterBaseProps {
213
214
* @default - None
214
215
*/
215
216
readonly s3ExportBuckets ?: s3 . IBucket [ ] ;
217
+
218
+ /**
219
+ * Existing subnet group for the cluster.
220
+ *
221
+ * @default - a new subnet group will be created.
222
+ */
223
+ readonly subnetGroup ?: ISubnetGroup ;
216
224
}
217
225
218
226
/**
@@ -278,8 +286,8 @@ abstract class DatabaseClusterNew extends DatabaseClusterBase {
278
286
public readonly instanceEndpoints : Endpoint [ ] = [ ] ;
279
287
280
288
protected readonly newCfnProps : CfnDBClusterProps ;
281
- protected readonly subnetGroup : CfnDBSubnetGroup ;
282
289
protected readonly securityGroups : ec2 . ISecurityGroup [ ] ;
290
+ protected readonly subnetGroup : ISubnetGroup ;
283
291
284
292
constructor ( scope : Construct , id : string , props : DatabaseClusterBaseProps ) {
285
293
super ( scope , id ) ;
@@ -291,13 +299,12 @@ abstract class DatabaseClusterNew extends DatabaseClusterBase {
291
299
Annotations . of ( this ) . addError ( `Cluster requires at least 2 subnets, got ${ subnetIds . length } ` ) ;
292
300
}
293
301
294
- this . subnetGroup = new CfnDBSubnetGroup ( this , 'Subnets' , {
295
- dbSubnetGroupDescription : `Subnets for ${ id } database` ,
296
- subnetIds,
302
+ this . subnetGroup = props . subnetGroup ?? new SubnetGroup ( this , 'Subnets' , {
303
+ description : `Subnets for ${ id } database` ,
304
+ vpc : props . instanceProps . vpc ,
305
+ vpcSubnets : props . instanceProps . vpcSubnets ,
306
+ removalPolicy : props . removalPolicy === RemovalPolicy . RETAIN ? props . removalPolicy : undefined ,
297
307
} ) ;
298
- if ( props . removalPolicy === RemovalPolicy . RETAIN ) {
299
- this . subnetGroup . applyRemovalPolicy ( RemovalPolicy . RETAIN ) ;
300
- }
301
308
302
309
this . securityGroups = props . instanceProps . securityGroups ?? [
303
310
new ec2 . SecurityGroup ( this , 'SecurityGroup' , {
@@ -330,7 +337,7 @@ abstract class DatabaseClusterNew extends DatabaseClusterBase {
330
337
engine : props . engine . engineType ,
331
338
engineVersion : props . engine . engineVersion ?. fullVersion ,
332
339
dbClusterIdentifier : props . clusterIdentifier ,
333
- dbSubnetGroupName : this . subnetGroup . ref ,
340
+ dbSubnetGroupName : this . subnetGroup . subnetGroupName ,
334
341
vpcSecurityGroupIds : this . securityGroups . map ( sg => sg . securityGroupId ) ,
335
342
port : props . port ?? clusterEngineBindConfig . port ,
336
343
dbClusterParameterGroupName : clusterParameterGroupConfig ?. parameterGroupName ,
@@ -641,7 +648,7 @@ interface InstanceConfig {
641
648
* A function rather than a protected method on ``DatabaseClusterNew`` to avoid exposing
642
649
* ``DatabaseClusterNew`` and ``DatabaseClusterBaseProps`` in the API.
643
650
*/
644
- function createInstances ( cluster : DatabaseClusterNew , props : DatabaseClusterBaseProps , subnetGroup : CfnDBSubnetGroup ) : InstanceConfig {
651
+ function createInstances ( cluster : DatabaseClusterNew , props : DatabaseClusterBaseProps , subnetGroup : ISubnetGroup ) : InstanceConfig {
645
652
const instanceCount = props . instances != null ? props . instances : 2 ;
646
653
if ( instanceCount < 1 ) {
647
654
throw new Error ( 'At least one instance is required' ) ;
@@ -696,7 +703,7 @@ function createInstances(cluster: DatabaseClusterNew, props: DatabaseClusterBase
696
703
? ( instanceProps . performanceInsightRetention || PerformanceInsightRetention . DEFAULT )
697
704
: undefined ,
698
705
// This is already set on the Cluster. Unclear to me whether it should be repeated or not. Better yes.
699
- dbSubnetGroupName : subnetGroup . ref ,
706
+ dbSubnetGroupName : subnetGroup . subnetGroupName ,
700
707
dbParameterGroupName : instanceParameterGroupConfig ?. parameterGroupName ,
701
708
monitoringInterval : props . monitoringInterval && props . monitoringInterval . toSeconds ( ) ,
702
709
monitoringRoleArn : monitoringRole && monitoringRole . roleArn ,
0 commit comments