October 15, 2024

Creating a Load Balancer in AWS

Creating a Load Balancer in AWS involves several steps, depending on the type of load balancer you want to create. In this guide, I’ll walk you through the process of creating an Application Load Balancer (ALB), which is one of the most commonly used types in AWS.

Step-by-Step Guide to Creating an Application Load Balancer (ALB)

Step 1: Sign in to the AWS Management Console

Step 2: Navigate to the EC2 Dashboard

  • In the AWS Management Console, type “EC2” into the search bar and select “EC2” from the dropdown list.
  • This will take you to the EC2 Dashboard.

Step 3: Create a Load Balancer

  • In the EC2 Dashboard, scroll down to the “Load Balancing” section on the left-hand menu.
  • Click on “Load Balancers” and then click the “Create Load Balancer” button.

Step 4: Select the Load Balancer Type

  • AWS offers three types of load balancers: Application Load Balancer (ALB), Network Load Balancer (NLB), and Gateway Load Balancer (GWLB).
  • For this example, choose “Application Load Balancer” and click “Create” to proceed.

Step 5: Configure the Load Balancer

  • Name: Enter a name for your load balancer (e.g., “MyApplicationALB”).
  • Scheme: Choose whether you want an internet-facing load balancer (public) or an internal load balancer (private).
  • IP Address Type: Select “IPv4” or “Dualstack” if you need both IPv4 and IPv6 addresses.
  • Listeners: Add listeners for the protocols and ports you want your load balancer to listen on (e.g., HTTP port 80 or HTTPS port 443).

Step 6: Configure Availability Zones

  • VPC: Choose the Virtual Private Cloud (VPC) in which the load balancer will operate.
  • Availability Zones: Select the Availability Zones where your load balancer will distribute traffic. You should select at least two Availability Zones for redundancy.
  • Subnet: Choose a subnet in each selected Availability Zone. This determines where your load balancer will place its nodes.

Step 7: Configure Security Settings (Optional for HTTPS)

  • If you’re setting up an HTTPS listener, you’ll need to configure SSL/TLS settings:
    • Certificate: Choose an existing SSL certificate from AWS Certificate Manager (ACM) or upload a new one.
    • SSL Policy: Choose the SSL policy that dictates the cryptographic algorithms your load balancer uses.

Step 8: Configure Security Groups

  • Security Group: Security groups control the traffic allowed to reach your load balancer. You can create a new security group or select an existing one.
    • For example, if your load balancer is serving HTTP/HTTPS traffic, ensure that the security group allows inbound traffic on ports 80 and 443.

Step 9: Configure Routing

  • Target Group: Target groups define where the load balancer sends traffic. You can create a new target group or use an existing one.
    • Target Type: Choose between Instance, IP address, or Lambda function. For most use cases, you’ll choose “Instance” to route traffic to EC2 instances.
    • Protocol and Port: Specify the protocol and port that your targets (e.g., EC2 instances) will use to receive traffic.
    • Health Checks: Configure health checks to monitor the health of your targets. By default, HTTP is used, but you can customize the health check path and settings.

Step 10: Register Targets

  • Add Targets: Select the EC2 instances (or other targets) you want to include in the target group.
  • Add to Registered: Once selected, click “Add to registered” to finalize the registration of your targets.

Step 11: Review and Create

  • Review all the configurations you’ve made, including the load balancer settings, security groups, and target group settings.
  • If everything looks correct, click “Create Load Balancer” to launch the load balancer.

Step 12: View and Manage the Load Balancer

  • Once created, you can view the details of your load balancer in the EC2 Dashboard under the “Load Balancers” section.
  • Monitor: You can monitor the performance of your load balancer using Amazon CloudWatch. CloudWatch provides metrics such as request count, healthy/unhealthy host count, and latency.
  • Manage: You can manage the load balancer, including updating listeners, adding/removing targets, and modifying security groups through the AWS Management Console or AWS CLI.

Verifying the Load Balancer

  1. Accessing the Load Balancer:
    • Once the load balancer is active, you can access it via the DNS name provided by AWS (e.g., myapplicationalb-1234567890.us-west-2.elb.amazonaws.com).
    • Enter this DNS name in your browser to verify that the load balancer is routing traffic correctly to your targets.
  2. Testing Failover:
    • To test failover, you can stop one of the registered EC2 instances and observe how the load balancer routes traffic to the remaining healthy instances.
  3. Checking Logs and Metrics:
    • Use AWS CloudWatch Logs and metrics to check the health of your load balancer and targets. This can help you fine-tune your configurations for better performance and reliability.

