AWS Auto Scaling with Load Balancer
Tags: linux, devops, aws
Published: 2023-11-30 15:38:43

- 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9bmg9gxekgwq8uv1gnaq.png)
- Create an image template by enabling noboot option.
 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/50dpcvuyt2s2dhaxgs8i.png)

## Now create Target group and Load Balancer

- Target Groups was created as "demo-tg"

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nw2fh0yatbtiv9yql82a.png)

- Load balancer also created as "demo-lb"

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/226jlzzc79kvadf36g76.png)

- Launch template was created with the AMI Image.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9qeaxnr83auu2hps8hv7.png)

- 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/arhu73mc2rjlw9t2yrij.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9i7706f0h1wzeknud6zi.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4sb00kyubnv09j120q54.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oexicl8z1mnrw4zqun4r.png)

- Verfiy the instance status

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oxcnqddhtkbkjvxx6ldn.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f501ituf7mqrx5id7lgk.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/559pldtgwrvdfeuwxfq2.png)

- 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                           
```

- It will exceed the CPU utilisation and trigger the ASG with Load balancer and create another instance.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hc0646tlg36ho9raz9ue.png)

- Cloud watch Alram Notification.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bsa2d4iv3ufkdcfu5n8v.png)

- 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         
```