Autoscale on AWS with EC2, Python, Flask, and Nginx Part3

Analytics Vidhya
Published in
6 min readNov 21, 2021
Image generated using Imgflip

Previously, we set up an EC2 server, accessed it via Putty, created a Flask server with waitress WSGI, also created ubuntu service, and configured an Nginx server. Now, we are going to create an auto-scale group.

Before creating an autoscale group we will require an AMI, Target group, Launch templates. So let’s start with creating an AMI.


An AMI(Amazon Machine Image) provides the information required to launch an instance. It will basically store every configuration that we have done in our instance from which we are going to create AMI.

  • Go to EC2 instances and select the instance for which you want to create an image. Then click on actions → Images and templates Create image.
  • Give image name and description and leave other things as defaults.
  • Our image is created. Now let’s create Launch templates.

Launch Template

  • The launch template will tell our auto scale group to launch every new instance with AMI that we have created earlier with the given set of instructions like Instance type and Network setting.
  • On the left of the EC2 dashboard side, you can see Launch templates click on it. Give template name and version as per your choice.
  • Go to the AMI section. Where you have to select the image we have created earlier.
  • To select the image you can simply type your image name in the search box and your image will come in the MY AMIs section.
  • Now scroll down where you will see instance type and key pair section. Select instance type and key pair as per your choice. Whenever a new instance will be created it will use this instance type and key pair file.
  • In the Network setting please make sure you will select the security groups that we create in the first part. This will enable ssh and HTTP rules without this you will not able to access your instance via the internet or SSH(Like putty).
  • Keep all things as default and click on Create launch template. Our launch template is ready so let’s move on Target group. which will help load balancer to route traffic to all instances which are created in the autoscale group.

Target group

  • You can find the Target group in the load balancing section in the ec2 dashboard. Here we will select target type as an instance because we are creating this for ec2. Give Target group name as per your choice and click Next.
  • Select the ec2 instance you created first part. Now click on include as pending below. It will register our instance in this target group.
  • Scroll down a bit and you will see an instance is registered in the target group. Click on create target group. Till here we have created a Launch template, AMI, And target group now we have all things which are required for the Autoscale group.

Auto Scaling Group

  • You can find the Autoscaling group in the auto-scaling section at bottom of the left side in the EC2 dashboard. Click on the auto-scaling group then click Create auto-scaling group.
  • First, enter an Autoscaling group name of your choice. Select the launch template that we have created earlier and ensure the version of the launch template is the latest.
  • Select the VPC same that you have used at the time of your instance creation. Select all Availability zones.
  • Now we will create our load balancer and include our target group. Select the “Attach new load balancer” radio button and load balancer type as “Application load balancer” as we are going to control HTTP and HTTPS traffic.
  • Load Balancer
  1. Create load balancer name as per your choice.
  2. Select load balancer scheme ad internet-facing as we want our customers to access the app.
  3. Select the VPC and availability zones same as per the instance we have created earlier.
  4. In listener and routing select the target group which we have created earlier.
  5. Click Next.
  • In group size, we can select the capacity of the server which is required as per our requirements. For demo purposes, I will keep desired capacity as 1, Minimum capacity as 1, and maximum capacity as 2.
  • Scaling Policies is a metric that will tell the Autoscaling group when to increase or decrease server. Here we are keeping a 50% threshold for CPU utilization. So if CPU utilization goes above 50% it will add a new instance or if we have more than the minimum instance than required and CPU utilization goes below 50% it will decrease instances. Warm-up seconds are the time till when your instance is utilization reached threshold and keep above that threshold for this much time then Autoscale group will increase/decrease instance.
  • Leave all the steps as default, review, and launch the Auto Scaling group.
  • That’s all we have created the auto-scaling group. Now let’s verify how it’s working.

Access site

  • For Access your site you can go to load balancer → select to the load balancer → Copy DNS name → paste in your browser.
  • If you are not able to access the site make sure you try DNS names with HTTPS and HTTP. if it doesn’t work try to change the security group of the load balancer with the same as the instance security group.
  • For change security group select load balancer then scroll down you will get an option for the security group.
  • To check auto-scale’s performance, let’s create some artificial load on the site. There are many options that you can find here.
  • I am going with Apache Benchmark. We’ll send 10000 requests with 100 concurrent users. The results will be saved in the test1.txt file.
ab -n 100000 -c 100 load_balancer_dns_name > test1.txt
  • It will generate 15–20 % CPU usage. You can change the autoscale CPU usage parameter to 10% so you can verify.
  • To check CPU usage run the following command. which will command will show CPU usage in percentage in every second
mpstat 1

If you like this post, HIT Buy me a coffee! Thanks for reading.

Your every small contribution will encourage me to create more content like this.



Analytics Vidhya

Howdy & Welcome. I am a content creator, machine learning researcher, and consultant. consultancy: