<p>Your CloudFormation template currently has the following resources:</p>
<ul>
<li>A Launch Configuration</li>
<li>An Auto Scaling Group</li>
<li>A Load Balancer</li>
</ul>
<p>Follow these steps in sequence to attach the auto scaling group to the load balancer:</p>
<ol>
<li><p>Add a listener resource to your CloudFormation template, as described <a href="https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-elasticloadbalancingv2-listener.html" rel="noreferrer">here</a>. Set its <code>LoadBalancerArn</code> property to the load balancer's ARN.</p>
</li>
<li><p>Add a target group resource to your CloudFormation template, as described <a href="https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-elasticloadbalancingv2-targetgroup.html" rel="noreferrer">here</a>.</p>
</li>
<li><p>Add a &quot;listener rule&quot; resource to your CloudFormation template, as described <a href="https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-elasticloadbalancingv2-listenerrule.html" rel="noreferrer">here</a>. Set its <code>ListenerArn</code> property to the listener's ARN. In its <code>Actions</code> property, add a <code>forward</code> action &amp; provide the target group's ARN like this:</p>
</li>
</ol>
<pre><code>Actions:
- Type: forward
  TargetGroupArn: !Ref MyTargetGroup
</code></pre>
<ol start="4">
<li>Add the target group's ARN to the <code>TargetGroupARNs</code> property of the auto scaling group, as described <a href="https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-as-group.html#cfn-as-group-targetgrouparns" rel="noreferrer">here</a>.</li>
</ol>
<p>If you prefer to visualize a chain of CloudFormation resources linking the load balancer to the auto scaling group, it'll look like this:</p>
<p><a href="https://i.sstatic.net/OaeBH.png" rel="noreferrer"><img src="https://i.sstatic.net/OaeBH.png" alt="enter image description here" /></a></p>
<p>Here's a complete example: <a href="https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-elasticloadbalancingv2-listenerrule.html#aws-resource-elasticloadbalancingv2-listenerrule--examples--HTTP_Header_Rule_Example--yaml" rel="noreferrer">LB to ASG Example</a>.</p>

CC BY-SA 4.0
2020-07-15T16:20:29.503Z
2020-07-15T16:20:29.503Z
Harish KM
<p>I'm working with AWS cloudformation I have an autoscaling group for a couple of machines running an API server, and I would like to set up an application load balancer as a single entry point, so that client application only sees one URL. I was able to create the load balancer and the autoscaling group but I don't know how to connect them.</p>

<pre><code>Resources:
  NodeLaunchConfig:
    Type: AWS::AutoScaling::LaunchConfiguration
    Properties:
      AssociatePublicIpAddress: false
      # IamInstanceProfile: !Ref NodeInstanceProfile
      ImageId: !Ref NodeImageId
      InstanceType: !Ref NodeInstanceType
      KeyName: !Ref KeyName
      # SecurityGroups is like this on purpuse, this work, IGNORE the error message if any
      SecurityGroups: !Ref NodeSecurityGroup
      BlockDeviceMappings:
        - DeviceName: /dev/xvda
          Ebs:
            VolumeSize: 200
            VolumeType: gp2
            DeleteOnTermination: true

  NodeGroup:
    Type: AWS::AutoScaling::AutoScalingGroup
    DependsOn: LoadBalancer
    Properties:
      LaunchConfigurationName: !Ref NodeLaunchConfig
      DesiredCapacity: "1"
      MinSize: "0"
      MaxSize: "10"
      # TargetGroupARNs:
      #   - !Ref LoadBalancer
      TerminationPolicies:
        - NewestInstance
      # VPCZoneIdentifier is like this on purpuse, this work, IGNORE the error message if any
      VPCZoneIdentifier: !Ref Subnets
      Tags:
        - Key: Name
          Value: !Sub ${AWS::StackName}-node
          PropagateAtLaunch: true
        - Key: Owner
          Value: !Ref Owner
          PropagateAtLaunch: true
        - Key: Department
          Value: AI
          PropagateAtLaunch: true
        - Key: Env
          Value: !Ref Environment
          PropagateAtLaunch: true
        - Key: Job
          Value: KubernetesGroupNode
          PropagateAtLaunch: true
    UpdatePolicy:
      AutoScalingRollingUpdate:
        MaxBatchSize: 1
        MinInstancesInService: 1
        PauseTime: PT5M

  LoadBalancer:
    Type: AWS::ElasticLoadBalancingV2::LoadBalancer
    Properties:
      Name: decrypt-loadbalancer
      # SecurityGroups is like this on purpuse, this work, IGNORE the error message if any
      SecurityGroups: !Ref NodeSecurityGroup
      # Subnets is like this on purpuse, this work, IGNORE the error message if any
      Subnets: !Ref Subnets
      Tags:
        - Key: Name
          Value: !Sub ${AWS::StackName}-node
        - Key: Owner
          Value: !Ref Owner
        - Key: Department
          Value: AI
        - Key: Env
          Value: !Ref Environment
        - Key: Job
          Value: KubernetesGroupNode
      Type: application
