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

Attach Security Group to Launch Config #636

Merged
merged 3 commits into from
Sep 3, 2018
Merged
Changes from 1 commit
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
16 changes: 14 additions & 2 deletions packages/@aws-cdk/aws-autoscaling/lib/auto-scaling-group.ts
Original file line number Diff line number Diff line change
@@ -153,14 +153,16 @@ export class AutoScalingGroup extends cdk.Construct implements ec2.IClassicLoadB

private readonly userDataLines = new Array<string>();
private readonly autoScalingGroup: cloudformation.AutoScalingGroupResource;
private readonly securityGroup: ec2.SecurityGroup;
private readonly securityGroup: ec2.SecurityGroupRef;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Should we prefer *Ref objects in places like this?

Copy link
Contributor

Choose a reason for hiding this comment

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

Hell yes!

private readonly securityGroups: ec2.SecurityGroupRef[] = [];
private readonly loadBalancerNames: cdk.Token[] = [];

constructor(parent: cdk.Construct, name: string, props: AutoScalingGroupProps) {
super(parent, name);

this.securityGroup = new ec2.SecurityGroup(this, 'InstanceSecurityGroup', { vpc: props.vpc });
this.connections = new ec2.Connections({ securityGroup: this.securityGroup });
this.securityGroups.push(this.securityGroup);

if (props.allowAllOutbound !== false) {
this.connections.allowTo(new ec2.AnyIPv4(), new ec2.AllConnections(), 'Outbound traffic allowed by default');
@@ -177,12 +179,13 @@ export class AutoScalingGroup extends cdk.Construct implements ec2.IClassicLoadB
// use delayed evaluation
const machineImage = props.machineImage.getImage(this);
const userDataToken = new cdk.Token(() => new cdk.FnBase64((machineImage.os.createUserData(this.userDataLines))));
const securityGroupsToken = new cdk.Token(() => this.securityGroups.map((sg) => sg.securityGroupId));
Copy link
Contributor

Choose a reason for hiding this comment

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

nit (parens not needed): (sg) => sg.securityGroupId => sg => sg.securityGroupId


const launchConfig = new cloudformation.LaunchConfigurationResource(this, 'LaunchConfig', {
imageId: machineImage.imageId,
keyName: props.keyName,
instanceType: props.instanceType.toString(),
securityGroups: [this.securityGroup.securityGroupId],
securityGroups: securityGroupsToken,
iamInstanceProfile: iamProfile.ref,
userData: userDataToken
});
@@ -227,6 +230,15 @@ export class AutoScalingGroup extends cdk.Construct implements ec2.IClassicLoadB
this.applyUpdatePolicies(props);
}

/**
* Attach the security group to all instances in this autoscaling group
*
* @param securityGroup: The SecurityGroupRef to add
*/
public attachSecurityGroup(securityGroup: ec2.SecurityGroupRef): void {
Copy link
Contributor

Choose a reason for hiding this comment

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

addSecurityGroup?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was 50/50 here -- sounds like addSecurityGroup ftw

this.securityGroups.push(securityGroup);
}

public attachToClassicLB(loadBalancer: ec2.ClassicLoadBalancer): void {
this.loadBalancerNames.push(loadBalancer.loadBalancerName);
}
30 changes: 30 additions & 0 deletions packages/@aws-cdk/aws-autoscaling/test/test.auto-scaling-group.ts
Original file line number Diff line number Diff line change
@@ -332,6 +332,30 @@ export = {

test.done();
},
'can add Security Group to Fleet'(test: Test) {
// GIVEN
const stack = new cdk.Stack(undefined, 'MyStack', { env: { region: 'us-east-1', account: '1234' }});
const vpc = mockVpc(stack);

// WHEN
const asg = new autoscaling.AutoScalingGroup(stack, 'MyFleet', {
instanceType: new ec2.InstanceTypePair(ec2.InstanceClass.M4, ec2.InstanceSize.Micro),
machineImage: new ec2.AmazonLinuxImage(),
vpc,
});
asg.attachSecurityGroup(mockSecurityGroup(stack));
expect(stack).to(haveResource("AWS::AutoScaling::LaunchConfiguration", {
SecurityGroups: [
{
"Fn::GetAtt": [
"MyFleetInstanceSecurityGroup774E8234",
"GroupId"
]
},
'most-secure'],
}));
test.done();
},
};

function mockVpc(stack: cdk.Stack) {
@@ -342,3 +366,9 @@ function mockVpc(stack: cdk.Stack) {
privateSubnetIds: [ new ec2.VpcSubnetId('pri1') ],
});
}

function mockSecurityGroup(stack: cdk.Stack) {
return ec2.SecurityGroupRef.import(stack, 'MySG', {
securityGroupId: new ec2.SecurityGroupId('most-secure'),
});
}