-
Notifications
You must be signed in to change notification settings - Fork 4k
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
Changes from 1 commit
ff4a64b
7ffd142
2a69ad6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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; | ||
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)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit (parens not needed): |
||
|
||
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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was 50/50 here -- sounds like |
||
this.securityGroups.push(securityGroup); | ||
} | ||
|
||
public attachToClassicLB(loadBalancer: ec2.ClassicLoadBalancer): void { | ||
this.loadBalancerNames.push(loadBalancer.loadBalancerName); | ||
} | ||
|
There was a problem hiding this comment.
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?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hell yes!