Creating an Auto Scaling EC2 Utilizing AWS Resources

A Tutorial Guide to Increase Your Knowledge of the Benefits of Creating a Customized Auto Scaling E2 and Utilizing AWS Resources to Manage High Availability.

Melissa (Mel) Foster
Women in Technology
11 min readMar 28, 2023

--

Flow Chart Created using https://app.diagrams.net/

Today, we will start by creating an AWS VPC. This will enable us to launch AWS resources into a virtual network that we define, with the added benefits of using the scalable infrastructure of AWS.

VPC: virtual network that closely resembles a traditional network.

A few key take away benefits for using a VPC:

  • Security
  • Improved performance
  • Availability
  • Increased resources to channel innovation

As we progress our knowledge of AWS our tasks are going to continue to dive a little deeper and test what we are capable of. This might feel heavy at times, but trust the process.

Tasks for today:

1️⃣Create a VPC with a CIDR 10.10.0.0/16

2️⃣Create three Public Subnets with different addresses: 10.10.1.0/24 & 10.10.2.0/24 & 10.10.3.0/24

3️⃣Create an autoscaling group using t2.micro instances. Each Instance should have Apache installed.

4️⃣Create an Application Load Balancer to distribute traffic to the autoscaling group

5️⃣Create a security group that allows inbound traffic from HTTP and outbound from 0.0.0.0/0, all from your Application Load Balancer

6️⃣Set the min & max Autoscaling to 2 and 5

7️⃣Add a Target Policy to scale comp 50%

8️⃣Use the DNS url of the Application Load Balancer in a browser to verify you can reach your sit.

To follow along with this project you will need:

  • AWS User Account with privileges. Note: once again we will be utilizing free tier throughout the entirety of the project.
  • PowerShell/Terminal
  • Time & Patience

To begin, we you will need to be logged into your AWS Console to start Task #1: Create a Custom VPC. If you already don’t have VPC showing in your Recently visited, you can enter VPC into the Search Bar.

Select VPC
Select Create VPC

We are now in the custom VPC screen here we will:

  1. Choose VPC & More
  2. Create a Custom Name
  3. Set Parameters for IPv4 CIDR block (Tasked with assigning 10.10.0.0/16)
Note: the small typo in image on the IPv4 CIDR- when I went to launch I received an error stating that the custom subnets below would not be in the same CIDR. Corrected to 10.10.0.0/16.

Scroll to the next section Availability Zones where we will complete Task 2: Create three Public Subnets with different addresses: 10.10.1.0/24 & 10.10.2.0/24 & 10.10.3.0/24

  1. Choose the number of Availability Zones (Tasked with needing 3)
  2. Set the number of public subnets (Tasked with assigning 3)
  3. Leave Private Subnets at 0
  4. Customize the subnets CIDR blocks we were tasked with assigning
Remember to double check your text for typos. Off by a number and you will not be set up correctly and run into errors.

Scroll down to NAT gateways to complete the final steps of setting up our VPC:

  1. Set the number of Availability Zones to None
  2. Set the VPC endpoints to S3 Gateway
  3. Check both boxes for DNS options
  4. When your ready Select Create VPC

*If you noticed before you hit Create VPC your right hand of screen will be displaying a routing table preview for you.

After selecting Create VPC you window should now display a Success Screen detailing everything we created.

Feels good to mark two tasks complete doesn’t it! Let’s keep moving along to Task #3: Create an autoscaling group using t2.micro instances. Each Instance should have Apache installed.

Reminder:

  • EC2 Instance is a virtual server in Amazon’s Elastic Compute Cloud (EC2) used for running applications on the Amazon Web Services (AWS) infrastructure.
  • Apache is the most widely used webserver software worldwide

To create our EC2 Instances we return to our AWS Console.

Select EC2

We are going to learn another way to use our EC2 resource. Instead of creating the EC2 like I have shown in previous projects, we are going to use a template. Select Launch Template from the left-hand menu options.

When you selected Launch Templates from the menu, you will be brought to a home screen like this if you have never created a template. This will be my first template, I am confident we will be successful. Select Create launch template.