</code></pre>

<amazon-web-services>
is it possible to connect an AWS autoscaling group with an application load balancer?
juanp_1982
# is it possible to connect an AWS autoscaling group with an application load balancer?

(Asked by: username_0 on 2020-05-19)

I'm working with AWS cloudformation I have an autoscaling group for a couple of machines running an API server, and I would like to set up an application load balancer as a single entry point, so that client application only sees one URL. I was able to create the load balancer and the autoscaling group but I don't know how to connect them.
Resources:
  NodeLaunchConfig:
    Type: AWS::AutoScaling::LaunchConfiguration
    Properties:
      AssociatePublicIpAddress: false
      # IamInstanceProfile: !Ref NodeInstanceProfile
      ImageId: !Ref NodeImageId
      InstanceType: !Ref NodeInstanceType
      KeyName: !Ref KeyName
      # SecurityGroups is like this on purpuse, this work, IGNORE the error message if any
      SecurityGroups: !Ref NodeSecurityGroup
      BlockDeviceMappings:
        - DeviceName: /dev/xvda
          Ebs:
            VolumeSize: 200
            VolumeType: gp2
            DeleteOnTermination: true

  NodeGroup:
    Type: AWS::AutoScaling::AutoScalingGroup
    DependsOn: LoadBalancer
    Properties:
      LaunchConfigurationName: !Ref NodeLaunchConfig
      DesiredCapacity: "1"
      MinSize: "0"
      MaxSize: "10"
      # TargetGroupARNs:
      #   - !Ref LoadBalancer
      TerminationPolicies:
        - NewestInstance
      # VPCZoneIdentifier is like this on purpuse, this work, IGNORE the error message if any
      VPCZoneIdentifier: !Ref Subnets
      Tags:
        - Key: Name
          Value: !Sub ${AWS::StackName}-node
          PropagateAtLaunch: true
        - Key: Owner
          Value: !Ref Owner
          PropagateAtLaunch: true
        - Key: Department
          Value: AI
          PropagateAtLaunch: true
        - Key: Env
          Value: !Ref Environment
          PropagateAtLaunch: true
        - Key: Job
          Value: KubernetesGroupNode
          PropagateAtLaunch: true
    UpdatePolicy:
      AutoScalingRollingUpdate:
        MaxBatchSize: 1
        MinInstancesInService: 1
        PauseTime: PT5M

  LoadBalancer:
    Type: AWS::ElasticLoadBalancingV2::LoadBalancer
    Properties:
      Name: decrypt-loadbalancer
      # SecurityGroups is like this on purpuse, this work, IGNORE the error message if any
      SecurityGroups: !Ref NodeSecurityGroup
      # Subnets is like this on purpuse, this work, IGNORE the error message if any
      Subnets: !Ref Subnets
      Tags:
        - Key: Name
          Value: !Sub ${AWS::StackName}-node
        - Key: Owner
          Value: !Ref Owner
        - Key: Department
          Value: AI
        - Key: Env
          Value: !Ref Environment
        - Key: Job
          Value: KubernetesGroupNode
      Type: application

**Tags:** amazon-web-services


---

## 1 answers

### Answer by username_1 on 2020-07-15. Score: 5

Your CloudFormation template currently has the following resources:

A Launch Configuration
An Auto Scaling Group
A Load Balancer

Follow these steps in sequence to attach the auto scaling group to the load balancer:

Add a listener resource to your CloudFormation template, as described here. Set its LoadBalancerArn property to the load balancer's ARN.

Add a target group resource to your CloudFormation template, as described here.

Add a "listener rule" resource to your CloudFormation template, as described here. Set its ListenerArn property to the listener's ARN. In its Actions property, add a forward action & provide the target group's ARN like this:


Actions:
- Type: forward
  TargetGroupArn: !Ref MyTargetGroup


Add the target group's ARN to the TargetGroupARNs property of the auto scaling group, as described here.

If you prefer to visualize a chain of CloudFormation resources linking the load balancer to the auto scaling group, it'll look like this:

Here's a complete example: LB to ASG Example.


---

