AWS Builder Center
Build a Scalable Web App with AWS NLB & Auto Scaling

Build a Scalable Web App with AWS NLB & Auto Scaling

Learn how to build a highly available web application on AWS using a Network Load Balancer, EC2 Auto Scaling, Launch Templates, and Target Groups.

Introduction

Modern web applications must be able to handle increasing traffic while remaining available even if individual servers fail. Deploying an application on a single EC2 instance introduces a single point of failure, making it unsuitable for production workloads.
In this project, I built a highly available web application using AWS services that automatically distribute incoming traffic, replace unhealthy instances, and scale capacity based on demand. The solution combines a Network Load Balancer (NLB), Target Groups, Launch Templates, and EC2 Auto Scaling to create a resilient architecture across multiple Availability Zones.

Why use a Network Load Balancer?

AWS offers multiple load balancing options, each designed for different workloads.
A Network Load Balancer operates at Layer 4 (TCP), making it ideal for applications that require extremely low latency, high throughput, or support for non-HTTP protocols. Unlike an Application Load Balancer, which routes traffic based on HTTP requests, an NLB forwards TCP connections directly to healthy backend instances while preserving the client IP address.
For this project, the NLB ensures incoming traffic is evenly distributed across multiple EC2 instances while automatically avoiding unhealthy targets.

Architecture Overview

The architecture consists of five core AWS services working together:
  • Network Load Balancer receives incoming traffic.
  • Target Group monitors instance health.
  • Auto Scaling Group maintains the desired number of EC2 instances.
  • Launch Template provides a standardized EC2 configuration.
  • Amazon EC2 hosts the web application.
When traffic increases, the Auto Scaling Group launches additional EC2 instances using the Launch Template. These instances are automatically registered with the Target Group, allowing the Network Load Balancer to begin routing traffic without manual intervention.


Building the Solution

Security Groups

To follow security best practices, separate security groups were created for the Network Load Balancer and the EC2 instances.
The Network Load Balancer accepts inbound HTTP traffic from the internet, while the EC2 instances only accept HTTP traffic originating from the NLB security group. SSH access is restricted to my public IP for administration.
This approach prevents users from bypassing the load balancer and accessing backend instances directly.



Launch Template

The Launch Template defines the configuration that every EC2 instance launched by Auto Scaling will use.
It includes the Amazon Linux 2023 AMI, instance type, security group, key pair, and a user data script that automatically installs Apache and configures the web server during instance launch.
Using a Launch Template ensures consistency across all instances created by the Auto Scaling Group.


Target Group

The Target Group acts as the connection between the Network Load Balancer and the EC2 instances.
It continuously performs health checks on registered instances and routes traffic only to healthy targets. If an instance becomes unhealthy, it is automatically removed from traffic until it recovers or is replaced by Auto Scaling.


Network Load Balancer

The Network Load Balancer was configured as an internet-facing load balancer across two Availability Zones.
It listens for TCP traffic on port 80 and distributes incoming connections across healthy EC2 instances registered in the Target Group.
Because it operates at Layer 4, the NLB provides high performance and low latency while supporting millions of concurrent connections.


Auto Scaling Group

The Auto Scaling Group was configured with a minimum and desired capacity of two instances and a maximum capacity of four.
A target tracking scaling policy monitors average CPU utilization and automatically launches additional EC2 instances when demand increases. As traffic decreases, unnecessary instances are terminated, helping maintain both availability and efficient resource utilization.


Testing the Deployment

After deployment, both EC2 instances were successfully registered as healthy targets in the Target Group.
Opening the Network Load Balancer DNS name displayed the web application, including the instance ID, hostname, and Availability Zone. This confirmed that traffic was being routed correctly through the load balancer to healthy backend instances.



Key Takeaways

This project strengthened my understanding of several core AWS concepts, including:
  • Designing highly available architectures across multiple Availability Zones
  • Using Network Load Balancers for Layer 4 traffic distribution
  • Automating EC2 provisioning with Launch Templates
  • Maintaining application availability with Target Groups and health checks
  • Automatically scaling infrastructure based on CloudWatch metrics
  • Applying security best practices through layered security groups

Cost Considerations

The primary costs for this project come from Amazon EC2, Network Load Balancer (NLB), Amazon EBS, and any associated data transfer. While EC2 Auto Scaling has no additional service charge, any instances it launches are billed at standard EC2 pricing.

Conclusion

Building this project provided practical experience designing a scalable and highly available AWS architecture using native cloud services. While the application itself is simple, the underlying design reflects production patterns used to improve resilience, automate infrastructure management, and support changing traffic demands.
Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article