Skip to content

Harshit10j2004/autoscaling

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Overview

This Bash script provides a custom EC2 auto-scaling solution based on real-time CPU and network usage of a server. It continuously monitors system load and automatically launches or terminates EC2 instances behind an Application Load Balancer (ALB) target group using the AWS CLI.

When CPU and network usage cross the configured upper thresholds, the script calculates how many new instances are required, launches them, waits for them to become healthy, and registers them with the target group. When usage drops below the lower thresholds, it safely deregisters and terminates instances that were previously created.

To ensure stable operation, the script uses a file-based lock to prevent multiple executions at the same time, logs all actions to a dedicated log file, and keeps track of created instance IDs locally so scale-down actions are predictable and controlled.

Features:

  • Automatic scale-up based on CPU and network thresholds

  • Automatic scale-down when system usage is low

  • Health verification before attaching instances to the target group

  • Safe instance tracking using a local instance ID file

  • Lock-based execution to avoid concurrent runs

  • Detailed logging for auditing and debugging

Prerequisites:

  • Ubuntu-based system

  • AWS CLI installed and configured

  • IAM user or role with EC2 and ELB permissions

  • Application Load Balancer and target group already created

  • Required Tools (Ubuntu)

Install the required dependencies using:

sudo apt update
sudo apt install -y awscli sysstat ifstat util-linux jq

Tool usage:

  • awscli – Interact with EC2 and ELB services

  • mpstat (sysstat) – CPU usage monitoring

  • ifstat – Network throughput monitoring

  • flock (util-linux) – Prevent parallel script execution

  • AWS Configuration

Configure AWS credentials before running the script:

aws configure

Environment Setup:

The script loads configuration values from:

mkdir /home/ubuntu/data
cd /home/ubuntu/data
vi data.env

/home/ubuntu/data/data.env

Logging & Execution:

  • Logs are written to: /home/ubuntu/logs/as_log.txt

    mkdir /home/ubuntu/logs
    cd /home/ubuntu/logs
    touch as_log.txt
    
  • Instance IDs are tracked in: /home/ubuntu/data/instance_id.txt

    mkdir /home/ubuntu/data
    cd /home/ubuntu/data
    vi instance_id.txt
    
  • The script is intended to run periodically using cron.

    crontab -e
    * * * * * /bin/bash /home/ubuntu/scripts/autoscaling.sh >> /home/ubuntu/logs/cron_autoscaling.log 2>&1
    chmod +x /home/ubuntu/scripts/autoscaling.sh
    

About

My own autoscaling with shell and aws cli

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages