Introduction
In today’s digital landscape, website downtime can lead to revenue loss and poor customer experience. A company hosting its website on an AWS EC2 instance faced challenges when traffic spikes during promotions overloaded the server, causing downtime. The solution? "Auto Scaling" which automatically adjusts resources based on demand.
This guide walks you through setting up an AWS Auto Scaling solution that dynamically scales up during high traffic and scales down during low traffic, ensuring efficiency and cost optimization.
Project Goal
To configure an auto-scaling architecture that ensures the website remains available during traffic spikes whminimising costs when traffic is low.
Step 1: Set Up the AWS VPC and Subnets
- Create a VPC ( with IPv4 CIDR 10.0.0.0/16).
Inside the VPC, create 3 public subnets (for the Load Balancer) and 3 private subnets (for EC2 instances).
Create three private subnets (for EC2 instances).
- Create three public subnets (for the Load Balancer).
- Ensure subnets are spread across multiple Availability Zones for high availability.
Step 2: Launch an EC2 Instance with a User Data Script
- a. Choose Amazon Linux AMI.
- b. Launch an EC2 instance in a private subnet.
- EC2 Instance won't be accessible yet via EC2 Connect
- Create an internet gateway
- Attach IGW to your VPC
- Attach your VPC in use
- Edit route tables
- Create public & private route tables
- Associate all 3 public subnets to the public route table and all 3 private subnets to the private route table.
- Edit the route in the public route table to send traffic to the IGW created
- Connect to EC2 instance again via EC2 connect and it should connect now
- Use a User Data script to configure the web server:
!/bin/bash
sudo yum update -y
sudo yum install -y httpd
sudo systemctl start httpd
sudo systemctl enable httpd
echo "
Welcome to Auto-Scaling Web Server
" | sudo tee /var/www/html/index.html- Assign a Security Group that allows HTTP traffic only from the Load Balancer.
Step 3: Configure the Auto Scaling Group
- Create a Launch Template specifying the AMI, instance type, security group, and User Data script.
**2. Create an Auto Scaling Group: **
- Attach it to private subnets.
- Set minimum = 1, desired capacity = 2, and maximum = 5 instances.
Step 4: Set Up an Application Load Balancer (ALB)
Create an Application Load Balancer in the public subnets.
Create a Target Group for the EC2 instances.
Attach the Auto Scaling Group to the Target Group.
⚠️ Note: Due to the teardown of infrastructure after project completion, real-time screenshots for this step are unavailable. However, the configuration steps below accurately reflect what was implemented.
Configuration Summary:
Created an Application Load Balancer (ALB) in three public subnets (across different Availability Zones).
Configured the Listener to forward HTTP (port 80) traffic to the Target Group.
Created a Target Group (type: instance) and registered EC2 instances launched via the Auto Scaling Group.
Attached the Auto Scaling Group to the Target Group to distribute traffic evenly among healthy instances.
Step 5: Implement Auto Scaling Policies
Target Tracking Scaling
- Set a policy to maintain average CPU utilization at 30%.
- AWS automatically adjusts the number of instances as needed.
⚠️ Note: Auto Scaling configurations were tested and deleted afterward. The following summary describes the actual implementation.
✅** Option Used: Target Tracking Policy**
Configured an Auto Scaling Policy to maintain average CPU utilization at 30%.
AWS automatically scaled the number of EC2 instances up or down based on real-time load.
Cooldown periods and instance warm-up were set to defaults.
Step 6: Security Best Practices
Keep EC2 instances in private subnets.
Use a bastion host or VPN for secure access.
Assign IAM roles with the least privilege access.
Step 7: Cost Optimization
1. Elimination of Over-Provisioning
Before Auto Scaling: You may need to run more instances than necessary just to prepare for spikes.
With Auto Scaling: Only the exact number of resources needed at any moment are provisioned.
✅ Cost Benefit: You avoid paying for idle or underutilized instances during off-peak periods.
2. Reduced Human Intervention
- Auto Scaling automates instance provisioning and termination.
✅*** Cost Benefit***: Saves time and money on manual monitoring and scaling tasks.
3. Minimized Downtime Costs
- By handling traffic spikes automatically, Auto Scaling helps maintain performance during surges.
✅*** Cost Benefit:*** Reduces the risk of lost revenue from slow page loads or outages.
4.*** Optimized Resource Utilization***
- When demand drops, unnecessary instances are terminated.
✅*** Cost Benefit***: Avoids unnecessary billing by releasing unneeded resources.
5. Scalability without Financial Risk
- You can scale for unpredictable growth or seasonal traffic without a large upfront investment.
✅*** Cost Benefit:*** Supports business growth while keeping infrastructure costs flexible.
- Using CloudWatch ensures there are no cost surprises
Challenges and Fixes
-
Configuring the web server data script with ("
Welcome to Auto-Scaling Web Server
" > /var/www/html/index.html) didn't work, so i had to use (echo "Welcome to Auto-Scaling Web Server
" | sudo tee /var/www/html/index.html)
⚠️ Note: The later command gave me an elivated privilege because i was running the command from an IAM user that might not have enough privileges
- While on the project, i encountered challenges setting up the "Application Load Balancer" as a result, i couldn't take screenshots when it finally worked, so i did a summary in the place of the screenshots giving the fact that i had already clleaned up the VPC i used and terminated/deleted the resources.
Conclusion
Implementing Auto Scaling on AWS ensures your website remains available during high traffic while keeping costs minimal. This approach provides flexibility, security (by putting the web server in a private subnet rather than a public subnet), and cost savings, making it a smart choice for businesses handling fluctuating traffic.
By leveraging AWS Auto Scaling, your website is always responsive, even during unexpected traffic surges. Ready to scale your web applications efficiently? Try this setup today!
Have thoughts on this implementation? Share them in the comments! Follow for more AWS cloud tips
Top comments (0)