AWS: Highly available infrastructure using ASG and ELB
Building a Highly Available Environment on AWS using Auto Scaling Group and Load Balancer
1. Introduction
Highly available infrastructure ensures that applications remain accessible even if one or more servers fail. In AWS, this can be achieved by combining:
- EC2 Instances
- Auto Scaling Group (ASG)
- Elastic Load Balancer (ELB)
- Health Checks
This architecture automatically replaces unhealthy instances and distributes traffic across multiple servers.
---
2. Prerequisites
Before starting the setup, ensure the following components are available.
2.1 AWS Account
You must have access to the AWS Management Console.
2.2 VPC Setup
A Virtual Private Cloud with:
- At least two public subnets
- Subnets in different Availability Zones
Example:
VPC
├── Subnet-A (Availability Zone 1)
└── Subnet-B (Availability Zone 2)
2.3 Security Group
Allow the following ports:
HTTP (80)
HTTPS (443)
SSH (22)
---
3. Launch Base EC2 Instance
First create a base EC2 instance which will later be used to create an AMI.
Steps:
1. Go to EC2 Dashboard
2. Click Launch Instance
Example configuration:
Name: web-server
OS: Amazon Linux / Ubuntu
Instance type: t2.micro
Security group: allow HTTP and SSH
---
4. Install Application on EC2
Login to the EC2 instance.
Example: Install Nginx web server.
Amazon Linux
sudo yum install nginx -y
sudo systemctl enable nginx
sudo systemctl start nginx
Ubuntu
sudo apt update
sudo apt install nginx -y
sudo systemctl enable nginx
sudo systemctl start nginx
Verify:
http://EC2-PUBLIC-IP
You should see the default Nginx page.
---
5. Create AMI from EC2 Instance
Now create an Amazon Machine Image (AMI) from the configured instance.
Steps:
1. Select the EC2 instance
2. Click Actions
3. Image → Create Image
Example:
Image name: webserver-ami
This AMI will be used by the Auto Scaling Group to launch instances.
---
6. Create Target Group
A target group registers EC2 instances for the load balancer.
Steps:
1. Go to EC2 Console
2. Click Target Groups
3. Click Create Target Group
Configuration:
Target type: Instances
Protocol: HTTP
Port: 80
Health check path: /
Name:
web-target-group
---
7. Create Application Load Balancer (ALB)
Steps:
1. Go to EC2 Console
2. Click Load Balancers
3. Click Create Load Balancer
Select:
Application Load Balancer
Configuration:
Name: web-alb
Scheme: Internet-facing
Listeners: HTTP (80)
Select the VPC and both subnets.
Attach the target group created earlier.
---
8. Create Launch Template
Launch Template defines how new instances will be created.
Steps:
1. Go to EC2 Console
2. Click Launch Templates
3. Click Create Launch Template
Configuration:
Name: web-launch-template
AMI: webserver-ami
Instance type: t2.micro
Security group: web-security-group
Save the template.
---
9. Create Auto Scaling Group (ASG)
Steps:
1. Go to Auto Scaling Groups
2. Click Create Auto Scaling Group
Configuration:
Name: web-asg
Launch template: web-launch-template
Select VPC and both subnets.
---
Desired Capacity
Example:
Minimum instances: 2
Desired instances: 2
Maximum instances: 4
This ensures that at least two servers are always running.
---
10. Attach Load Balancer to ASG
While creating the Auto Scaling Group:
Select:
Attach to an existing load balancer
Choose:
web-alb
web-target-group
Now the load balancer distributes traffic to ASG instances.
---
11. Configure Health Checks
Health checks ensure that unhealthy instances are automatically replaced.
ELB Health Check
Protocol: HTTP
Path: /
Interval: 30 seconds
ASG Health Check
Enable:
ELB health check
If an instance fails the health check:
ASG terminates instance
ASG launches new instance
---
12. Enable Auto Scaling Policy
Auto Scaling adjusts the number of instances based on load.
Example policy:
Scale out:
If CPU > 70% add 1 instance
Scale in:
If CPU < 30% remove 1 instance
This keeps infrastructure cost-efficient while handling traffic spikes.
---
13. Test High Availability
Test the setup.
Step 1
Access the application using the Load Balancer DNS name.
Example:
http://web-alb-123456.ap-south-1.elb.amazonaws.com
Step 2
Terminate one EC2 instance manually.
Expected behavior:
ASG detects instance termination
ASG launches a new instance automatically
Load balancer routes traffic to healthy instances
This confirms high availability.
---
14. Architecture Diagram
Internet
│
Application Load Balancer
│
┌────────┴────────┐
│ │
EC2 Instance EC2 Instance
(AZ-1) (AZ-2)
│ │
Auto Scaling Group (ASG)
│
Launch Template + AMI
---
15. Benefits of this Architecture
High Availability
Traffic is distributed across multiple instances in different Availability Zones.
Fault Tolerance
If one instance fails, a new instance is automatically launched.
Auto Scaling
Infrastructure automatically adjusts based on demand.
Load Distribution
The load balancer ensures even traffic distribution.
---
16. Conclusion
Using Auto Scaling Groups with Application Load Balancers is a standard approach for building highly available and fault-tolerant applications on AWS. This architecture ensures application uptime, automatically replaces failed instances, and dynamically scales infrastructure to handle changing workloads.
Comments
Post a Comment