Installing AWS Web servers using an Auto Scaling Group with Application Load Balancer
Welcome my friends! Today I am going to create some web servers with the Auto Scaling Group (ASG) with an Application Load Balancer (ALB). So the system is scaled automatically and the incoming traffic is distributed on the web services. Thus the stability and availability of the system will be highly improved. From the above diagram, we will install the ASG and ALB inside a VPC and three public subnets, as well as an internet gateway. Also we need to create a route table for the router and some security group, which do not show in the diagram. This post is going to walk through the whole process, we can always go back to the diagram to understand it better. OK, let’s start!
Step 1 Create the VPC and the Subnets
First we need to create a VPC for our system like below.
The subnets are in different Available Zones. And their CIDRs must be within the one in the VPC.
After creating the subnets, check the “Enable auto-assign public IPv4 address” in the subnet settings to make the subnets public.
In the “Routes” tab add the internet gateway we just created.
In the “Subnet Associations” tab, add the three public subnets like below.
Step 2 Create an ASG for the three public subnets
The ASG will use the t2.micro (free) instance with the public subnets from above. All instances will have Apache installed on each instance so that the web service will be ready to use once the EC2 instances are up.
The traffic control is going to be done via the security sroup for the load balancer.
In the field of “User data” in the section of “Advanced details”, paste the Bash script which will be run to install the Apache Web Service. Also we will need to add an index.html file at /var/www/html otherwise there will be an error in the ALB health check and it will show “unhealthy” with the code [403] like below.
A sample Bash script for the User data field shows below.
#!/bin/bas
yum update -y
yum install -y httpd
systemctl start httpd
systemctl enable httpd
INTERFACE=$(curl -s https://169.254.169.254/latest/meta-data/network/interfaces/macs/)
SUBNETID=$(curl -s https://169.254.169.254/latest/meta-data/network/interfaces/macs/${INTERFACE}/subnet-id)
echo '<h1>This instance is in the subnet wih ID: SUBNETID </h1>' > /var/www/html/index.txt
sed "s/SUBNETID/$SUBNETID/" /var/www/html/index.txt > /var/www/html/index.html
领英推荐
Step 3 Create an Application Load Balancer to distribute traffic to the Auto Scaling Group
The security group allows inbound traffic from HTTP from 0.0.0.0/0 only
Step 4 Validate the system and the servers
On the EC2 dashboard, the three instances created by the ASG are all running.
Also we need to make sure:
To validate the load balancer, open the DNS name of the ALB in a browser. It should show different instance’s welcome pages when refreshing the web page.
Now the ALB loads the Apache’s welcome web page in EC2 1.
Refresh the page, the ALB loads the Apache’s welcome web page in EC2 2.
Refresh it again, the ALB loads the Apache’s welcome web page in EC2 3.
That is all for this AWS project! The Auto Scaling group and the Application Load Balancer are very convenient and useful to use when we want an auto scalable and stable system. Hope you enjoy the AWS journey here. See you next time!
Senior Growth Manager | No code & Dev tools
2 年Nicely done Weiping!