Autoscaling and application load balancer - How to make them collaborate?

0

I created a launch template, an autoscaling group and a target group

all is running in 3 subnets, dualstack ipv4/ipv6 and load balancer is dual-stack-without-public-ip

I can access my instance using http://[Ipv6] (the ipv6 of the instance, launched from autoscaling group), so, single instances are up and running and serving website well

I am stuck at this point: I want load balancer to collaborate with autoscaling group to ... well .. to do load balancing !

  LoadBalancer:
    Type: AWS::ElasticLoadBalancingV2::LoadBalancer
    Properties:
      Name: LoadBalancer
      Scheme: internet-facing
      IpAddressType: dualstack-without-public-ipv4
      Subnets:
        - !Ref PublicIpv6SubnetA
        - !Ref PublicIpv6SubnetB
        - !Ref PublicIpv6SubnetC
      SecurityGroups:
        - !Ref WebServerSecurityGroup  # Usa il security group esistente
      LoadBalancerAttributes:
        - Key: idle_timeout.timeout_seconds
          Value: 60
      Tags:
        - Key: Name
          Value: !Sub "${AWS::StackName}-Load-Balancer"

  InstanceLaunchTemplate:
    Type: AWS::EC2::LaunchTemplate
    Properties:
      LaunchTemplateName: InstanceLaunchTemplate
      LaunchTemplateData:
        ImageId: !Ref LatestAmiId
        InstanceType: t3.micro
        SecurityGroupIds:
          - !Ref WebServerSecurityGroup
        UserData:
          Fn::Base64: |
            #!/bin/bash
            yum install httpd -y
            service httpd start
            echo "<html><body><h1>Hello from ipv6-only web server!<h1></body></html>" > /var/www/html/index.html
      TagSpecifications:
        - ResourceType: "launch-template"
          Tags:
          - Key: Name
            Value: !Sub "${AWS::StackName}-Launch-Template"

  InstanceTargetGroup:
    Type: AWS::ElasticLoadBalancingV2::TargetGroup
    Properties:
      Name: TargetGroup
      Port: 80  # La porta su cui il target group ascolta il traffico
      Protocol: HTTP  # Il protocollo utilizzato dal target group (HTTP, HTTPS, TCP, etc.)
      VpcId: !Ref VPC  # ID della VPC in cui verrà creato il target group
      TargetType: instance
      HealthCheckEnabled: true  # Abilita i controlli di integrità
      HealthCheckProtocol: HTTP  # Protocollo utilizzato per i controlli di integrità
      HealthCheckPort: 80  # Porta su cui eseguire i controlli di integrità
      HealthCheckPath: /  # Percorso per i controlli di integrità
      Matcher:  # Configurazione del criterio di corrispondenza dei controlli di integrità
        HttpCode: 200
      TargetGroupAttributes:  # Attributi aggiuntivi del target group (opzionale)
        - Key: deregistration_delay.timeout_seconds
          Value: "30"  # Timeout in secondi prima che una destinazione viene considerata non disponibile dopo una deregistrazione
      Tags:  # Tags per identificare e organizzare il target group (opzionale)
        - Key: Name
          Value: !Sub "${AWS::StackName}-Target-Group"

  AutoScalingGroup:
    Type: AWS::AutoScaling::AutoScalingGroup
    Properties:
      AutoScalingGroupName: AutoScalingGroup
      LaunchTemplate:
        LaunchTemplateName: InstanceLaunchTemplate
        Version: !GetAtt InstanceLaunchTemplate.LatestVersionNumber
      MinSize: 1  # Numero minimo di istanze
      MaxSize: 3  # Numero massimo di istanze
      DesiredCapacity: 1  # Capacità iniziale
      VPCZoneIdentifier:
        - !Ref PublicIpv6SubnetA
        - !Ref PublicIpv6SubnetB
        - !Ref PublicIpv6SubnetC
      TargetGroupARNs:
        - !Ref InstanceTargetGroup  # Riferimento al Target Group appena creato
      Tags:
        - Key: Name
          Value: !Sub "${AWS::StackName}-Auto-Scaling-Group"
          PropagateAtLaunch: true

What should I do to 'attach' the load balancer to my autoscale group?

I think i am missing a piece of theory.

How can AWS load balancing using my target group, where instances are created by AutoScalingGroup?

3 Answers
1

Hello.

"TargetGroupARNs" is specified in the AutoScaling settings, so when EC2 scales out, it will be linked to the specified target group.
Also, it may not be included because it is not the full text of the CloudFormation template, but please create a listener using "AWS::ElasticLoadBalancingV2::Listener" to link the target group to the ALB.
https://docs.aws.amazon.com/ja_jp/AWSCloudFormation/latest/UserGuide/aws-resource-elasticloadbalancingv2-listener.html

For example, to add a listener for port 80, you need to add the following template.

  AlbListener:
    Type: AWS::ElasticLoadBalancingV2::Listener
    Properties:
      DefaultActions:
        - TargetGroupArn: !Ref InstanceTargetGroup
          Type: forward
      LoadBalancerArn: !Ref LoadBalancer
      Port: 80
      Protocol: HTTP
profile picture
EXPERT
answered a month ago
profile picture
EXPERT
reviewed a month ago
0

The key missing element is the Listener configuration for your Load Balancer.

For example:

  LoadBalancerListener:
    Type: AWS::ElasticLoadBalancingV2::Listener
    Properties:
      DefaultActions:
        - Type: forward
          TargetGroupArn: !Ref InstanceTargetGroup
      LoadBalancerArn: !Ref LoadBalancer
      Port: 80
      Protocol: HTTP
profile pictureAWS
EXPERT
answered a month ago
profile picture
EXPERT
reviewed a month ago
0

Hello,

You have almost completed the implementation to allow your Load Balancer to forward traffic across EC2 instances in the Auto Scaling group. However, you need to configure the Listeners in the Application Load Balancer (ALB). Listeners are essential as they enable the ALB to listen for incoming traffic and forward it to the target group. To read more about Listeners, https://docs.aws.amazon.com/elasticloadbalancing/latest/application/load-balancer-listeners.html

LoadBalancerListener:
    Type: AWS::ElasticLoadBalancingV2::Listener
    Properties:
      DefaultActions:
        - Type: forward
          TargetGroupArn: !Ref InstanceTargetGroup
      LoadBalancerArn: !Ref LoadBalancer
      Port: 80
      Protocol: HTTP
profile picture
EXPERT
answered a month ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions