Installing AWS Web servers using an Auto Scaling Group with Application Load Balancer

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

  • Create a VPC with CIDR 10.10.0.0/16

First we need to create a VPC for our system like below.

No alt text provided for this image

  • Create three subnets with 10.10.1.0/24 & 10.10.2.0/24 & 10.10.3.0/24

The subnets are in different Available Zones. And their CIDRs must be within the one in the VPC.

No alt text provided for this image

After creating the subnets, check the “Enable auto-assign public IPv4 address” in the subnet settings to make the subnets public.

No alt text provided for this image

  • Create the internet gateway

No alt text provided for this image

  • Edit the route table and allow the connection to the internet

In the “Routes” tab add the internet gateway we just created.

No alt text provided for this image

In the “Subnet Associations” tab, add the three public subnets like below.

No alt text provided for this image

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.

  • Create a security sroup for the EC2s in the ASG that allows inbound traffic from HTTP

The traffic control is going to be done via the security sroup for the load balancer.

No alt text provided for this image
No alt text provided for this image

  • Create a template for the ASG in “Launch templates” inside the EC2 panel

No alt text provided for this image

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.

No alt text provided for this image

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        

  • Create the ASG

No alt text provided for this image
No alt text provided for this image

Step 3 Create an Application Load Balancer to distribute traffic to the Auto Scaling Group

  • Create a Target group with “HTTP:80” and IP type: IPv4

No alt text provided for this image

  • Create a load balancer security group

The security group allows inbound traffic from HTTP from 0.0.0.0/0 only

No alt text provided for this image
No alt text provided for this image

  • Create an ALB with the target group above and a listener “HTTP:80” setup

No alt text provided for this image
No alt text provided for this image

  • Add the target group to the ASG’s Load balancing section

No alt text provided for this image

Step 4 Validate the system and the servers

On the EC2 dashboard, the three instances created by the ASG are all running.

No alt text provided for this image

Also we need to make sure:

  • In the instances the Apache Web Service is installed successfully.
  • In the instances the file /var/www/html/index.html exists.
  • In the Target Group health check for the instances are “Healthy”

No alt text provided for this image
No alt text provided for this image

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.

No alt text provided for this image

Now the ALB loads the Apache’s welcome web page in EC2 1.

No alt text provided for this image

Refresh the page, the ALB loads the Apache’s welcome web page in EC2 2.

No alt text provided for this image

Refresh it again, the ALB loads the Apache’s welcome web page in EC2 3.

No alt text provided for this image

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!

Tarak ??

Senior Growth Manager | No code & Dev tools

2 年

Nicely done Weiping!

回复

要查看或添加评论,请登录

Weiping Zhang的更多文章

  • Launching an EC2 and installing Apache with a custom webpage

    Launching an EC2 and installing Apache with a custom webpage

    Hello friends! Welcome to my first Cloud project. In this post, I am going to talk about how to launch an EC2 instance…

  • Contributing a new project to Github

    Contributing a new project to Github

    Welcome to Weiping’s Channel! As a powerful and popular code hosting platform for version control and collaboration, It…

    6 条评论
  • Updating the Repository with Git

    Updating the Repository with Git

    Hello everyone! In this post, I am going to demonstrate how to make changes and updates to a repository safely with the…

    2 条评论
  • Installing a NGINX Web Server on CentOS 8 Server

    Installing a NGINX Web Server on CentOS 8 Server

    Welcome everyone! In this post I am going to demonstrate how to install and remove a NGLNX Web Server on CentOS 8…

    3 条评论

社区洞察

其他会员也浏览了