1
1
import ec2 = require( '@aws-cdk/aws-ec2' ) ;
2
2
import elb = require( '@aws-cdk/aws-elasticloadbalancing' ) ;
3
+ import elbv2 = require( '@aws-cdk/aws-elasticloadbalancingv2' ) ;
3
4
import iam = require( '@aws-cdk/aws-iam' ) ;
4
5
import sns = require( '@aws-cdk/aws-sns' ) ;
5
6
import cdk = require( '@aws-cdk/cdk' ) ;
@@ -136,7 +137,8 @@ export interface AutoScalingGroupProps {
136
137
*
137
138
* The ASG spans all availability zones.
138
139
*/
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 {
140
142
/**
141
143
* The type of OS instances of this fleet are running.
142
144
*/
@@ -157,6 +159,7 @@ export class AutoScalingGroup extends cdk.Construct implements elb.ILoadBalancer
157
159
private readonly securityGroup : ec2 . SecurityGroupRef ;
158
160
private readonly securityGroups : ec2 . SecurityGroupRef [ ] = [ ] ;
159
161
private readonly loadBalancerNames : string [ ] = [ ] ;
162
+ private readonly targetGroupArns : string [ ] = [ ] ;
160
163
161
164
constructor ( parent : cdk . Construct , name : string , props : AutoScalingGroupProps ) {
162
165
super ( parent , name ) ;
@@ -206,7 +209,8 @@ export class AutoScalingGroup extends cdk.Construct implements elb.ILoadBalancer
206
209
maxSize : maxSize . toString ( ) ,
207
210
desiredCapacity : desiredCapacity . toString ( ) ,
208
211
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 ) ,
210
214
} ;
211
215
212
216
if ( props . notificationsTopic ) {
@@ -241,10 +245,30 @@ export class AutoScalingGroup extends cdk.Construct implements elb.ILoadBalancer
241
245
this . securityGroups . push ( securityGroup ) ;
242
246
}
243
247
248
+ /**
249
+ * Attach to a classic load balancer
250
+ */
244
251
public attachToClassicLB ( loadBalancer : elb . LoadBalancer ) : void {
245
252
this . loadBalancerNames . push ( loadBalancer . loadBalancerName ) ;
246
253
}
247
254
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
+
248
272
/**
249
273
* Add command to the startup script of fleet instances.
250
274
* The command must be in the scripting language supported by the fleet's OS (i.e. Linux/Windows).
0 commit comments