Creating a VPC environment with an Auto-Scaling group on EC2 instances…and more

Angelomarchaidez
7 min readFeb 25, 2023

--

Special guest appearances by Auto-Scaling Group, The Public Subnets, Security Groups, The Load Balancer and his balancer group.

Overall architecture of a VPC environment

This week, we will be tackling our project in two different phases, 1) the foundational portion, 2) the complex portion coming soon. Let’s take a look at the requirements for each. If you’re reading this I will assume basic knowledge of AWS EC2, VPC, Auto-Scaling, Load balancers, and security groups. We will create our own environment on EC2 instances with an Auto-scaling group allowing for our application to be spread among three different AZ (availability zones) in the same region. Below are the requirements for this project. Let’s get started

FOUNDATIONAL

  1. Create a VPC with cidr 10.10.0.0/16
  2. Create three public subnets with 10.10.1.0/24 & 10.10.2.0/24 & 10.10.3.0/24
  3. Create an autoscaling group using t2.micro instances. All instances should have apache installed on each instance with the ability to check any random IP address and be able to produce a test page. Ensure the autoscaling group is using the public subnets from #2.
  4. The autoscaling min and max should be 2 and 5.
  5. Create an Application Load Balancer to distribute traffic to the autoscaling group.
  6. Create web server security group that allows inbound traffic from HTTP from your Application Load Balancer.
  7. Create a load balancer security group that allows inbound traffic from HTTP from 0.0.0.0/0.

Creating our VPC

Okay! Let’s get started.

Let’s first create our VPC from the AWS console. In the search bar type in VPC and select “Create VPC”

Let’s go through our initial settings for our VPC, we can create our three different subnets later or we can launch them here, and since I am lazy, lets get it done all at once. Select “VPC and more” option, name your project, and give your CIDR block and address per the requirements.

Now we can select the amount of AZ’s and subnets. We will create three each.

Creating our Subnets

Now, let’s give each public subnet a CIDR block address, make sure that there is no overlap or else the subnets will not work.

subnets in their own AZ’s

Once you create your VPC and subnets, you can view the mappings of your connections. In our case we have the VPC connecting to our three subnets on a public (default) table and our gateway.

Let’s execute the creation of our VPC with our subnets.

Creating our Gateway

Now lets go to our VPC and enable our internet connection. In the VPC dashboard we will select “Internet Gateway” and attach to our VPC.

Creating our Route table

While in our VPC dashboard, select “Route table” and select the “Create route table” option.

Let’s name our route table and select our VPC and create our route table.

Adding routes to our route table

Once created we need to add our routes to our table. We will add all traffic noted in the 0.0.0.0/0, and select Internet Gateway and your gateway should appear.

Creating our EC2 templates

Let’s get started on our EC2 instances, we want to create a launch template, that our ASG (Auto Scaling Group) will launch and prevent from failing or deletion. In our EC2 dashboard look for “Launch templates” and select. Now select “Create launch template”, name your template and give it a description.

Inflation and gas are high, bank account too low, and I cant afford to give AWS my money, so let’s stick with all of the free tiers Amazon offers. Select the “Amazon Linux” AMI that is free.

Select the “Instance type” as “t2.micro” free tier eligible, create a “Key pair” if you like, in this case to avoid any confusion I will treat this all as a new project.

Let’s create a security group for our instances, this group will essentially work as our firewall to control inbound/outbound traffic. Give your group a name and description.

Now let’s use our VPC to host our EC2 instances on. We will select our VPC created earlier.

Select the “Advanced Network Configuration” drop down and assure that you have “Auto-assign public IP” Enabled.

enable Auto-assign public IP

In the advanced dropped down lets scroll all the way to the bottom and we will give our instance some options to update the instance each time it is launched, install Apache server and start, and give some simple html code for our static site. Now we can “Create launch template”.

Creating our Auto-Scaling Group

Ok, now we can create an ASG to use the EC2 instances, the ASG will automatically scale our instances when needed.

Let’s name our group and select the launch template we created in the previous steps. Select the VPC created earlier and select the availability zones that we created along with our VPC.

Let’s review and select “Next”

Creating our Load Balancer

While creating our ASG we have the option to create a Load balancer which will balance incoming traffic across our target zones. Lets select new balancer and select “Application Load Balancer” for HTTP and HTTPS.

Select your VPC and select our three AZ’s. In the HTTP option with port 80, select “Create a target group” from the drop down and one will be created for you using conventional naming after your ASG group.

Now let’s select the group size, note that we will select 2 as the desired capacity with a maximum of 5.

Let’s review and create our ASG.

Make sure that once complete you ASG you change the security group option to your security group created earlier. You will also have to add the security group to your load balancer.

Launching our EC2 instances from template

Once complete, let’s head back to our EC2 dashboard and see if our ASG launched our instances

We can see that our instances are running, now lets check if we load our site through our load balancer DNS name and launch the instance to our browser.

Checking our Apache server

Let’s look at what is happening in our browser as we hit refresh, we can see that as we refresh the new traffic will be distributed among our different AZ’s.

Browser window with refresh

We can see that the site is being loaded through both the us-east-1 and us-east-2, and the reason that us-east-3 is not being utilized is from the previous setting in our load balancer, where we left desired amount to 2, with maximum of 5. However there is not enough traffic to use more than two AZ’s.

I will be attempting to finish the complex portion next stay tuned…

(COMPLEX: Do the Foundational or Advanced project using AWS CloudFormation.)

Thank you for tuning in and reading my article. Catch you on the flippity flip.

--

--

Angelomarchaidez

Love learning about technology. Experience with Java, Python , BASH scripting, C/C++, FORTRAN 90 (I'm old). AWS DevOps Engineer, terraform associate and Linux.