How can I set up an AWS elastic load balancer to achieve the requirements of setting up a load balancer for ?

260    Asked by CsabaToth in AWS , Asked on Mar 26, 2024

I am currently working as a cloud architect for a large e-commerce company that has recently migrated its application to AWS. As a part of the migration, I have been tasked with setting up a load balancer for distributing incoming traffic across multiple EC2 instances hosting your application. Describe to me how can I set up an AWS elastic load balancer for achieving these requirements. Include the types of AWS LBs you would use, the Configuration setting I would apply and how can I ensure high availability and scalability for my application. 

Answered by Deepa bhawana

 In the context of AWS, here is a detailed plan that would outline the setup of the AWS elastic load balancer for the given scenario:-

Application load balancer

You can create a target group for each microservices for communication with EC2 Instances.

You can configure listeners to handle incoming HTTP/HTTPS traffic and define which can route to target groups.

You can enable session stickiness for maintaining session persistence for the user interacting with specific microservices.

Network load balancer

You can define the target group with the health Checking setting for EC2 instances running my microservices specifying the protocol and port for health Checking.

You can configure listeners for TCP/IP, mapping to target the group based on the required port number for each microservice.

You can implement connection draining for gracefully handling the shutdown of EC2 instances without disrupting the active connection.

Here is the example given of how you can Create an application load balancer (ALB) with listeners and target groups:-

Resources:

  MyALB:
    Type: AWS::ElasticLoadBalancingV2::LoadBalancer
    Properties:
      Name: MyALB
      Subnets:
        - !Ref Subnet1
        - !Ref Subnet2
      SecurityGroups:
!Ref MyALBSecurityGroup
      Scheme: internet-facing
      Type: application
  MyTargetGroup:
    Type: AWS::ElasticLoadBalancingV2::TargetGroup
    Properties:
      Name: MyTargetGroup
      Port: 80
      Protocol: HTTP
      VpcId: !Ref MyVPC
      HealthCheckEnabled: true
      HealthCheckPath: /health
      HealthCheckProtocol: HTTP
      TargetType: instance
      Targets:
        - Id: !Ref EC2Instance1
        - Id: !Ref EC2Instance2
  MyListener:
    Type: AWS::ElasticLoadBalancingV2::Listener
    Properties:
      DefaultActions:
Type: forward
          TargetGroupArn: !Ref MyTargetGroup
      LoadBalancerArn: !Ref MyALB
      Port: 80
      Protocol: HTTP
Here is the example given of how you can Create a Network load balancer (NLB) with listeners and target groups:-
Resources:
  MyNLB:
    Type: AWS::ElasticLoadBalancingV2::LoadBalancer
    Properties:
      Name: MyNLB
      Subnets:
        - !Ref Subnet1
        - !Ref Subnet2
      SecurityGroups:
!Ref MyNLBSecurityGroup
      Scheme: internet-facing
      Type: network
  MyTargetGroup:

    Type: AWS::ElasticLoadBalancingV2::TargetGroup

    Properties:
      Name: MyTargetGroup
      Port: 80
      Protocol: TCP
      VpcId: !Ref MyVPC
      HealthCheckEnabled: true
      HealthCheckIntervalSeconds: 30
      HealthCheckPort: 80
      HealthCheckProtocol: TCP
      HealthCheckTimeoutSeconds: 10
      HealthyThresholdCount: 3
      TargetType: ip
      Targets:
        - Id: !Ref EC2Instance1
        - Id: !Ref EC2Instance2
  MyListener:
    Type: AWS::ElasticLoadBalancingV2::Listener
    Properties:
      DefaultActions:
Type: forward
          TargetGroupArn: !Ref MyTargetGroup
      LoadBalancerArn: !Ref MyNLB
      Port: 80
      Protocol: TCP


Your Answer

Interviews

Parent Categories