Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Do I Attach an ASG to an ALB Target Group?

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.
like image 906
Nicholas Carey Avatar asked Dec 23 '22 01:12

Nicholas Carey


1 Answers

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
like image 125
Nicholas Carey Avatar answered Feb 08 '23 23:02

Nicholas Carey