Creating a Load Balancer via Putty Software

Create a Load Balancer via the command line using PuTTY (which is an SSH client), you would use the AWS Command Line Interface (CLI). Below is a step-by-step guide on how to create an Application Load Balancer (ALB) using AWS CLI commands. Before you begin, ensure that you have the AWS CLI installed and configured on your EC2 instance or local machine that you are accessing via PuTTY.

Prerequisites

  1. Install AWS CLI: Ensure that the AWS CLI is installed. If not, you can install it by running:

    bash

    sudo apt-get update
    sudo apt-get install awscli -y

    (For Ubuntu/Debian systems) or

    bash
    sudo yum install awscli -y
  • (For Amazon Linux/RHEL systems).
  • Configure AWS CLI: Configure the AWS CLI with your credentials and default region:
    bash

    aws configure

  1. You will need to provide your AWS Access Key ID, Secret Access Key, region, and output format (e.g., json).

Step-by-Step Command to Create an Application Load Balancer

Step 1: Create a Security Group

Create a security group that allows HTTP (port 80) and HTTPS (port 443) traffic:

bash
aws ec2 create-security-group --group-name my-alb-sg --description "Security group for ALB"

 

Get the security group ID from the output, then add inbound rules to allow HTTP and HTTPS traffic:

bash

aws ec2 authorize-security-group-ingress --group-id sg-xxxxxxxx --protocol tcp --port 80 --cidr 0.0.0.0/0

aws ec2 authorize-security-group-ingress --group-id sg-xxxxxxxx --protocol tcp --port 443 --cidr 0.0.0.0/0

Replace sg-xxxxxxxx with the actual security group ID returned from the previous command.

Step 2: Create a Target Group

Create a target group that specifies the instances to which your ALB will route traffic:

bash

aws elbv2 create-target-group --name my-target-group --protocol HTTP --port 80 --vpc-id vpc-xxxxxxxx --target-type instance

Replace vpc-xxxxxxxx with your VPC ID.

Step 3: Register Targets

Register EC2 instances to the target group:

bash

aws elbv2 register-targets --target-group-arn arn:aws:elasticloadbalancing:region:account-id:targetgroup/my-target-group/xxxxxxxx --targets Id=i-xxxxxxxx

Replace the target group ARN, region, account-id, and i-xxxxxxxx with your target group ARN, AWS region, account ID, and instance ID, respectively.

Step 4: Create an Application Load Balancer

Now create the Application Load Balancer:

bash

aws elbv2 create-load-balancer --name my-application-load-balancer --subnets subnet-xxxxxxxx subnet-yyyyyyyy --security-groups sg-xxxxxxxx --scheme internet-facing --type application

Replace subnet-xxxxxxxx and subnet-yyyyyyyy with your subnet IDs and sg-xxxxxxxx with the security group ID created earlier.

Step 5: Create a Listener

Create a listener that forwards traffic from the load balancer to your target group:

bash

aws elbv2 create-listener --load-balancer-arn arn:aws:elasticloadbalancing:region:account-id:loadbalancer/app/my-application-load-balancer/xxxxxxxx --protocol HTTP --port 80 --default-actions Type=forward,TargetGroupArn=arn:aws:elasticloadbalancing:region:account-id:targetgroup/my-target-group/xxxxxxxx

Replace the load-balancer-arn and target-group-arn with your actual load balancer and target group ARNs.

Verifying the Load Balancer

You can verify that your load balancer was created successfully and is working by describing the load balancer:

bash

aws elbv2 describe-load-balancers --names my-application-load-balancer

This command will provide details about the load balancer, including its DNS name, which you can use to test if it’s routing traffic correctly.

Finally,

You’ve now created an Application Load Balancer (ALB) using the AWS CLI through PuTTY. This process involves creating the necessary security group, target group, registering your EC2 instances, and setting up the load balancer with a listener to forward traffic. This method allows for automation and script-based deployments, which is essential for managing large or complex AWS environments.

Leave a Reply

Your email address will not be published. Required fields are marked *