1
- import { Connections , IConnectable , ISecurityGroup , ISubnet , IVpc , Peer , Port , SecurityGroup } from '@aws-cdk/aws-ec2' ;
1
+ import { Connections , IConnectable , ISecurityGroup , IVpc , Peer , Port ,
2
+ SecurityGroup , SelectedSubnets , SubnetSelection , SubnetType } from '@aws-cdk/aws-ec2' ;
2
3
import { Construct , Duration , Lazy , Resource } from '@aws-cdk/core' ;
3
4
import { CfnLoadBalancer } from './elasticloadbalancing.generated' ;
4
5
@@ -57,6 +58,16 @@ export interface LoadBalancerProps {
57
58
* @default true
58
59
*/
59
60
readonly crossZone ?: boolean ;
61
+
62
+ /**
63
+ * Which subnets to deploy the load balancer
64
+ *
65
+ * Can be used to define a specific set of subnets to deploy the load balancer to.
66
+ * Useful multiple public or private subnets are covering the same availability zone.
67
+ *
68
+ * @default - Public subnets if internetFacing, Private subnets otherwise
69
+ */
70
+ readonly subnetSelection ?: SubnetSelection ;
60
71
}
61
72
62
73
/**
@@ -226,18 +237,18 @@ export class LoadBalancer extends Resource implements IConnectable {
226
237
this . connections = new Connections ( { securityGroups : [ this . securityGroup ] } ) ;
227
238
228
239
// Depending on whether the ELB has public or internal IPs, pick the right backend subnets
229
- const subnets : ISubnet [ ] = props . internetFacing ? props . vpc . publicSubnets : props . vpc . privateSubnets ;
240
+ const selectedSubnets : SelectedSubnets = loadBalancerSubnets ( props ) ;
230
241
231
242
this . elb = new CfnLoadBalancer ( this , 'Resource' , {
232
243
securityGroups : [ this . securityGroup . securityGroupId ] ,
233
- subnets : subnets . map ( s => s . subnetId ) ,
244
+ subnets : selectedSubnets . subnetIds ,
234
245
listeners : Lazy . anyValue ( { produce : ( ) => this . listeners } ) ,
235
246
scheme : props . internetFacing ? 'internet-facing' : 'internal' ,
236
247
healthCheck : props . healthCheck && healthCheckToJSON ( props . healthCheck ) ,
237
248
crossZone : ( props . crossZone === undefined || props . crossZone ) ? true : false
238
249
} ) ;
239
250
if ( props . internetFacing ) {
240
- this . elb . node . addDependency ( ... subnets . map ( s => s . internetConnectivityEstablished ) ) ;
251
+ this . elb . node . addDependency ( selectedSubnets . internetConnectivityEstablished ) ;
241
252
}
242
253
243
254
ifUndefined ( props . listeners , [ ] ) . forEach ( b => this . addListener ( b ) ) ;
@@ -426,3 +437,17 @@ function healthCheckToJSON(healthCheck: HealthCheck): CfnLoadBalancer.HealthChec
426
437
unhealthyThreshold : ifUndefined ( healthCheck . unhealthyThreshold , 5 ) . toString ( ) ,
427
438
} ;
428
439
}
440
+
441
+ function loadBalancerSubnets ( props : LoadBalancerProps ) : SelectedSubnets {
442
+ if ( props . subnetSelection !== undefined ) {
443
+ return props . vpc . selectSubnets ( props . subnetSelection ) ;
444
+ } else if ( props . internetFacing ) {
445
+ return props . vpc . selectSubnets ( {
446
+ subnetType : SubnetType . PUBLIC
447
+ } ) ;
448
+ } else {
449
+ return props . vpc . selectSubnets ( {
450
+ subnetType : SubnetType . PRIVATE
451
+ } ) ;
452
+ }
453
+ }
0 commit comments