Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(autoscaling): Support AssociatePublicIpAddress #1604

Merged
merged 2 commits into from
Jan 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ha, neat trick.

return false;
}
}
return true;
}));
test.done();
},
};

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