AWS Builder Center

Building a Highly Available Web Application Using AWS EC2 and Application Load Balancer

Learn how to deploy a highly available web application on AWS using EC2 instances, an Application Load Balancer, Auto Scaling, and security groups.

Building a Highly Available Web Application Using AWS EC2 and Application Load Balancer

Introduction

High availability is an important requirement for modern cloud applications. Applications should remain accessible even when an individual server becomes unavailable.
AWS provides several services that can be combined to build highly available applications. In this article, we will explore how Amazon EC2, Application Load Balancer, Auto Scaling, and Security Groups can be used together to create a highly available web application.

Architecture

The basic architecture consists of:
  • Amazon EC2
  • Application Load Balancer (ALB)
  • Auto Scaling Group (ASG)
  • Security Groups
  • Amazon VPC
The Application Load Balancer receives incoming HTTP/HTTPS requests and distributes them across healthy EC2 instances.

1. Create EC2 Instances

Amazon EC2 provides virtual servers that can host our web application.
We can create multiple EC2 instances instead of depending on a single server.
For example:
  • EC2 Instance 1
  • EC2 Instance 2
If one instance becomes unavailable, the other instance can continue serving requests.

2. Configure Security Groups

Security Groups act as virtual firewalls for EC2 instances and other AWS resources.
A common configuration is:

Load Balancer Security Group

Allow:
  • HTTP – Port 80
  • HTTPS – Port 443

EC2 Security Group

Allow HTTP traffic only from the Load Balancer Security Group.
This prevents users from directly accessing the EC2 instances.

3. Create a Target Group

The target group contains the EC2 instances that will receive traffic from the Application Load Balancer.
The target group also performs health checks.
For example:
1
2
3
4
5
6
7
Application Load Balancer
|
v
Target Group
/ \
v v
EC2-1 EC2-2

4. Create an Application Load Balancer

The Application Load Balancer provides a single entry point for users.
Instead of accessing individual EC2 instances, users access the load balancer.
1
2
3
4
5
6
7
Users
|
v
Application Load Balancer
/ \
v v
EC2-1 EC2-2
The ALB distributes incoming requests across healthy instances.

5. Configure Auto Scaling

An Auto Scaling Group can automatically maintain the required number of EC2 instances.
For example:
1
2
3
Minimum capacity: 2
Desired capacity: 2
Maximum capacity: 4
If demand increases, Auto Scaling can launch additional instances.
If an instance becomes unhealthy, Auto Scaling can replace it.

6. Health Checks

Health checks help determine whether an EC2 instance is working correctly.
For example, the load balancer can check:
1
HTTP:80/
If the application responds successfully, the instance is considered healthy.
If the health check fails, the instance is removed from load balancing.

7. Benefits of This Architecture

This architecture provides several benefits:

High Availability

Multiple EC2 instances reduce dependency on a single server.

Fault Tolerance

Failed instances can be replaced automatically.

Scalability

Auto Scaling can increase or decrease the number of instances based on demand.

Better Traffic Distribution

The Application Load Balancer distributes requests across healthy instances.

Improved Security

Security Groups can restrict direct access to the application servers.

Example Request Flow

When a user opens the application:
1
2
3
4
5
6
7
8
User
|
v
Application Load Balancer
|
+----> EC2 Instance 1
|
+----> EC2 Instance 2
The load balancer selects a healthy target and forwards the request.
If EC2 Instance 1 becomes unhealthy:
1
2
3
4
5
6
7
8
User
|
v
Application Load Balancer
|
X----> EC2 Instance 1
|
+----> EC2 Instance 2
The ALB sends traffic only to the healthy instance.
Auto Scaling can then launch a replacement instance.

Best Practices

When building a highly available AWS application:
  • Deploy EC2 instances across multiple Availability Zones.
  • Use an Application Load Balancer.
  • Configure health checks.
  • Use Auto Scaling Groups.
  • Restrict EC2 access using Security Groups.
  • Avoid exposing application servers directly to the internet.
  • Monitor resources using Amazon CloudWatch.
  • Use HTTPS for production applications.

Conclusion

Building a highly available web application on AWS does not require relying on a single server.
By combining EC2, Application Load Balancer, Auto Scaling, Security Groups, and multiple Availability Zones, we can create an architecture that is scalable, fault tolerant, and more reliable.
This architecture provides a strong foundation for deploying production web applications on AWS.
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