We will create Autoscaling Group configuration with SNS Route53 ALB Target Groups Alarms
2 Type of AutoScalling
—> Vertical : Basically vertical autoscalling, increase the capacity of instance by increaseing the size of server. I doning so , it firstly turnoff the instance recreates a new instance of more capacity and then again run the load. It creates Downtime.
—> Horizontal : Horizontal does the same thing , but it does it differently. It increases the capacity by creating new instance of same size and then distributes the workload on it.
Horizontal Atuoscalling is also know as HPA in Kubernetes.
Steps ::
1 ) Create Launch Templates ::
Go to Launch Templates —> Give name —> Template Version ( Optionla) —> AMI = My AMI ( Below is the reference for AMI) —> Instance type = t2.micro —> Key pair —> Add user data (mentioned in plain text)
We will not give and Security Groups or subnets.
#/bin/bash
sudo apt update
ansible-playbook /myrepo/playbook.yaml
Click on Launch Template.
AMI Creation :: This is already done on AMI with Hashicorp Packer. Below are the 2 files we created have kept in github, check below URL
https://github.com/ApurvDevops/packer_autoscalling.git
And follow below commands to create ami
packer validate --var-file packer-vars.json packer.json
packer inspect --var-file packer-vars.json packer.json
packer build --var-file packer-vars.json packer.json
📌 Make sure the Subnet you are using while creating a EC2 instance should have Auto Assign public IP enables
2) Create Target Group ::
Before this create a VPC and , enable Auto assign the Public Ip
Instance —> Target Group Name —> HTTP —> VPC = Your newly created VPC with Auto Assign public IP enabled on it (As Mentioned in Above) —> Next —> Dont Select anything —> create target Group
3) Create Load Balancer :
Create ALB —> Name —> Internet Facing —> VPC, that you created ( Select all public Subnet ) —> Security Groups —> Listner & routing ( HTTP & HTTPS ) Select the TG you created —> Select the certificate you created in old demos. —> create Load balancer.
4 ) Updating in DNS
GO to CloudFlare —> Go to Domain and update the DNS records
GIve domain name and ASG LB DNS name
5) Create SNS (Simple Notification Service)
AWS SNS —> Create Topic —> Standard —> Give some name —> create Topic
After topic is created —> create subscription —> Email —> email id , where alert wil be received,
open you email and Approve your subscription.
Go back to SNS and Refresh —> you will see its confirmed
6 ) Autoscalling group
Create ASG —> Give some name —> Give Launch Template that we created —> Select the VPC you created —> Select only Public Zones —> Attach an existing Load Balancer you created in previous steps) —> No VPC services —> Health Check ✅ Turn on Elastic Load balacing health check —> 90 second —> Next —> Desired Capacity =1 —> scaling Min =1 and Max =4 —> No Scaling Policies —> Next —> Add notification ( Already created SNS, select that) —> Next —> Create Scaling Group
7) Cloud Watch Alarm
Go to cloud Watch Alarm —> Alarms —> select metrics —> EC2 —> By Auto Scaling group —> CPU Utilization —> Select Metrics —>
Next
Notification —> In Alarm —> Select Exisitng SNS topic —> Next —> >40 —> Next Create Alarm
Similary We need to create Alarm for <35
Notification —> In Alarm —> Select Exisitng SNS topic —> Next —> <35 —> Next Create Alarm
7 ) Dynamic scalling
Go to Auto Scalling Groups —> Automatic scaling —> Create Dynamic Scaling —> Policy Type —> Step Scaling —> HIhgt > 40 (Scaling policy Name) —> Cloudwatch Alarm ( > 40) select —> Take action “ADD“ , Below is what we need to ADD and then Create
8 ) Create Another Policy
Go to Auto Scalling Groups —> Automatic scaling —> Create Dynamic Scaling —> Policy Type —> Simple Scaling —> Cloudwatch Alarm ( <35) select —> Take action “REMOVE“ , Below is what we need to REMOVE and then Create
10 )Now got and connec to Ec2 instance and give the Stress to that machine , it will create Instances and then send an email as well
stress --cpu 8 --io 4 --vm 2 --vm-bytes 128 --timeout 10s
Thanks for Reading it till here, Please implement it as well !!!