Skip to content

Commit bd9ee01

Browse files
authored
feat(aws-elasticloadbalancingv2): support for ALB/NLB (#750)
BREAKING CHANGE: Adds classes for modeling Application and Network Load Balancers. AutoScalingGroups now implement the interface that makes constructs a load balancing target. The breaking change is that Security Group rule identifiers have been changed in order to make adding rules more reliable. No code changes are necessary but existing deployments may experience unexpected changes.
1 parent 6c0e75b commit bd9ee01

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+5092
-52
lines changed

build.sh

+23-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,27 @@
11
#!/bin/bash
22
set -euo pipefail
33

4+
bail="--no-bail"
5+
while [[ "${1:-}" != "" ]]; do
6+
case $1 in
7+
-h|--help)
8+
echo "Usage: build.sh [--bail|-b] [--force|-f]"
9+
exit 1
10+
;;
11+
-b|--bail)
12+
bail="--bail"
13+
;;
14+
-f|--force)
15+
export CDK_BUILD="--force"
16+
;;
17+
*)
18+
echo "Unrecognized parameter: $1"
19+
exit 1
20+
;;
21+
esac
22+
shift
23+
done
24+
425
if [ ! -d node_modules ]; then
526
/bin/bash ./install.sh
627
fi
@@ -24,10 +45,10 @@ trap "rm -rf $MERKLE_BUILD_CACHE" EXIT
2445

2546
echo "============================================================================================="
2647
echo "building..."
27-
time lerna run --no-bail --stream build || fail
48+
time lerna run $bail --stream build || fail
2849

2950
echo "============================================================================================="
3051
echo "testing..."
31-
lerna run --no-bail --stream test || fail
52+
lerna run $bail --stream test || fail
3253

3354
touch $BUILD_INDICATOR

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

+26-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import ec2 = require('@aws-cdk/aws-ec2');
22
import elb = require('@aws-cdk/aws-elasticloadbalancing');
3+
import elbv2 = require('@aws-cdk/aws-elasticloadbalancingv2');
34
import iam = require('@aws-cdk/aws-iam');
45
import sns = require('@aws-cdk/aws-sns');
56
import cdk = require('@aws-cdk/cdk');
@@ -136,7 +137,8 @@ export interface AutoScalingGroupProps {
136137
*
137138
* The ASG spans all availability zones.
138139
*/
139-
export class AutoScalingGroup extends cdk.Construct implements elb.ILoadBalancerTarget, ec2.IConnectable {
140+
export class AutoScalingGroup extends cdk.Construct implements elb.ILoadBalancerTarget, ec2.IConnectable,
141+
elbv2.IApplicationLoadBalancerTarget, elbv2.INetworkLoadBalancerTarget {
140142
/**
141143
* The type of OS instances of this fleet are running.
142144
*/
@@ -157,6 +159,7 @@ export class AutoScalingGroup extends cdk.Construct implements elb.ILoadBalancer
157159
private readonly securityGroup: ec2.SecurityGroupRef;
158160
private readonly securityGroups: ec2.SecurityGroupRef[] = [];
159161
private readonly loadBalancerNames: string[] = [];
162+
private readonly targetGroupArns: string[] = [];
160163

161164
constructor(parent: cdk.Construct, name: string, props: AutoScalingGroupProps) {
162165
super(parent, name);
@@ -206,7 +209,8 @@ export class AutoScalingGroup extends cdk.Construct implements elb.ILoadBalancer
206209
maxSize: maxSize.toString(),
207210
desiredCapacity: desiredCapacity.toString(),
208211
launchConfigurationName: launchConfig.ref,
209-
loadBalancerNames: new cdk.Token(() => this.loadBalancerNames),
212+
loadBalancerNames: new cdk.Token(() => this.loadBalancerNames.length > 0 ? this.loadBalancerNames : undefined),
213+
targetGroupArns: new cdk.Token(() => this.targetGroupArns.length > 0 ? this.targetGroupArns : undefined),
210214
};
211215

212216
if (props.notificationsTopic) {
@@ -241,10 +245,30 @@ export class AutoScalingGroup extends cdk.Construct implements elb.ILoadBalancer
241245
this.securityGroups.push(securityGroup);
242246
}
243247

248+
/**
249+
* Attach to a classic load balancer
250+
*/
244251
public attachToClassicLB(loadBalancer: elb.LoadBalancer): void {
245252
this.loadBalancerNames.push(loadBalancer.loadBalancerName);
246253
}
247254

255+
/**
256+
* Attach to ELBv2 Application Target Group
257+
*/
258+
public attachToApplicationTargetGroup(targetGroup: elbv2.ApplicationTargetGroup): elbv2.LoadBalancerTargetProps {
259+
this.targetGroupArns.push(targetGroup.targetGroupArn);
260+
targetGroup.registerConnectable(this);
261+
return { targetType: elbv2.TargetType.SelfRegistering };
262+
}
263+
264+
/**
265+
* Attach to ELBv2 Application Target Group
266+
*/
267+
public attachToNetworkTargetGroup(targetGroup: elbv2.NetworkTargetGroup): elbv2.LoadBalancerTargetProps {
268+
this.targetGroupArns.push(targetGroup.targetGroupArn);
269+
return { targetType: elbv2.TargetType.SelfRegistering };
270+
}
271+
248272
/**
249273
* Add command to the startup script of fleet instances.
250274
* The command must be in the scripting language supported by the fleet's OS (i.e. Linux/Windows).

packages/@aws-cdk/aws-autoscaling/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
"dependencies": {
6262
"@aws-cdk/aws-ec2": "^0.9.2",
6363
"@aws-cdk/aws-elasticloadbalancing": "^0.9.2",
64+
"@aws-cdk/aws-elasticloadbalancingv2": "^0.9.2",
6465
"@aws-cdk/aws-iam": "^0.9.2",
6566
"@aws-cdk/aws-sns": "^0.9.2",
6667
"@aws-cdk/cdk": "^0.9.2"

packages/@aws-cdk/aws-autoscaling/test/integ.asg-w-loadbalancer.expected.json packages/@aws-cdk/aws-autoscaling/test/integ.asg-w-classic-loadbalancer.expected.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,7 @@
458458
}
459459
}
460460
},
461-
"FleetInstanceSecurityGroupPort80LBtofleetDC12B17A": {
461+
"FleetInstanceSecurityGroupfromawscdkec2integLBSecurityGroupDEF4F99A8025E910CB": {
462462
"Type": "AWS::EC2::SecurityGroupIngress",
463463
"Properties": {
464464
"IpProtocol": "tcp",
@@ -581,7 +581,7 @@
581581
}
582582
}
583583
},
584-
"LBSecurityGroupPort80LBtofleet0986F2E8": {
584+
"LBSecurityGrouptoawscdkec2integFleetInstanceSecurityGroupB03BE84D80B371C596": {
585585
"Type": "AWS::EC2::SecurityGroupEgress",
586586
"Properties": {
587587
"GroupId": {

0 commit comments

Comments
 (0)