"Step-by-Step Guide: Creating and Configuring an Auto Scaling Group with Application Load Balancer on AWS".

Anil KushwahaAnil Kushwaha
7 min read

Creating and configuring an AWS Auto Scaling group with an Application Load Balancer involves several steps. Here is a step-by-step guide:

  1. Create an Amazon Machine Image (AMI) of your application that will be used by the Auto Scaling group.

  2. Create a launch template that defines the AMI, instance type, and any other settings that are required for your instances.

  3. Create a target group for the Application Load Balancer that will be used by the Auto Scaling group.

  4. Create an Application Load Balancer that will distribute traffic to the instances in the Auto Scaling group. Configure the listener rules and actions as needed.

  5. Create an Auto Scaling group that will use the launch configuration, target group, and Application Load Balancer.

  6. Configure the scaling policies for the Auto Scaling group. You can define scaling policies based on metrics such as CPU utilization, network traffic, or custom metrics.

  7. Set up CloudWatch alarms to trigger the scaling policies when certain thresholds are met.

  8. Test the Auto Scaling group by simulating traffic and monitoring the scaling activity.

Here are the detailed steps to create and configure an AWS Auto Scaling group with an Application Load Balancer:

Step 1: Create an Amazon Machine Image (AMI)

  1. Open the Amazon EC2 console.

  2. Launch an instance with the desired configuration for your application.

  3. Configure and install any necessary software on the instance.

    #yum install httpd -y

    #systemctl enable httpd --now

    #echo "this is $(hostname)" > /var/www/html/index.html

    http://18.136.212.107/

  4. Once the instance is configured, stop & then start it.

  5. In the EC2 console, select the stopped instance and click "Create Image" from the "Actions" menu.

  6. Provide a name and description for the image, and click "Create Image."

  7. Wait for the image creation process to complete. The new AMI will be available in the AMIs section of the EC2 console.

Step 2: Create a launch template.

To create a launch template for AWS Auto Scaling, follow these steps:

  1. Open the AWS Management Console and navigate to the EC2 service.

  2. Click on "Launch Templates" in the left-hand menu.

  3. Click on the "Create a Launch Template" button.

  4. Provide a name and description for the launch template.

  5. Choose an Amazon Machine Image (AMI) for your instances. You can either select an existing AMI, or create a new one by clicking on the "Create AMI" button.

  6. Choose an instance type for your instances.

  7. Configure the network settings for your instances, including the VPC, subnets, security groups, and any additional network interfaces.

  8. Configure the storage settings for your instances, including the root volume size, volume type, and any additional volumes.

  9. Configure the instance launch settings, including user data, IAM role, and SSH key pair.

  10. Review your launch template settings and click on the "Create launch template" button to create the template.

Step 3: Create a target group

  1. Open the EC2 console.

  2. Click "Target Groups" in the left navigation menu.

  3. Click "Create target group."

  4. Provide a name and description for the target group.

  5. Choose the target type as "Instance."

  6. Configure any other settings that are required for your target group, such as the protocol and port.

  7. Click "Create target group."

Step 4: Create an Application Load Balancer

  1. Open the EC2 console.

  2. Click "Load Balancers" in the left navigation menu.

  3. Click "Create Load Balancer."

  4. Select "Application Load Balancer."

  5. Provide a name and description for the load balancer.

  6. Choose the VPC and availability zones that you want to use for your load balancer.

  7. Configure the listeners and target groups as needed.

  8. Click "Create."

  9. "Finally, our Application Load Balancer's DNS name is ready to serve traffic. You can access your application by visiting the following link:

    Instance-ALB-1100665796.ap-southeast-1.elb.amazonaws.com .

    If you want to map this DNS name to a custom domain name, you can follow the instructions in this article: https://stackoverflow.com/questions/49488095/validating-domain-for-aws-acm-in-godaddy

    . Once you have added the DNS name to your provider, you can access your application using your custom domain name."

