DEV Community

Cover image for AWS Auto Scaling with Load Balancer
Kannan
Kannan

Posted on

AWS Auto Scaling with Load Balancer

  • Create a instance with Ubuntu OS and with instance type as t2.nano and selected the existing key pair and security group and set 2 instance to create and launch.

  • Run the apdt update and install apache webserver and modify the index.html
    sudo apt update
    sudo apt install apache2 -y
    sudo rm /var/www/html/index.html
    sudo vim /var/www/html/index.html
    <h1>Auto Scaling Group with Load Balancer</h1>

  • Go to Action >Image and Template > Create Image

Image description

  • Create an image template by enabling noboot option.

Image description

Now create Target group and Load Balancer

  • Target Groups was created as "demo-tg"

Image description

  • Load balancer also created as "demo-lb"

Image description

  • Launch template was created with the AMI Image.

Image description

  • Create Auto scaling Group >Select the template >Select all network availability zone >Select Load balancing.

  • On Load balancing setup select the "Attach to an existing Load Balancer"

  • Group size >Desired capacity as 2 >Traget tracking scaling policies,Set Execute policy with CPU utilisation exceeds 50.

  • Select instance maintance policy as "Launch before Terminating"

  • Add Notification >Create a topic and provide name of the topic and email ID to send notification > Unselect "Fail to launch & Terminate"

Image description

Image description

Image description

  • Auto Scaling group has been create and launch the Instance from the AMI Image Template. you can monitor on the Activity tab.

Image description

  • Verfiy the instance status

Image description

Image description

  • On Cloud watch > Alarms> verify the scaling target alarm in detail.

Image description

  • Login to the 2 instance created by Auto Scaling groups and execute these commands to see the CPU utilisation and execute the cmd to exceed the CPU utilisation. top yes > /dev/null &
ubuntu@ip-172-31-1-56:~$ yes > /dev/null &
[1] 918
ubuntu@ip-172-31-1-56:~$ top

top - 15:16:41 up 18 min,  1 user,  load average: 0.08, 0.02, 0.01
Tasks: 101 total,   2 running,  99 sleeping,   0 stopped,   0 zombie
%Cpu(s): 75.0 us, 25.0 sy,  0.0 ni,  0.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
MiB Mem :    446.5 total,     24.5 free,    148.0 used,    274.0 buff/cache
MiB Swap:      0.0 total,      0.0 free,      0.0 used.    269.6 avail Mem 

    PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND                                            
    918 ubuntu    20   0    5764   1920   1920 R  93.8   0.4   0:05.42 yes                           
Enter fullscreen mode Exit fullscreen mode
  • It will exceed the CPU utilisation and trigger the ASG with Load balancer and create another instance.

Image description

  • Cloud watch Alram Notification.

Image description

  • To kill the cmd which we have run to exceed the CPU utilisation.Run the cmd with kill "PID".
ubuntu@ip-172-31-1-56:~$ kill 918
ubuntu@ip-172-31-1-56:~$ top

top - 15:33:28 up 35 min,  2 users,  load average: 0.85, 0.94, 0.68
Tasks:  98 total,   1 running,  97 sleeping,   0 stopped,   0 zombie
%Cpu(s):  0.0 us,  0.0 sy,  0.0 ni,100.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
MiB Mem :    446.5 total,     17.4 free,    152.4 used,    276.6 buff/cache
MiB Swap:      0.0 total,      0.0 free,      0.0 used.    265.1 avail Mem 

    PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND                                            
      1 root      20   0  166200  11352   8280 S   0.0   2.5   0:04.58 systemd         
Enter fullscreen mode Exit fullscreen mode

Image of Timescale

Timescale – the developer's data platform for modern apps, built on PostgreSQL

Timescale Cloud is PostgreSQL optimized for speed, scale, and performance. Over 3 million IoT, AI, crypto, and dev tool apps are powered by Timescale. Try it free today! No credit card required.

Try free

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more