Let’s begin with customizing our template. In the first section of your screen we will enter the following:

  1. Enter what you would like to name your template. I chose ASG-Week7-MelFoster-Template, since the our project is to set up an Auto-Scaling Group. Get creative, name it whatever you want!
  2. Enter the Template version description, which is what we are using it for.
  3. We need to select the little box under Auto Scaling guidance if we intend to use this template with an EC2. Which we do, and who doesn’t love a little guidance to ensure we set everything up correctly.

Continue down the screen to the next section Launch template contents, where we will set up our AMI.

Reminder: AMI — Amazon Machine Image.

We are choosing the OS that will be running the instance we create. I currently do not have a custom AMI to use for this project today, so click the Quick Start tab continue to set up.

I will be working with Linux, as it is easier to test out commands. You do have the option to choose another OS. Ensure you choose one with Free Tier Options, and you continue to follow the Free Tier Guidelines.

Continuing down the set-up screen we will create our Key Pair.

Helpful Tip: Remember to store you key-pair in a secure location where you can refer to it’s direct path incase you run into a SSH issue. In PowerShell sometimes it requires you to Set-Location to be able to SSH into an EC2 with a .pem file. If you are on a MacOs and having an issue with Terminal, you can cd into the directory where your .pem keys are stored and use the SSH command from there.

Our next section we need to pay attention to details here:

  1. Don’t include Subnet in launch template
  2. Create our Security Group

2a. Name your Security Group

2b. Allow SSH & HTTP

3. Select the VPC we previously created

Next, let’s continue down the screen and ensure we correctly set up our Inbound security group rules per our initial task objectives. Make sure we have the Source Type: as Anywhere. Setting it up this way, we have given permission to allow Inbound traffic on port 22 & 80. (Pretty essential if you want your customers to connect to your webpage.)

Continue down to Advanced network configurations:

It is VERY IMPORTANT we will be only enabling Auto IP Addresses. We do not need to configure or change any other option in this screen.

Scroll down till we get to Advanced Details. Under this section we will again continue to scroll down, till we get to User Data. This will allow us to create a bash script to install the apache server.

Keep Scrolling till you get to the User Data portion of the Advanced Settings
#!/bin/bash
yum update -y
yum install -y httpd
systemctl start httpd
systemctl enable httpd
echo "Mel Foster thinks Green Team is the BEST!" >> /var/www/html/index.txt > /var/www/html/index.html

After you feel comfortable with your script, and you verified your settings are correct. Join me in selecting Create launch template. (Kind of feeling very nervous, it’s normal it’s our first time.)

At the bottom of your screen Select View Template

Yay! It feels good to complete a large portion of Task 3! We are officially on the right path. We now need to create an Auto-scaling group. (Deep Breaths you got this!) Our next section will complete the rest of our tasks, as they all go hand in hand. Hang in there we will get through it.

After selecting view template, we are on the Launch template screen where we will see our custom template, we so carefully created. On the left hand menu you will want to scroll down till you see Auto Scaling then choose Auto Scaling Groups.

Want to know more in depth about Auto Scaling check out the AWS White Paper: https://docs.aws.amazon.com/autoscaling/ec2/userguide/auto-scaling-groups.html

You will now be at the home page for Amazon EC2 Auto Scaling. Select Create Auto Scaling Group.

Your screen should be showing Choose launch template or configuration screen. Here we will enter a name to identify the group and from the drop down arrow choose the Launch Template we created. Click Next at the bottom of the screen when you are ready to move on.

Our next step is to choose the VPC we created from the drop down, and then assign all the subnets we created from the next drop down.

If everything is correct we will now select the next button.

On the left hand menu it shows that we are now at Step 3. This is where we will configure our load balancing. So for us it’s not optional. Select Attach to a new load balancer & then choose Application Load Balancer.

Want to know more about AWS Load Balancers Check out this AWS White Paper: https://docs.aws.amazon.com/elasticloadbalancing/

Next we will name our Load balancer & choose our Load balancer scheme, which will be Internet Facing:

