Skip to content

Commit 23c9afc

Browse files
otterleyrix0rrr
authored andcommitted
feat(autoscaling): Support AssociatePublicIpAddress (#1604)
Allow AssociatePublicIpAddress property to be specified in ASG Launch Configuration. Fixes #1603
1 parent 2af2426 commit 23c9afc

File tree

3 files changed

+86
-4
lines changed

3 files changed

+86
-4
lines changed

packages/@aws-cdk/assert/lib/assertions/have-resource.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ function makeSuperObjectPredicate(obj: any, allowValueExtension: boolean) {
103103
};
104104
}
105105

106-
interface InspectionFailure {
106+
export interface InspectionFailure {
107107
resource: any;
108108
failureReason: string;
109109
}

packages/@aws-cdk/aws-autoscaling/lib/auto-scaling-group.ts

+11-2
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,14 @@ export interface AutoScalingGroupProps {
148148
* @default 300 (5 minutes)
149149
*/
150150
cooldownSeconds?: number;
151+
152+
/**
153+
* Whether instances in the Auto Scaling Group should have public
154+
* IP addresses associated with them.
155+
*
156+
* @default Use subnet setting
157+
*/
158+
associatePublicIpAddress?: boolean;
151159
}
152160

153161
/**
@@ -231,7 +239,8 @@ export class AutoScalingGroup extends cdk.Construct implements IAutoScalingGroup
231239
instanceType: props.instanceType.toString(),
232240
securityGroups: securityGroupsToken,
233241
iamInstanceProfile: iamProfile.ref,
234-
userData: userDataToken
242+
userData: userDataToken,
243+
associatePublicIpAddress: props.associatePublicIpAddress,
235244
});
236245

237246
launchConfig.addDependency(this.role);
@@ -748,4 +757,4 @@ export interface MetricTargetTrackingProps extends BaseTargetTrackingProps {
748757
* Value to keep the metric around
749758
*/
750759
targetValue: number;
751-
}
760+
}

packages/@aws-cdk/aws-autoscaling/test/test.auto-scaling-group.ts

+74-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { expect, haveResource, haveResourceLike, ResourcePart } from '@aws-cdk/assert';
1+
import { expect, haveResource, haveResourceLike, InspectionFailure, ResourcePart } from '@aws-cdk/assert';
22
import ec2 = require('@aws-cdk/aws-ec2');
33
import iam = require('@aws-cdk/aws-iam');
44
import cdk = require('@aws-cdk/cdk');
@@ -404,6 +404,79 @@ export = {
404404
}));
405405
test.done();
406406
},
407+
'allows association of public IP address'(test: Test) {
408+
// GIVEN
409+
const stack = new cdk.Stack();
410+
const vpc = mockVpc(stack);
411+
412+
// WHEN
413+
new autoscaling.AutoScalingGroup(stack, 'MyStack', {
414+
instanceType: new ec2.InstanceTypePair(ec2.InstanceClass.M4, ec2.InstanceSize.Micro),
415+
machineImage: new ec2.AmazonLinuxImage(),
416+
vpc,
417+
minSize: 0,
418+
maxSize: 0,
419+
desiredCapacity: 0,
420+
associatePublicIpAddress: true,
421+
});
422+
423+
// THEN
424+
expect(stack).to(haveResource("AWS::AutoScaling::LaunchConfiguration", {
425+
AssociatePublicIpAddress: true,
426+
}
427+
));
428+
test.done();
429+
},
430+
'allows disassociation of public IP address'(test: Test) {
431+
// GIVEN
432+
const stack = new cdk.Stack();
433+
const vpc = mockVpc(stack);
434+
435+
// WHEN
436+
new autoscaling.AutoScalingGroup(stack, 'MyStack', {
437+
instanceType: new ec2.InstanceTypePair(ec2.InstanceClass.M4, ec2.InstanceSize.Micro),
438+
machineImage: new ec2.AmazonLinuxImage(),
439+
vpc,
440+
minSize: 0,
441+
maxSize: 0,
442+
desiredCapacity: 0,
443+
associatePublicIpAddress: false,
444+
});
445+
446+
// THEN
447+
expect(stack).to(haveResource("AWS::AutoScaling::LaunchConfiguration", {
448+
AssociatePublicIpAddress: false,
449+
}
450+
));
451+
test.done();
452+
},
453+
'does not specify public IP address association by default'(test: Test) {
454+
// GIVEN
455+
const stack = new cdk.Stack();
456+
const vpc = mockVpc(stack);
457+
458+
// WHEN
459+
new autoscaling.AutoScalingGroup(stack, 'MyStack', {
460+
instanceType: new ec2.InstanceTypePair(ec2.InstanceClass.M4, ec2.InstanceSize.Micro),
461+
machineImage: new ec2.AmazonLinuxImage(),
462+
vpc,
463+
minSize: 0,
464+
maxSize: 0,
465+
desiredCapacity: 0,
466+
});
467+
468+
// THEN
469+
expect(stack).to(haveResource("AWS::AutoScaling::LaunchConfiguration", (resource: any, errors: InspectionFailure) => {
470+
for (const key of Object.keys(resource)) {
471+
if (key === "AssociatePublicIpAddress") {
472+
errors.failureReason = "Has AssociatePublicIpAddress";
473+
return false;
474+
}
475+
}
476+
return true;
477+
}));
478+
test.done();
479+
},
407480
};
408481

409482
function mockVpc(stack: cdk.Stack) {

0 commit comments

Comments
 (0)