Autoscaling on AWS using Elastic Beanstalk and CloudFront

abhinav singhal
AWS Tip
Published in
6 min readMar 4, 2023

--

In this exercise we will see how to create an environment where we can autoscale the EC2 instances once CPU load is increased on the instances. In order to set it up, we will create an environment like a real-world example using step-by-step instructions and then create elastic bean stalk config to configure the autoscaling.

Below diagram show high level example of what we are trying to achieve when we set up the environment.

  1. Create dynamo DB2.
  2. EC2 instance and ELB and elastic beanstalk.
  3. Add cloud front and Amazon Route3

Login to Amazon and look for dynamo DB

  1. Create new Table, lets call it as users
  2. partition key as email

and create the instance.

Setting up Elastic beanstalk

  1. open a separate tab and search of elastic beanstalk
  2. create application call it as “tcb-conference” and select platform as python 3.4.4

Upload your code, I am using my sample code repository for this purpose: link

Select High availability option.

update the instances as shown below

update the scaling

update the security and select the key pair to start the instance

Network setting update

Select subnets

now click on create APP, this will provision the infrastructure. you can observe it started creating an ec2 instance

This will help is setting up in EC2 instances that can be scaled up. We may also have a scenario where you are not able to access Dynamo DB as we didn’t setup security group on Elastic bean talk. So one of the approach could be that you can add new policy to the existing roles under the security group which is highlighted in below image.

This way you don’t have to create another group just for DB access. All you should do is add fulldynamoiDB access policy under the same role.

Setting up Cloud Front

This will help in deploying and accessing the static and application files closer to servers to improve the performance.

  1. Navigate to cloudfront free tier. Select the create cloud front and in the drop down select the option of elastic load balancer . As our application will be fronted by load balancer.

Now create cloud front distribution.

The deployment can take up to 10–15 minutes, so wait for few minutes till the status shows as deployed.In above image you can see that we have the domain name that can be used to access the application server.

in our example its coming as : https://d35kojaitbwkcu.cloudfront.net

You can use above URI to access the website that you just deployed.

Previously we set up elastic beanstalk with 2 instances, which seem healthy. We can check in the configuration if their utilization is healthy or not.

Now let's try to put some load on the application, which will help in showcasing the autoscaling of the application.

Now as last part of the deployment, we will setup of Autoscaling capability.If you recall we did the setting in the previous session of autoscaling like shown below, the upper threshold is 50% and once it reaches upper threshold it will autoscale another EC2 instance.

In order to test this we will try to put some pressure on the environment.

Access one of the ec2 instances using the private key. as shown above. If you carefully read it, it also says that this instance is being managed by elastic beanstalk. This is a cool thing to see.

Now we will install a stress package.

If you want to learn more about it, please refer to EPEL package using this link .

The ‘EPEL’ part is an abbreviation that stands for Extra Packages for Enterprise Linux. The EPEL group creates, maintains and manages a high-quality set of additional packages.

sudo amazon-linux-extras install epel -y
sudo yum install stress -y

Once stress test package is installed now let's run the stress test. This can be done by using this command.

stress -c 4
  1. Below you can see that terminal is adding load on the environment.
This shows the warning signs that your EC2 instances are going under stress. It could be based on configuration that was being set using beanstalk
  1. You can see slowly that cloud front health is showing the as warning
  1. This is showing that instance health is degrading and will start showing as “degraded”
  1. Now you can see that the third instance is also coming up.

This is one of an interesting examples with hands on where we saw how to setup instances to scale up when the environment experiences load on the server.

The goal has been met in this example. Now we can stop the load testing as the results are being met . This will remove the warning back to healthy in a few minutes as shown below.

Once this exercise is completed, please remove all the resources manually. I don’t want you to get charged $$ for billing these examples :-)

I hope you had fun experimenting.

--

--

SE-Manager #Hiring #Mentorship #DEI #SoftwareDelivery #KPI #OKR #java-J2EE #Nodejs #GraphQl #Aws