7

In AWS' Cloudformation, how do I attach an Autoscaling Group (ASG) to an Application Load Balancer Target Group?

There does not appear to be any direct way to do that directly in a Cloudformation Template (CFT), though it it possible using the AQWS CLI or API. The AWS::ElasticLoadBalancingV2::TargetGroup resource only offers these target types:

  • instance. Targets are specified by instance ID.
  • ip. Targets are specified by IP address.
  • lambda. The target groups contains a single Lambda function.
Nicholas Carey
  • 71,308
  • 16
  • 93
  • 135

1 Answers1

15

That is because, apparently, one does not attach an ASG to a target group; instead, one attaches a target group or groups to an ASG.

Seems a little backwards to me, but I'm sure it has to do with the ASG needing to register/deregister its instances as it scales in and out.

See the documentation for the AWS::AutoScaling::AutoScalingGroup resource for details.

Example:

TargetGroup:
  Type: AWS::ElasticLoadBalancingV2::TargetGroup
  Properties:
    VpcId: !Ref VPC
    TargetType: instance
    Port: 80
    Protocol: HTTP

AutoScalingGroup: 
  Type: AWS::AutoScaling::AutoScalingGroup
  Properties: 
    AvailabilityZones: !GetAZs !Ref "AWS::Region"
    MaxSize: "3"
    MinSize: "1"
    TargetGroupArns:
      - !Ref TargetGroup
Nicholas Carey
  • 71,308
  • 16
  • 93
  • 135
  • And then how do you connect a target group with to ABL (AWS::ElasticLoadBalancingV2::LoadBalancer) ?? – juanp_1982 Sep 03 '19 at 15:21
  • 2
    @juanp_1982 — One creates a *listener* (or listeners) within the ALB in question. Then, for that listener, one creates a rule (or rules) that forwards traffic to the target group in question. – Nicholas Carey Sep 03 '19 at 19:51
  • I just Followed the above template but it is showing the instance in target group as `unused`. how can we change its status from `unused` to `healthy`??? – Zain Ul Abideen Apr 27 '21 at 05:21
  • Just to help other people the field name TargetGroupArns must be TargetGroupARNs with letter RN capitalize, wihtout this cause syntax error. – YLR Mar 23 '22 at 15:46