Continuing down, there’s a small section that is has a lot of impact. We need to ensure we have our Protocol on as HTTP and Port 80. Create a new target group and name it. Lastly, before we hit next, make sure we check the box showing Enable group metrics collection within CloudWatch. (Task #5)

Yes we are now at Step 4- Again this section is not optional for us, as we want to configure our desired min/max capacity that we were tasked with. Task #6 from above, we want to configure to have a min of 2 and a max of 5.

Under the Scaling polices select Target tracking scaling policy and make sure the target value stays at 50, That’s Task #7 ✔

At the bottom of the screen once you feel you have everything configured, click next to continue on. We will be skipping Step 5 & 6. Step 7 It’s Review time! Glance over your configurations before clicking Creating Auto Scaling Group.

Created record utilizing Microsoft Extension and GIF Created in Canva

If we were successful we should be able to see two EC2 Instances running on our EC2 Dashboard.

Our First Instance IP
Our Second Instance IP

SO close, I know you thought we were complete, but wait Task #8: Use the DNS url of the Application Load Balancer in a browser to verify you can reach your site. Initally, when I tried to verify by my DNS it timed out. To resolve, I went back to my Load Balancer to ensure the security group that I set up was linked properly. It was not. Let’s correct that. We will go into our Load Balancer, scroll down to Security Group Section; choose Edit.

(You might be noticing a web browser color change. You can thank my lovely eight year old for that bit of color pop.)

From here you can choose from the drop down and ensure your setting isn’t on default, and choose your custom one you created.

Select Save Changes and return to your Load Balancer Screen to capture your DNS name to verify via your web browser.

Yes!! It feels good, doesn’t it?

WHAT!! WE DID IT! I absolutely think Green Team is the BEST!!

Let’s finish this project strong by testing Task #7 to ensure that our scaling policies are function. The best way to do this is to Stress Test. From your PowerShell or Terminal you need to SSH into one of the running EC2s we created. Once you are successfully logged in run the command to install stress test:

sudo yum install -y stress
Stress Test Installed Successfully

If we run the command uptime you will see the Instance runtime, how many users, and load average. If we run the a stress command followed by a few things to stress out our system we should be able to get a new Instance launched to handle the load.

uptime
sudo stress --cpu 8 --vm-bytes $(awk '/MemAvailable/{printf "%d\n", $2 * 0.9;}' < /proc/meminfo)k --vm-keep -m 1

Our stress command run at the sudo level will create 8 CPU workers and increase the memory to 90%. I found this command while researching on a couple different webpages. https://www.cyberciti.biz/faq/stress-test-linux-unix-server-with-stress-ng/ is a useful resource for information and commands to stress test.

You can also see I couldn’t get the stress to stop running or spit out results. If your PowerShell ever does this it’s helpful to remember ^C which will halt the execution and it will bring you a new command line.

During my stress test, my internet went down and my AWS Console had an undefined error occurred with the instructions to refresh my AWS Console. Once my internet was back up and running, we can see that my stress test did successful cause the reaction we were hoping for: an additional EC2 was created.

Overall, we successfully completed all required tasks! Congratulations! I know it was a long process to get here, and maybe you might come across some bumps and unexpected issues of your own. That is all part of the process. Research, network and then resolve. Troubleshooting is a huge part of working with technology that is always changing. The ability to adapt and remain patience is a skill I am growing daily.

Edited Logos on White Background

Important Reminder: You need to terminate your Instances.

First adjust your min/max back in the Auto Scaling menu. Once you do this, your additional Instances will automatically terminate. Additionally, you can detach your Instance. As a friendly reminder if you are doing these projects utilizing Free Tier, it’s best practice to delete/terminate anything you no longer need. Save yourself space and money.
Helpful resources can always be found on the AWS Whitepapers.

--

--

Melissa (Mel) Foster
Women in Technology

𝔻𝕖𝕧𝕆𝕡𝕤 𝗘𝗻𝗴𝗶𝗻𝗲𝗲𝗿 |𝒲𝑜𝓂𝑒𝓃 𝐼𝓃 𝒯𝑒𝒸𝒽 𝒜𝒹𝓋𝑜𝒸𝒶𝓉𝑒 | 𝚂𝚘𝚌𝚒𝚊𝚕 𝙼𝚎𝚍𝚒𝚊 𝙲𝚛𝚎𝚊𝚝𝚘𝚛 | Photographer