File tree 4 files changed +31
-14
lines changed
4 files changed +31
-14
lines changed Original file line number Diff line number Diff line change @@ -292,6 +292,9 @@ export class AutoScalingGroup extends cdk.Construct implements IAutoScalingGroup
292
292
}
293
293
294
294
asgProps . vpcZoneIdentifier = props . vpc . subnetIds ( props . vpcSubnets ) ;
295
+ if ( ! props . vpc . isPublicSubnets ( asgProps . vpcZoneIdentifier ) && props . associatePublicIpAddress ) {
296
+ throw new Error ( "To set 'associatePublicIpAddress: true' you must select Public subnets (vpcSubnets: { subnetType: SubnetType.Public })" ) ;
297
+ }
295
298
296
299
this . autoScalingGroup = new CfnAutoScalingGroup ( this , 'ASG' , asgProps ) ;
297
300
this . osType = machineImage . os . type ;
Original file line number Diff line number Diff line change @@ -418,6 +418,8 @@ export = {
418
418
minCapacity : 0 ,
419
419
maxCapacity : 0 ,
420
420
desiredCapacity : 0 ,
421
+
422
+ vpcSubnets : { subnetType : ec2 . SubnetType . Public } ,
421
423
associatePublicIpAddress : true ,
422
424
} ) ;
423
425
@@ -428,6 +430,25 @@ export = {
428
430
) ) ;
429
431
test . done ( ) ;
430
432
} ,
433
+ 'association of public IP address requires public subnet' ( test : Test ) {
434
+ // GIVEN
435
+ const stack = new cdk . Stack ( ) ;
436
+ const vpc = mockVpc ( stack ) ;
437
+
438
+ // WHEN
439
+ test . throws ( ( ) => {
440
+ new autoscaling . AutoScalingGroup ( stack , 'MyStack' , {
441
+ instanceType : new ec2 . InstanceTypePair ( ec2 . InstanceClass . M4 , ec2 . InstanceSize . Micro ) ,
442
+ machineImage : new ec2 . AmazonLinuxImage ( ) ,
443
+ vpc,
444
+ minCapacity : 0 ,
445
+ maxCapacity : 0 ,
446
+ desiredCapacity : 0 ,
447
+ associatePublicIpAddress : true ,
448
+ } ) ;
449
+ } ) ;
450
+ test . done ( ) ;
451
+ } ,
431
452
'allows disassociation of public IP address' ( test : Test ) {
432
453
// GIVEN
433
454
const stack = new cdk . Stack ( ) ;
Original file line number Diff line number Diff line change @@ -77,13 +77,9 @@ export interface IVpcNetwork extends IConstruct {
77
77
subnetInternetDependencies ( selection ?: SubnetSelection ) : IDependable ;
78
78
79
79
/**
80
- * Return whether the given subnet is one of this VPC's public subnets.
81
- *
82
- * The subnet must literally be one of the subnet object obtained from
83
- * this VPC. A subnet that merely represents the same subnet will
84
- * never return true.
80
+ * Return whether all of the given subnets are from the VPC's public subnets.
85
81
*/
86
- isPublicSubnet ( subnet : IVpcSubnet ) : boolean ;
82
+ isPublicSubnets ( subnetIds : string [ ] ) : boolean ;
87
83
88
84
/**
89
85
* Adds a new VPN connection to this VPC
@@ -253,14 +249,11 @@ export abstract class VpcNetworkBase extends Construct implements IVpcNetwork {
253
249
public abstract export ( ) : VpcNetworkImportProps ;
254
250
255
251
/**
256
- * Return whether the given subnet is one of this VPC's public subnets.
257
- *
258
- * The subnet must literally be one of the subnet object obtained from
259
- * this VPC. A subnet that merely represents the same subnet will
260
- * never return true.
252
+ * Return whether all of the given subnets are from the VPC's public subnets.
261
253
*/
262
- public isPublicSubnet ( subnet : IVpcSubnet ) {
263
- return this . publicSubnets . indexOf ( subnet ) > - 1 ;
254
+ public isPublicSubnets ( subnetIds : string [ ] ) : boolean {
255
+ const pubIds = new Set ( this . publicSubnets . map ( n => n . subnetId ) ) ;
256
+ return subnetIds . every ( pubIds . has . bind ( pubIds ) ) ;
264
257
}
265
258
266
259
/**
Original file line number Diff line number Diff line change @@ -456,7 +456,7 @@ export class VpcNetwork extends VpcNetworkBase {
456
456
if ( placement ) {
457
457
const subnets = this . subnets ( placement ) ;
458
458
for ( const sub of subnets ) {
459
- if ( ! this . isPublicSubnet ( sub ) ) {
459
+ if ( this . publicSubnets . indexOf ( sub ) === - 1 ) {
460
460
throw new Error ( `natGatewayPlacement ${ placement } contains non public subnet ${ sub } ` ) ;
461
461
}
462
462
}
You can’t perform that action at this time.
0 commit comments