Create a Load Balancer for a single EC2 instance

Introduction

This project focuses on setting up an AWS Load Balancer for a single Amazon EC2 instance. By creating a Load Balancer, you prepare your environment for future scalability (even if you only have one instance right now).

Why is it useful?

  • Scalability: Easily add more EC2 instances behind the Load Balancer as your application grows.
  • High Availability: Even with one instance, you can quickly switch traffic to a replacement instance if the original fails (when you add more instances later).
  • Cost-Effective: AWS offers a Free Tier option that allows new users to run certain services at no charge, as long as you remain within the free-tier usage limits.

Prerequisites

  • AWS Account: Sign up for the AWS Free Tier (requires a valid payment method on file, but no charges if you stay within free-tier limits).
  • Installed AWS CLI (Command-Line Interface): For automation and scripting.
  • Owner or Administrator Permissions on the AWS account (or at least permissions to create EC2 instances, security groups, and load balancers).
  • Enabled Services:
    EC2 (for your instance).
    Elastic Load Balancing (for the load balancer).
  • Make sure you select regions where the Free Tier is applicable (e.g., us-east-1).

Step-by-Step Implementation

Create and Configure an EC2 Instance

We will create a single EC2 instance that falls under the free tier (e.g., t2.micro or t3.micro, depending on availability in your region).

Manual Steps (Graphical User Interface - GUI)

In the AWS Management Console, go to EC2.

Click Launch Instances.

Name your instance, for example: MyFreeTierInstance.

Choose Amazon Linux 2 AMI (Free Tier eligible).

Select t2.micro (or another free-tier eligible type).

Configure Key Pair: Either select an existing key pair or create a new one (to SSH into the instance later).

Network Settings:
Leave the default VPC selected.
Create or select an existing Security Group. Make sure inbound rules allow at least SSH (port 22) and HTTP (port 80) for testing.

Click Launch Instance.

CLI Equivalent:

Create a key pair (if you don't already have one) :

aws ec2 create-key-pair --key-name MyKeyPair --query "KeyMaterial" --output text > MyKeyPair.pem

chmod 400 MyKeyPair.pem

Create a security group

Add inbound rules (SSH on 22, HTTP on 80)

Launch the instance using the AMI for Amazon Linux 2 (replace 'ami-123abc45' with a valid AMI ID in your region)

Create a Target Group

A Target Group is where your EC2 instance(s) will be registered so the Load Balancer can route traffic to them.

Manual Steps (GUI)

Still in the EC2 dashboard, go to Target Groups under Load Balancing.

Click Create target group.

Choose a target type: select Instances.

Give it a name (e.g., MyTargetGroup).

Protocol: HTTP; Port: 80.

VPC: select the same VPC where your EC2 instance resides.

Click Next and select the newly launched instance to register.

Click Create target group.

CLI Equivalent:

(Replace <YourVPCID> with the VPC ID where your instance is running.)

Register your instance with the target group:

(Replace <YourTargetGroupARN> and <YourInstanceID> with the actual values returned from the previous steps.)

Create an Application Load Balancer

We’ll create an Application Load Balancer (ALB) that forwards HTTP traffic on port 80 to the target group.

Manual Steps (GUI)

Go to Load Balancers under Load Balancing in the EC2 dashboard.

Click Create Load Balancer.

Select Application Load Balancer.

Name your ALB, e.g. MyLoadBalancer.

Scheme: Internet-facing (so it’s publicly accessible).

IP address type: IPv4.

Network Mapping: Select the same VPC as your EC2 instance. Choose at least two Availability Zones for higher availability.

Listeners: By default, an HTTP listener on port 80 is created.

Security Groups: Select or create one that allows inbound HTTP traffic.

Default Action: Choose Forward to MyTargetGroup.

Click Create Load Balancer.

CLI Equivalent:

Create the load balancer

Create a listener that forwards traffic to the target group

(Replace <SubnetID1>, <SubnetID2>, <YourLoadBalancerARN>, <YourSecurityGroupID>, and <YourTargetGroupARN> with your values.)

Common Issues and Troubleshooting

  • Instance Health Check Failing
    Ensure the security group or firewall settings allow HTTP (port 80) traffic.
    Verify the health check path in the target group settings (default is /, but you may have changed it).
  • Load Balancer Stuck in ‘Provisioning’ State
    Check that you selected subnets in at least two Availability Zones.
    Confirm your IAM permissions or account limits are not restricting creation.
  • Connection Timeouts
    Make sure the instance is in the same VPC and subnets you selected during the Load Balancer setup.
    Verify the instance’s security group allows traffic from the Load Balancer’s security group.
  • No Response in Browser
    Double-check the DNS name of the Load Balancer. It can take a minute or two for DNS to propagate.
    Confirm that your EC2 instance has a running web server (Apache, Nginx, etc.) if you expect a web page.

Conclusion

We have successfully created a Load Balancer in AWS and attached a single EC2 instance to it, all while staying within free-tier eligibility. We configured security groups, set up a target group, and routed traffic through an Application Load Balancer. We also learned how to verify our setup via both the AWS Console and the AWS CLI. Through this project, we have gained insights into AWS networking, security, and load balancing fundamentals—preparing us to scale the application further in the future.

What is Cloud Computing ?

Cloud computing delivers computing resources (servers, storage, databases, networking, and software) over the internet, allowing businesses to scale and pay only for what they use, eliminating the need for physical infrastructure.


  • AWS: The most popular cloud platform, offering scalable compute, storage, AI/ML, and networking services.
  • Azure: A strong enterprise cloud with hybrid capabilities and deep Microsoft product integration.
  • Google Cloud (GCP): Known for data analytics, machine learning, and open-source support.