Introduction:
Auto Scaling help you manage the load on your applications by automatically adding or removing EC2 instances based on CPU usage or other metrics. This guide will walk us through setting up EC2 with a simple use case.
Auto Scaling Prerequisites
- AWS Account: You need an active AWS account.
- IAM Role: Create an IAM role with AmazonEC2FullAccess and attach it to the instances launched by the Auto Scaling Group.
- Stress Tool: Install the stress tool on the instances to simulate high CPU usage.
- For Amazon Linux:
sudo yum install stress -y
-
- For Ubuntu:
sudo apt install stress -y
ALSO READ:
- How to Launch an EC2 Instance: Create a Key Pair and Configure Security Group.
- How to install Linux Server Monitoring tool Nagios XI and NCPA
- Step-by-Step Git Workflow: Managing a Project with Git and GitHub 2024
Auto Scaling Step-by-Step Guide
- Create a Launch Template
- Navigate to EC2 > Launch Templates.
- Click Create Launch Template.
- Enter a name like AutoScalingTemplate.
- Add a description such as “Template for Auto Scaling EC2 instances based on CPU usage.”
- Choose the instance type (e.g., t2.micro).
- Select the AMI (Amazon Linux or Ubuntu).
- Specify a key pair for SSH access:
- To use an existing key pair, select it from the list.
- To create a new key pair, go to EC2 > Key Pairs, click Create Key Pair, and download the private key.
- Specify the security group: Choose an existing one or create a new one allowing SSH and HTTP/HTTPS traffic.
- Attach the IAM role created earlier.
- Click on Create Launch template
-
Create an Auto Scaling Group
- Navigate to EC2 > Auto Scaling Groups.
- Click Create Auto Scaling Group.
- Step 1: Choose Launch Template:
- Select the launch template created earlier (AutoScalingTemplate).
- Specify the network, VPC, and availability zones.
- Example: vpc-0e052765c779304a4 (172.31.0.0/16)
- Availability Zones: us-east-1a, us-east-1b
- Step 3: Integrate with Other Services:
- Attach to a load balancer if needed.
- Enable EC2 health checks with a 10-second grace period.
- Configure Group Size and Scaling:
- Desired Capacity: 1 instance (or adjust as needed).
- Min desired capacity: 1.
- Max desired capacity: 2.
- Set scaling policy to target CPU utilization at 60%.
- Instance warmup: 30 seconds.
- Step 5: Add Notifications
- Configure notifications for EC2 Scaling events.
- Send notifications to an SNS topic (e.g., AutoScaling-Notifications).
- Add the email address to receive the notifications (e.g., krishna.devsecops@outlook.com).
- Select event types to notify on: Launch, Terminate, Fail to Launch, Fail to Terminate.
- Step 1: Choose Launch Template:
-
Review and Create Auto Scaling Group
- Verify all configurations and click Create Auto Scaling Group.
- Simulate CPU Load
- Wait for an instance to be launched by the Auto Scaling Group.
- SSH into the instance using its public IP address.
- Install the stress tool if not already installed:
sudo yum install stress -y
- Run the following command to simulate high CPU usage for 15 minutes:
stress --cpu 8 --timeout 900
-
Monitor Auto Scaling Activity
- Go to EC2 > Instances to observe new instances being launched.
- Wait for the cooldown period (30 seconds) after scaling operations.
- Monitor the scaling activity:
- The Auto Scaling Group will keep the additional instances running if CPU utilization remains high.
- If CPU utilization drops below 60%, the group will scale to the minimum capacity (1 instance).
Additional Information:
- Email Notifications: You’ll receive Email notifications about Scaling activities, helping you stay updated on instance launches or terminations.
Thank You