DEV Community

Omkar Sharma
Omkar Sharma

Posted on

2 1 1 1 1

Master AWS Load Balancing: Launch & Load-Balance 3 EC2 NGINX Servers

✅ Step 1: Launch EC2 Instances Using a Bash Script

  1. Go to the AWS EC2 Console
  2. Click on Launch Instance
  3. Fill in the details:
    • Name: server-1
    • Amazon Machine Image (AMI): Select Ubuntu Server 22.04 LTS (HVM), SSD Volume Type
    • Instance type: t2.micro (Free Tier eligible)
    • Key pair: Create new or select existing
    • Network Settings:
      • Type: HTTP | Protocol: TCP | Port: 80 | Source: Anywhere (0.0.0.0/0)
      • Type: SSH | Protocol: TCP | Port: 22 | Source: My IP

omkar sharma

Add User Data Script

In the Advanced Details section, paste the following user-data script:

#!/bin/bash
apt-get update
apt-get install nginx -y
echo "<h1>Welcome to Server $(hostname)</h1>" > /var/www/html/index.html
Enter fullscreen mode Exit fullscreen mode

omkar sharma

Similarly create three servers: server-1, server-2 and server-3 respectively

omkar sharma

✅ Step 2: Create a Target Group and Register EC2 Instances

Once your EC2 instances are launched and NGINX is running on them (port 80), the next step is to create a Target Group and register your instances. This Target Group will later be linked with a Load Balancer.


Navigate to Target Groups

  1. Go to the AWS Management Console
  2. Open the EC2 Dashboard
  3. From the left-hand sidebar, click on Target Groups under Load Balancing
  4. Click Create target group

omkar sharma

Configure Target Group

Fill out the target group creation form as follows:

  • Target type: Instances
  • Target group name: TargetGroup-1 (or any name you prefer)
  • Protocol: HTTP
  • Port: 80
  • VPC: Select the VPC where your EC2 instances are running
  • Health checks:
    • Protocol: HTTP
    • Path: /

Leave other settings as default unless you have specific health check requirements.

Click Next.

Register Targets (EC2 Instances)

On the Register targets page:

  1. You’ll see a list of running EC2 instances in the selected VPC.
  2. Select the checkboxes for the 3 NGINX EC2 instances you created.
  3. Under Ports for the selected instances, make sure the port is set to 80.
  4. Click Include as pending below.
  5. Scroll down and click Create target group.

omkar sharma

omkar sharma

omkar sharma

Verify Registration

After creation:

  • Go back to Target Groups
  • Select your newly created target group
  • Click on the Targets tab
  • You should see the EC2 instances with a healthy status after a few seconds (if health checks pass)

omkar sharma

✅ Step 3: Creating an Application Load Balancer and Associating It with a Target Group

After launching EC2 instances and registering them in a Target Group, the next step is to set up a Load Balancer to distribute traffic among them.

Navigate to Load Balancers

  1. Go to the AWS Management Console
  2. Open the EC2 Dashboard
  3. From the left-hand menu, click Load Balancers
  4. Click Create Load Balancer

Select Load Balancer Type

Choose Application Load Balancer (ALB):

  • Suitable for HTTP/HTTPS traffic
  • Operates at Layer 7 (Application Layer)

Click Create under Application Load Balancer.

omkar sharma

Configure Basic Settings

Fill in the required fields:

  • Name: ALB-Testing
  • Scheme: Internet-facing
  • IP address type: IPv4
  • Listener: HTTP on port 80

omkar sharma

Click Next: Configure Security Settings

omkar sharma

Register Target Group

In this step, you’ll associate the previously created Target Group with the Load Balancer.

  • Target group name: Select your existing target group (e.g., TargetGroup-1)
  • Protocol: HTTP
  • Port: 80

Click Next: Register Targets

omkar sharma

Review and Create

  1. Review all the configurations
  2. Click Create Load Balancer

After a few moments, your load balancer will be provisioned and active.

Access Your Load Balancer

  1. Go to EC2 → Load Balancers
  2. Copy the DNS name of your ALB (e.g., my-alb-123456789.us-east-1.elb.amazonaws.com)
  3. Paste it in your browser:
  • Note: You cannot access the instance through the ALB DNS name why? because the SG of ALB only allows to communicate within itself to one thing you can do you can add below rule to ALB SG this will work.
Type Protocol Port Source
HTTP TCP 80 0.0.0.0/0
  • Exposing ALB to Internet is ok, but our instance also exposed to internet and hackers can directly reach the instance by the public ip, so we need to update instances SG and allow trusted traffic through ALB.
Type Port Source
HTTP 80 ALB's Security Group ID ✅ (Only allow traffic from ALB)

omkar sharma

omkar sharma

omkar sharma

Hurray!! We are getting response from all three servers.

Building Security into Each Phase of the Mobile App Lifecycle

Building Security into Each Phase of the Mobile App Lifecycle

A mobile app security breach can not only result in loss of data but also loss of consumer trust. To prevent breaches, DevOps teams are starting to adopt a security-focused mindset, building it into the SDLC earlier than before.

Read more

Top comments (4)

Collapse
 
nevodavid profile image
Nevo David

Growth like this is always nice to see. Kinda makes me wonder what keeps stuff going long-term - habits or just showing up?

Collapse
 
omkarsharma2821 profile image
Omkar Sharma • Edited

Totally! Showing up build's habits, and habits keep things moving.

Collapse
 
nathan_tarbert profile image
Nathan Tarbert

Pretty cool, I’ve enjoyed all the research you’ve put into this project it adds up

Collapse
 
omkarsharma2821 profile image
Omkar Sharma

Thanks Nathan 😊

Heroku

Build AI apps faster with Heroku.

Heroku makes it easy to build with AI, without the complexity of managing your own AI services. Access leading AI models and build faster with Managed Inference and Agents, and extend your AI with MCP.

Get Started

👋 Kindness is contagious

Discover fresh viewpoints in this insightful post, supported by our vibrant DEV Community. Every developer’s experience matters—add your thoughts and help us grow together.

A simple “thank you” can uplift the author and spark new discussions—leave yours below!

On DEV, knowledge-sharing connects us and drives innovation. Found this useful? A quick note of appreciation makes a real impact.

Okay