README
π AWS Auto Scaling Tutorial: Launch Template to Auto-Healing EC2 Instancesβ
AWS Auto Scaling is a game-changer for managing EC2 instances. It automatically adjusts the number of running instances in response to traffic changes β improving availability while optimizing cost.
In this tutorial, weβll walk you through setting up Auto Scaling using EC2, AMIs, Launch Templates, and CloudWatch. Weβll also simulate traffic to trigger scaling events and monitor the process in action.
Letβs get started! π‘
β Step 1: Launch an EC2 Instanceβ
- Go to the AWS EC2 Console.
- Click Launch Instance.
- Choose an Amazon Machine Image (AMI), e.g., Amazon Linux 2.
- Select an instance type like t2.micro (Free Tier eligible).
- Configure storage and network settings as needed.
- Add a Security Group (e.g., allow SSH and HTTP).
- Scroll to Advanced Details in "Configure Instance".
- Find the User Data field.
- Paste the script:
#!/bin/bash
yum install httpd git -y
service httpd start
chkconfig httpd on
cd /var/www/html
rm -rf /var/www/html/*
git clone https://github.com/sandeepallakonda/Analog-Digital-Clock.git
cp -r Analog-Digital-Clock/* .
rm -rf Analog-Digital-Clock
service httpd restart - Launch the instance using a key pair.
π’ Result: Apache is installed, started, and configured to serve content from the GitHub repository.
πΈ Step 2: Create an AMI from Your EC2 Instanceβ
- Connect to the instance via SSH.
- Install your app or any required packages.
- (Optional) Install
stress
to simulate load:sudo yum install stress -y
- From the EC2 dashboard:
Instance β Actions β Image and templates β Create image - Name and create the image.
π¦ This AMI captures your instance configuration for reuse.
π§° Step 3: Create a Launch Template from the AMIβ
- Navigate to Launch Templates in EC2.
- Click Create launch template.
- Provide a name and description.
- Select the AMI you created earlier.
- Choose instance type (e.g., t2.micro).
- Define key pair, network settings, and security group.
- Click Create launch template.
π This template is used by Auto Scaling to launch new instances.
π‘ Step 4: Set Up CloudWatch Alarms and SNS Notificationsβ
- Go to Amazon SNS and create a topic.
- Subscribe your email to receive alerts.
- Confirm the email subscription.
- Go to CloudWatch β Alarms β Create Alarm.
- Choose the EC2 metric (e.g., CPUUtilization).
- Set threshold (e.g., CPU > 40% for 2 minutes).
- Set action to notify your SNS topic.
π¬ Youβll receive alerts when conditions are met to trigger scaling.
βοΈ Step 5: Create an Auto Scaling Groupβ
- Navigate to Auto Scaling Groups.
- Click Create Auto Scaling group.
- Choose the Launch Template you created.
- Define group size (e.g., min 1, desired 1, max 3).
- Set your VPC and Subnets.
- Configure scaling policies (based on CloudWatch alarms).
- Enable health checks (EC2 and ELB if applicable).
- Click Create Auto Scaling group.
π‘οΈ AWS now automatically manages instance count based on performance.
π₯ Step 6: Simulate Load and Trigger Auto Scalingβ
- SSH into your EC2 instance.
- Run the following to simulate CPU stress:
sudo apt update
sudo apt install stress -y
stress --cpu 2 --timeout 300
This stresses 2 CPU cores for 5 minutes, increasing utilization and triggering CloudWatch alarms.
π Watch Auto Scaling launch new instances!
π Step 7: Monitor with CloudWatch and SNSβ
- Open CloudWatch to monitor metrics and alarms.
- Check email for SNS notifications.
- Confirm new instances in your Auto Scaling Group via the EC2 dashboard.
ποΈ Monitoring ensures your scaling rules are working correctly.
π» Step 8: Observe Auto Scaling Downβ
- After the CPU load drops, CloudWatch resets the alarm.
- Auto Scaling terminates extra instances, maintaining desired count.
πΈ You're only billed for what you use β efficiency at its best!
π§ Conclusionβ
Youβve now set up a complete AWS Auto Scaling environment from scratch! With Launch Templates, AMIs, CloudWatch, and SNS, your infrastructure is now self-healing and responsive to demand.
π§ Whether youβre deploying a production app or experimenting with AWS, this is a crucial step toward mastering modern cloud-native architecture.