Amazon’s EC2 Logo

Configuring AWS ASG and testing the policy

Marshall Hubbard
Nerd For Tech
Published in
7 min readMar 24, 2021

--

This lab is a step-by-step walkthrough demonstration on how to configure an AWS Auto Scaling Group and also how to test to make sure that the policy that you created is being followed. What we need to know for this lab follows:

  1. How to configure an EC2 server
  2. What is bootstrapping and how to bootstrap your EC2 server
  3. What is an Apache server and how to install it to your EC2 server
  4. What is an Auto Scaling Group/Auto Scaling Policy and what exactly are they used for
  5. How to test your Auto Scaling Policy

Alright, as my 5 year old son would say, “let’s get this party started.”

Step 1: Configure your EC2 instance and bootstrap it to run Apache

Before we dive into bootstrapping our EC2 instance, please keep in mind that you will need a VPC. The VPC should be completely configured and loaded (including an internet gateway and a route table) ahead of time before AWS allows you to configure a EC2 instance and for it to have access to the internet.

Once your VPC is up and running, the next thing to do is to create your Instance by typing in EC2 in the AWS search bar and selecting Launch Instance. After the page has loaded, select the AWS AMI (Amazon Machine Image) that you want to use for the lab. This step is important because it will dictate what commands to use when bootstrapping your instance, choose carefully.

The next step is to select your VPC, public subnet and to Enable your “Auto-assign Public IP” setting.

Here we will begin bootstrapping our instance to automatically run Apache, an open-source web server. Enter the above script into the “user data” box and leave it as text. Keep in mind that depending on which AMI you choose will dictate what commands you use. An example, with AMI 2, you will typically use “yum” commands. For this lab, I chose the Unbutu AMI and ran the following “apt-” commands:

#!/bin/bash

apt-get update -y

apt-get upgrade -y

apt-get install apache2 -y

systemctl start apache2

systemctl enable — now apache2

When the page has been configured, move on to the next step.

This step is important. Configuring your instance’s SG will dictate how you interact with your instance as far as being able to SSH into it and whether it will open port 80 to host your Apache Web Server. I chose to utilize SSH, HTTP, and HTTPS (which isn’t necessary for this project). When everything is selected, Review and Launch your instance.

Check your work. After your instance is launched, we will check to verify that it will indeed launch Apache successfully. I chose to copy my instance public IP address and paste it into the search bar. If you receive this page, you are good to go to continue.

Step 2- Create your Image

For this step, click on the EC2 that we just created and select the Actions tab. Once you do that, scroll on the tab down to Templates and select “Create Image”. This will create an image of your EC2 for the rest of this lab so that the instances for the Auto Scaling Group can copy the configuration of this particular one. Make sure you give it a name and then press “Create Image” in the bottom right corner.

Step 3- Configure your launch template

The next step that we will be tackling is the “Launch Template”. This simply specifies the instance configuration information. To find it, scroll to the left side of the EC2 console and then scroll down until you find “Launch Template”. Click on it and then press the Launch Template that is highlighted orange. Once you do that, you should be on this screen that I posted directly above. We will give this template a name for future use and also select the template and the template version with what we just created in the last step. Verify that your key pair and security groups are correct before selecting the “Create Launch Template” in the bottom right corner.

Step 4- Create your Target Group

For this step, we will scroll back over to the left side of the EC2 console and scroll down for Target Groups. Select your Target name and then select “Create” in the bottom right corner. A Target Group simply just tells a load balancer where to direct traffic to. If for some reason you get an “error to load message”, try to switch back to the old EC2 experience. I found this step assisted me with completing this part of the lab.

Step 5- Configure your Auto-scaling Group

To get to this step, scroll back to the left side of the EC2 console and scroll down until you find Autoscaling Groups and click on that. An Autoscaling group (ASG) just contains a collection of Amazon EC2 instances that are treated as a logical grouping for the purposes of automatic scaling and management. Once on this page, give your group a name and select the Launch Template that we just created and make sure that everything is correct before you move to the next step.

Here you will choose “Adhere to launch template” and also select your VPC and your Subnet. Make sure that you choose your PUBLIC subnet. If you need another one, there is an option right there to create it.

I left the “Advanced Settings” as their default settings and moved on to configure the group size. For the desired capacity I chose 3, for the minimum capacity I chose 2 and for the maximum capacity I chose 5. This will allow the ASG to scale out to changes while still maintaining our minimum and maximum capacity. I also chose “300” as the seconds needed to warm up due to it being a AWS recommendation. I left everything else at default settings.

Although I did not add any notifications to this lab, this would be a crucial step in a real world application to be directly notified whenever your ASG launches or terminates your EC2 instances.

Step 6- Test out your ASG

Upon reaching this step, we will click on each EC2 that we just created and then go to “Instance State” and choose Stop Instance. The purpose of doing this is to see if the ASG will catch it after it’s 300 second check and create more instances due to the scaling policy that we just created.

Here you will see that the ASG successfully created new instances due to the policy that was created.

Now let’s go back to the “Auto Scaling Groups” on the left side of the EC2 console and select the ASG that you created and click on the Activity tab. Here you will see what exactly your ASG is doing. This is also a great way to make sure that your ASG is following the policy that you previously created. You can see in my activity that once I stopped my original ASG instances, the ASG created more immediately.

The following is an extra step simply because it takes less than a couple minutes to complete. Now we will login to the CLI and connect to each instance to test them. For my CLI, I used the Mac Terminal but you can use the AWS Instance Connect that’s already built into your Instance or run Puddy for Windows. As far as the actual testing goes, we will use Apache Bench which comes preloaded into your server once you install Apache. Apache Bench is simply a way to measure the performance of your server. Once you connect to your first instance, we will start by running the command “ab” so that you can see the options that you have to test your instance. I used the command “ab -n 1000 -c 10 http://3.94.126.92/test.cfm > test1.txt” which means that we are sending 10 requests at a time as quickly as possible and will stop when we reach 1000 requests. This test didn’t return back any failed request which means that we were successful.

--

--

Marshall Hubbard
Nerd For Tech

LE Professional transitioning to DevOps. Currently learning at Level Up In Tech.