Create a robust server using Auto Scaling groups and Load Balancers- AWS Hands On
Tags: aws, tutorial, cloud, beginners
Published: 2021-06-30 06:36:07

Image taken from [stackoverflow.com](https://stackoverflow.com/questions/13956653/aws-ami-multiple-instances)

Now we have all the tools to create a robust server. In this exercise we will create a server that can auto scale, be load balanced and can handle instance failures according to requirements.

* To do this create a load balancer as we did in last [hands on](https://dev.to/this-is-learning/setup-load-balancer-on-aws-elb-hands-on-1f9p) but don't add any instance to it or if you are using the previous exercise load balancer terminate those instances


* Go to Auto Scaling Groups under auto scaling in your EC2 instance dashboard. Click on Create Auto Scaling Group.

![auto-scaling-1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4ryubs9sk11e5xhs0prc.png)

* Fill the auto scaling group name and select a launch template of you have one if you don't have one select create new launch template. This launch template is a template which will be used to create our new EC2 instances for auto scaling.
![auto-scaling-2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qm3qk5wljrfaay7toew2.png)

  * Select a name and description for our template
    ![auto-scaling-3](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nlxyszjld05nso4md8dg.png)



  * Select the AMI for which you want this template. if you don't know select Amazon Linux 2 AMI (HVM), SSD Volume Type for the purpose of this tutorial this should be the first option

 * Select the instance type as t2.micro (free tier eligible)
   ![auto-scaling-5](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kfde9sr25ls843o5x035.png)


 * A key pair name for ssh login. You can select one we created while creating an ec2 instance.

 * Let network settings be VPC lets not get into what this is for the purpose of this tutorial

 * Select a security group. Select the same one that we select while creating our EC2 instances

 * Then there is storage, tags, network interfaces no need to touch them.
![auto-scaling-4](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bsyfhyg4ykhw874bi6yj.png)

 * Open Additional details and in user data paste the following script and click on create new launch template

  ```bash
#!/bin/bash
# Use this for your user data (script from top to bottom)
# install httpd (Linux 2 version)
yum update -y
yum install -y httpd
systemctl start httpd
systemctl enable httpd
echo "<h1>Hello World from $(hostname -f)</h1>" > /var/www   /html/index.html

  ```
![auto-scaling-6](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k0r480d7nlzxf9ep2npr.png)
![auto-scaling-7](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cxv4ievrf90nydl7ec01.png)

* Go to your old tab and click refresh on right side of launch template dropdown. You will see your launch template there select your template in my case its `demo-template`.
![auto-scaling-8](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9rp6xdgqaxrvesjda1wz.png)

* Now you will see configure settings screen which you can select that all your instances can adhere to launch template or you can mix and match them with spot or other instances I think that will be complex for now. So lets adhere to launch template
![auto-scaling-9](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s3e7p4d0h3s6i5nm90c8.png)

* Let the VPC remain same. Select all the subnets in your region. So that we can use multiple availability zones in our region. Click next
![auto-scaling-10](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iq4hfeeh464o491u7gbu.png)

* Next is load balancing. Select `Attach to an existing load balancer`. Select a target group you created while creating the load balancer. Select ELB health checks it checks if the underlying structure is working correctly. If you want to enable cloud watch logs. Select additional settings monitoring else click next. I don't need cloudwatch logs so I left it unchecked.
![auto-scaling-11](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qo0gorvaqb3qrpdq0dvp.png)


* Next it is the most important part we select our min, preferred and max instances.
![auto-scaling-12](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yc5yjo66hl01wevr9v84.png)


* Next we select the auto scaling policy as our needs for CPU utilization of each instance, requests etc. Configure it according to needs. Click on next.
![auto-scaling-13](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5zahj88vz2aqw98jem6l.png)


* Next we can add notifications on scale in and out but we will study about it later. For now remove it and click next.
![auto-scaling-14](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b29qnwwb5vfr02ruad0h.png)


* You can add tags to better identify our Auto scaling Group and instances associated with it.
![auto-scaling-15](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n197dtvkf49by7d2823e.png)

* Click next review that everything is set correctly and click `Create Auto Scaling Group`
![auto-scaling-16](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9krnnx447agv7m9jvof0.png)

* Your auto scaling group is created.
![auto-scaling-17](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zhtilh97hspu4sh261bs.png)

* To check it go to instances. You will see two instances with the name that we added in tags.
![auto-scaaling-18](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p0fabn6lu38ahk17lbr3.png)

* Next go to the load balancer DNS to see if the instances work.
![auto-scaling-20](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7dt44h5o5s8v6qaynguq.png)![auto-scaling-19](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xqh9xql0mfe3qwileuyq.png)

* Lets see what happens if we terminate on of our instances.
![auto-scaling-21](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8uza4fjjxj2o2xy95v7t.png)


* Go to auto scaling again and in a few seconds once you select instance management. We will see that it shows one healthy instance.
![UTO-SCALING-22](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c5a91dw68rws697w5gxj.png)


* Wait for a few minutes AWS will detect the unhealthy instance and launch a new instance in its place.
![auto-scaling-23](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oyuwv0nej1tu3acqrjbj.png)

* You can also check what happens in the activity tab. You will see it launches two instances first and then detects that there is a terminated instance and launches a new instance
![auto-scaling-24](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gtgao1mv0mzqcwcz3wx6.png)

Now to deploy an actual application you just have to expose the API over port 80 in deployment for HTTP or over port 443 for HTTPS in your instance and you have to setup SSL. Suggest a small opensource application if you want to see it deployed other wise I'm working on a small python Django project it is a work in progress we will deploy than in 20-30 days until that we have to wait. Until that this is as far as we go. 

In the next tutorial we will see how to setup EC2 instances AMI so that we have pre configured EC2 instances on our disposal.