http://instance-alb-1100665796.ap-southeast-1.elb.amazonaws.com/

Step 5: Create an Auto Scaling group

  1. Open the Auto Scaling Groups console.

  2. Click "Create Auto Scaling group."

  3. Select the launch template that you created in step 2.

  4. Choose the VPC and availability zones that you want to use for your instances.

  5. Select the target group that you created in step 3.

  6. Click "Next."

  7. Configure any scaling policies that you want to use for your Auto Scaling group.

  8. Configure the minimum, maximum, and desired number of instances for your Auto Scaling group.

  9. Click "Next."

  10. Configure any notifications:

    "To set up notifications, please refer to my previous blog post on SNS where I have covered the topic in detail."

    https://aws-devops.hashnode.dev/how-to-create-and-configured-cloud-watch-cloud-agent-sns-on-ec2-in-step-by-step

All done. :)

Next-Level Troubleshooting Solutions:

A.

We have to perform the below task to test the load balancer so that we will know whether our load balancer is doing load balancing properly or not.

We currently have three instances in our setup. The first instance was added during the setup of the load balancer, and the remaining two instances were launched using Auto Scaling. We will now execute the following command on all three instances, which will create an index.html file and assign individual IP addresses to each instance. After completing this step, we will be able to verify whether the load balancer is functioning correctly by refreshing the load balancer link.

#echo "this is $(hostname)" > /var/www/html/index.html

Do the same on the remaining instances.

http://instance-alb-1100665796.ap-southeast-1.elb.amazonaws.com/

Here is the load balancer tested and working correctly.

B.

ALB Error Code:- health checks failed with these codes 403 alb

For instance, if an instance becomes unhealthy due to a 403 error code, you can resolve the failed health status issue by adding an index.html file to the web document root path, which is typically located at /var/www/html. Once the file has been added, check the instance's health status in your target group, and it should now be marked as healthy.

Before.

After.

C.

In order to verify that Auto Scaling is functioning correctly or not, so we need to perform the following task:

C.1

Here, we will install and use either the 'tmux' or 'screen' command to enable you to divide your terminal window into multiple panes.

#yum update -y

#yum install tmux -y

To split the terminal session using tmux, follow these steps:

  1. Open the terminal session.

  2. Type 'tmux' and press Enter to launch tmux.

  3. Press 'Ctrl + b' and then '%' to split the session vertically.

  4. Your session will now be split, similar to the example below:

C.2

"At present, three instances are up and running, and the CPU utilization is set at 50%. This means that when the CPU utilization exceeds 50%, a new instance will be launched, and when the utilization goes below 50%, the excess instance will be deleted to maintain the desired number of instances."

"The 'stress' utility will be installed and utilized here to induce system stress and test performance."

#yum install stress -y

Current Instance and Usages:

C.3

"CPU Utilization Reaches 100% After Running 'stress' Command, Triggering Launch of New Instance Following Successful Health Check below".

C.4

"After the 'stress' command was stopped, the CPU utilization returned to normal. As shown in the screenshot below, any instances that were launched after the CPU threshold was exceeded were automatically terminated through autoscaling until the desired number of instances was reached."

#ps aux | grep stress

#pkill stress

#top

By covering the following topics, I have created a blog on how to automate the Application Load Balancer and Auto Scaling Group in AWS step-by-step:

-EC2

-Auto Scaling Group (ASG)

-Launch Template

-Scaling Policies

-SNS/CloudWatch Alarms

-Application Load Balancer

-Target Group

-Listener

-Troubleshooting

Thanks,

Anil Kushwaha

0
Subscribe to my newsletter

Read articles from Anil Kushwaha directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Anil Kushwaha
Anil Kushwaha

As a technology enthusiast with a passion for efficient operations, I specialize in Linux, AWS Cloud, Prometheus-Grafana, Security, Virtualization, and Git. With a deep understanding of these technologies, I am able to streamline development processes and ensure smooth operations for businesses.