Integration of Auto scaling group, Elastic load balancer and Ec2:
Creating a high availability of our website, whenever load will come it will automatically Scale in and scale out the Ec2 instance.
Firstly, I'm going to create an Auto-Scaling group.
Step 1: Login to console and choose a Region
- Sign-in to your AWS Management console account and select the region where the EC2 instances will be launched.
- I am selecting Mumbai region.
Step 2: Navigate to EC2 Auto Scaling service
Step 3: Create Launch Template
- provide a name to the Auto-Scaling Group then click on
“Create a launch Template” to create a Launch Template for the Group.
when you will click on create a launch template then it will open in new tab.
In the Create Launch Template, provide the necessary details as per requirement.
I provided Launch Template Name and a description(optional) for the version of the Launch Template.
There are two more sections:
- Template Tags — It Allows to add tags to the launch template for ease of resource grouping and management.
- Source Template — You can choose to copy the settings from an existing launch template by mentioning a source template created earlier under this option.
I am leaving these two sections as it is.
Inside Launch Template Contents, provide the details as per requirement.
- Amazon Machine Image (AMI):
- Provide the AMI, which the launch template will provide to the ASG to launch EC2 with this AMI.
- In this demo, I am selecting Amazon Linux 2 as AMI.
2. Instance Type:
- Provide the type of the instance to be launched using this Launch Template.
3. Key Pair:
- Provide the key-pair to be used with the EC2 instance to facilitate login based on the AMI.
- Here, I am selecting “t2.micro” as instance type because it is free-tier eligible. and I already have a key-pair, so I am using that key-pair.
In Network setting:
- We need to mention the Security Group in the VPC to be associated to the Instances in the Auto-Scaling Group.
- Ensure appropriate Inbound and Outbound Rules are allowed in the Security Group based on the application requirement.
- For this demo, I am creating a security group that allows Inbound Rules → HTTP traffic on Port 80 and SSH traffic on Port 22 is allowed from allowed sources and Outbound Rules allow all Outgoing traffic to any destination.
In Storage:
- mention the details about the root EBS volume type, size and other
parameters to be used while launching EC2 Instances. - For this demo:
- Size →10GB.
- Volume Type →General Purpose SSD (gp2).
- Delete on termination →Yes.
If you want to add Additional EBS Volumes to be attached to the EC2 Instances, then you can add by clicking on “Add new
volume” options.
- I am not adding any resource tag, because it is optional.
In “Advanced Details” section:
- In this section additional settings related to EC2 Instance can be mentioned such as “Termination Protection”, “EC2 User Data”, etc.,
Configure the settings under this section based on requirements. - For this demo, I am modifying only “User Data”, to provide a user data to EC2 Instances, to configure the instances to be deployed with a Web server and a simple web page.
User Data:
#!/bin/bash
yum update -y
yum install httpd -y
service httpd start
cd /var/www/html
cat > index.html << EOF
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Integration of AWS services</title>
</head>
<body bgcolor="yellow">
<h1 style="text-align:center;">Welcome To Integration of services</h1>
<br>
<h2 style="text-align:center">This is a workload running on an EC2 Instance in AWS</h2>
<br>
<h3 style="text-align:center" >Hostname of the Server is hostname</h3>
<br>
</body>
</html>
EOF
sed -i "s/hostname/$(hostname)/g" index.html
cat > script.sh << EOF
#!/bin/bash
while true
do
echo "Do something; hit [CTRL+C] to stop!"
done
EOF
chmod +x script.sh
yum install at -y
systemctl enable --now atd.service
at now + 2 minute -f script.sh
After that, click on create launch template
You can see the template created:
Step 4: Create ASG from Launch Template
After creation of the Launch Template, I am going back to the Auto-Scaling Group creation page to mention the created Launch Template.
Step 4.1: Choose launch template
Click on Next to go the next step in the Auto-Scaling Group creation process.
Step 4.2: Choose instance launch options
For this demo, I am selecting the “default” VPC, and 2 subnets. ASG will ensure there is a balance of EC2 Instances across the selected subnets.
Click on “Next” to navigate to the next step.
Step 4.3: Configure advanced options
Now, I am creating this without any load balancer. after creating we will use this in Load balancer
click on next to navigate to next page.
Step 4.4: Configure group size and scaling policies
- To set the size of the Auto-Scaling Group, and the Scaling Policies to be associated with the Auto-Scaling Group.
- For this demo, the Group Size is set to the below values -
Desired Capacity: 2 (ASG Launches 2 EC2 Instances upon creation).
Minimum Capacity: 1 (ASG ensures, at-least 1 instance is always up and running in the group).
Maximum Capacity: 3 (the ASG can scale out to maximum 3 EC2 Instance in the Group.)
Under “Scaling Policies”, define the Policies using which the ASG can perform the scaling operations.
Target tracking scaling policy: This Policy scales out the number of instances in the group to the maximum size of the group set, based on the Average CPU utilization of the Instances in the Group.
For this demo, I am creating a Target tracking scaling policy.
- The Target Value is set to “50%”, so the ASG will scale out automatically if the utilization crosses this value.
- Warm Up time (initialization time when the EC2 Instance starts) is set to 120 seconds.
- Scale-in protection can be enabled, based on requirements. For now, I am not enabling.
Click on Next to navigate to the next step.
Step 4.5 Add notifications
you can choose to add SNS notifications for the activities performed by the Auto-Scaling Group. For this demo I am creating a notification (It is Amazon SNS service).
Click on Next to navigate to the Next step.
Next Step is “Add Tags”, you can choose to add Tags for resource grouping and management.
Last step: click on create Auto Scaling group
The Auto-Scaling Group has been created successfully.
- This will launch 2 EC2 Instances with the settings mentioned under the Launch Template used with the ASG.
In the EC2 Dashboard Page, you can see 2 Instances, Launched and Running successfully.
Step 5: Configure a Load balancer
Step 5.1. Go to Ec2 dashboard and click on load balancer then click on create load balancer
Step 5.2. select application load balancer:
I am selecting application load balancer. according to your requirements you can choose.
Step 5.3: Configuring the Load Balancer
we need to Provide Basic Configuration details for the Load Balancer like, name to your Load balancer.
Step 5.4: Network mapping:
I have selected all 3 mappings.
Step 5.5: Create Security groups:
I am Creating a new Security Group for the Load Balancer to allow HTTP traffic on Port 80 from anywhere.
Step 5.6: Select Listener:
I am selecting Listener Protocol to be HTTP and Port to be 80.
Click on “Create target group”. This will redirect you to a new Create target group page.
Create a Target Group to register the EC2 Instances under this Target Group.
Click on create target group.
First Step: Specify group details:
I am selecting instances because, I have already created a ASG, and we launched 2 instances by that.
click on next to go to next step.
Next Step: Register the targets
Add the instance we already launched using ASG.
click on create target group.
You can see target group created successfully.
Selecting the target group:
click on create load balancer:
You can see load balancer created successfully:
Thank you…