2

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
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:

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

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

  3. 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
  1. 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:

enter image description here

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.