DEV Community

Radurga Rajendran
Radurga Rajendran

Posted on

Accessing Multiple Instances via Load Balancer in AWS

When deploying scalable applications, distributing traffic efficiently across multiple instances is crucial for performance, fault tolerance, and reliability. AWS provides Elastic Load Balancing (ELB) to simplify this process. Here,we’ll explore the concept of load balancers, target groups, security groups, and subnets, along with a step-by-step process to setting up an Application Load Balancer (ALB) to access multiple instances.

Load Balancer:

A Load Balancer is a service that distributes incoming application traffic across multiple targets (e.g., EC2 instances) in one or more availability zones. It improves the availability and fault tolerance of your application by ensuring no single instance is overwhelmed by traffic.
AWS supports three types of load balancers:

  1. Application Load Balancer (ALB): Works at Layer 7 (HTTP/HTTPS) and is ideal for web applications.
  2. Network Load Balancer (NLB): Operates at Layer 4 (TCP/UDP) for ultra-low latency.
  3. Gateway Load Balancer (GWLB): Works at Layer 3 (IP) for distributing traffic to virtual appliances.

1. Target Groups

  • Target Groups are collections of targets (e.g., EC2 instances, IPs) that receive traffic from a load balancer.
  • You can define health checks for targets to ensure traffic is routed only to healthy instances. It can Organize and monitor targets (EC2 instances).

2. Security Groups

  • Security Groups act as virtual firewalls for your instances and load balancers.
  • For the load balancer, inbound rules allow traffic on ports like 80 (HTTP) or 443 (HTTPS).
  • For the instances, inbound rules allow traffic only from the load balancer's IP or security group.
  • It Protect resources by restricting traffic based on rules.

3. Subnets

  • Subnets are segments of a VPC that isolate resources.
  • Load balancers require at least two public subnets in different availability zones for high availability.
  • EC2 instances are usually deployed in private subnets, accessible only through the load balancer.
  • It isolate resources; public subnets for load balancers and private subnets for instances.

Steps to Set Up a Load Balancer for Multiple Instances

Step 1: Launch EC2 Instances

  1. Create Two or More EC2 Instances:
    • Use the AWS Management Console to launch multiple EC2 instances in a VPC.
    • Place them in private subnets across two different availability zones.
  2. Configure Security Groups for Instances:
    • Allow traffic only from the load balancer's security group on port 80 (HTTP) or 443 (HTTPS).

Step 2: Create a Target Group

  1. Navigate to Target Groups in the EC2 section of the console.
  2. Click Create Target Group and choose Instances as the target type.
  3. Provide the following configurations:
    • Protocol: HTTP or HTTPS
    • VPC: Select the same VPC as the EC2 instances.
    • Health Check Settings: Configure health checks (e.g., Path: / and Port: 80).
  4. Register the EC2 instances as targets in this group.

Step 3: Set Up a Load Balancer
Application Load Balancer Configuration:

  1. Go to the Load Balancers section of the EC2 console.
  2. Click Create Load Balancer and choose Application Load Balancer.
  3. Configure the following:
    • Name: Provide a unique name for the load balancer.
    • Scheme: Select Internet-facing for public access.
    • Listeners: Use port 80 or 443 (for HTTPS).
    • Availability Zones: Select public subnets from at least two availability zones.

Step 4: Attach Target Group to the Load Balancer

  1. In the Listener and Rules section, forward traffic to the target group created earlier.
  2. Save and create the load balancer.

Step 5: Update Security Groups

  1. For the Load Balancer:
    • Allow inbound traffic on port 80 or 443 (if HTTPS).
    • Allow inbound traffic from all IPs (or restrict by source).
  2. For EC2 Instances:
    • Allow inbound traffic from the load balancer's security group.

Step 6: Test the Setup

  1. Get the DNS name of the load balancer from the AWS console.
  2. Access the DNS name in your browser to verify traffic is being distributed to your instances.

Step:7 Scaling with Auto Scaling Groups
Attach an Auto Scaling Group (ASG) to the target group for dynamic scaling based on traffic demand.

To access multiple EC2 instances via a load balancer in AWS, you first deploy your EC2 instances within a Virtual Private Cloud (VPC), ensuring they are in the same target network. Install and configure your desired application (e.g., a web server like Apache) on these instances. Then, create an Application Load Balancer (ALB) or Network Load Balancer (NLB) to distribute traffic. Associate the load balancer with a Target Group that includes your EC2 instances and their ports. Next, configure the load balancer's listener to route incoming traffic (e.g., HTTP or HTTPS) to the Target Group. To make the setup accessible via a domain name, map your load balancer's DNS to a custom domain using Route 53. This ensures users can access your application by visiting the domain, with the load balancer evenly distributing traffic among the EC2 instances for high availability and scalability.

My output:

Image description

Image description

Top comments (0)