Skip to content

Commit

Permalink
feat(autoscaling): Support AssociatePublicIpAddress (#1604)
Browse files Browse the repository at this point in the history
Allow AssociatePublicIpAddress property to be specified in ASG Launch
Configuration.

Fixes #1603
  • Loading branch information
otterley authored and rix0rrr committed Jan 24, 2019
1 parent 2af2426 commit 23c9afc
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 4 deletions.
2 changes: 1 addition & 1 deletion packages/@aws-cdk/assert/lib/assertions/have-resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ function makeSuperObjectPredicate(obj: any, allowValueExtension: boolean) {
};
}

interface InspectionFailure {
export interface InspectionFailure {
resource: any;
failureReason: string;
}
Expand Down
13 changes: 11 additions & 2 deletions packages/@aws-cdk/aws-autoscaling/lib/auto-scaling-group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,14 @@ export interface AutoScalingGroupProps {
* @default 300 (5 minutes)
*/
cooldownSeconds?: number;

/**
* Whether instances in the Auto Scaling Group should have public
* IP addresses associated with them.
*
* @default Use subnet setting
*/
associatePublicIpAddress?: boolean;
}

/**
Expand Down Expand Up @@ -231,7 +239,8 @@ export class AutoScalingGroup extends cdk.Construct implements IAutoScalingGroup
instanceType: props.instanceType.toString(),
securityGroups: securityGroupsToken,
iamInstanceProfile: iamProfile.ref,
userData: userDataToken
userData: userDataToken,
associatePublicIpAddress: props.associatePublicIpAddress,
});

launchConfig.addDependency(this.role);
Expand Down Expand Up @@ -748,4 +757,4 @@ export interface MetricTargetTrackingProps extends BaseTargetTrackingProps {
* Value to keep the metric around
*/
targetValue: number;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { expect, haveResource, haveResourceLike, ResourcePart } from '@aws-cdk/assert';
import { expect, haveResource, haveResourceLike, InspectionFailure, ResourcePart } from '@aws-cdk/assert';
import ec2 = require('@aws-cdk/aws-ec2');
import iam = require('@aws-cdk/aws-iam');
import cdk = require('@aws-cdk/cdk');
Expand Down Expand Up @@ -404,6 +404,79 @@ export = {
}));
test.done();
},
'allows association of public IP address'(test: Test) {
// GIVEN
const stack = new cdk.Stack();
const vpc = mockVpc(stack);

// WHEN
new autoscaling.AutoScalingGroup(stack, 'MyStack', {
instanceType: new ec2.InstanceTypePair(ec2.InstanceClass.M4, ec2.InstanceSize.Micro),
machineImage: new ec2.AmazonLinuxImage(),
vpc,
minSize: 0,
maxSize: 0,
desiredCapacity: 0,
associatePublicIpAddress: true,
});

// THEN
expect(stack).to(haveResource("AWS::AutoScaling::LaunchConfiguration", {
AssociatePublicIpAddress: true,
}
));
test.done();
},
'allows disassociation of public IP address'(test: Test) {
// GIVEN
const stack = new cdk.Stack();
const vpc = mockVpc(stack);

// WHEN
new autoscaling.AutoScalingGroup(stack, 'MyStack', {
instanceType: new ec2.InstanceTypePair(ec2.InstanceClass.M4, ec2.InstanceSize.Micro),
machineImage: new ec2.AmazonLinuxImage(),
vpc,
minSize: 0,
maxSize: 0,
desiredCapacity: 0,
associatePublicIpAddress: false,
});

// THEN
expect(stack).to(haveResource("AWS::AutoScaling::LaunchConfiguration", {
AssociatePublicIpAddress: false,
}
));
test.done();
},
'does not specify public IP address association by default'(test: Test) {
// GIVEN
const stack = new cdk.Stack();
const vpc = mockVpc(stack);

// WHEN
new autoscaling.AutoScalingGroup(stack, 'MyStack', {
instanceType: new ec2.InstanceTypePair(ec2.InstanceClass.M4, ec2.InstanceSize.Micro),
machineImage: new ec2.AmazonLinuxImage(),
vpc,
minSize: 0,
maxSize: 0,
desiredCapacity: 0,
});

// THEN
expect(stack).to(haveResource("AWS::AutoScaling::LaunchConfiguration", (resource: any, errors: InspectionFailure) => {
for (const key of Object.keys(resource)) {
if (key === "AssociatePublicIpAddress") {
errors.failureReason = "Has AssociatePublicIpAddress";
return false;
}
}
return true;
}));
test.done();
},
};

function mockVpc(stack: cdk.Stack) {
Expand Down

0 comments on commit 23c9afc

Please sign in to comment.