Skip to content

Autoscaling with EC2 Web Server provide users a smooth experience when visiting their favorite website. Resources dynamically add or reduce capacity depending on traffic conditions—making maintenance easier while providing consistent performance no matter the traffic size.

Notifications You must be signed in to change notification settings

ericincloud/Autoscaling-EC2-Web-Server

Repository files navigation

Autoscaling-EC2-Web-Server

Autoscaling with EC2 Web Server provides users a smoother experience when accessing their preferred website. Utilizing the Application Load Balancer and Autoscaling Group, user traffic is automatically distributed evenly, regardless of whether it is heavy or light. Resources dynamically adjust capacity based on traffic conditions, simplifying maintenance and ensuring consistent performance.

Architectural Diagram

AutoScaleArch

NOTE: Terraform file (main.tf) in repository.

Launch using Terraform requires only configuring the ALB's security group. Simply replace outbound HTTP Port 80 from anywhere to HTTP Port 80 EC2 Instance's Private IPv4 address.

Step 1: Create VPC

Create VPC with 3 Public subnets and 3 Private subnets.

CreateASvpc

Step 2: Create NGINX EC2

Create an EC2 instance installed with the Bitnmai NGINX AMI web server in a private subnet.

bitnaminginx nginxec2

Step 3: Setup ALB

Create target group containing the NGINX EC2 instance. Then setup an Application Load Balancer in public subnet and connect it to the NGINX web server EC2 instance.

Edit the ALB's security group Inbound rule to allow HTTP traffic on Port 80 from 0.0.0.0/0 anywhere + Allow Outbound traffic on Port 80 to the EC2 instance using it's private IPv4 address.

In the EC2's security group: Add Inbound EC2 security group rule allowing HTTP Port 80 for the ALB's security group. Add Outbound EC2 security group rule allowing HTTP Port 80 for the ALB's security group. Verify functionality by accessing the ALB DNS name. A default NGINX webpage should show up.

defaultnginx

Step 4: Create autoscaling group

Create Launch template with the Bitnami NGINX AMI and in a private subnet. The configuration should be of the initial Bitnami NGINX EC2 instance. Then create autoscaling group that takes into account the ALB and EC2.

AStarget ASGroup

Create 2 simple scaling policies - one that adds capacity units/instances if CPU utilization is over 70% and another that removes capacity units/instances if CPU utilization is under 40%.

add70 ASpolicies

Step 5: Setup EventBridge scaling alarm

Setup an CloudWatch Events scaling alarm that increases the number of EC2 instances available in the auto scaling group if CPU is over 70% and reduced the number of EC2 instances if under 40%. In CloudWatch, create an alarm > search for CPUUtlization > Select the newly EC2 CPUUtilization autoscaling group metric

CWcpu

Create 2 alarms. One that adds instance when CPU utilization is over 70% and another that removes instance when CPU utilization is undert 40%.

CW40and70

Finish!

Notes

  • Remember to edit the ALB security group to allow HTTP traffic on Port 80 from 0.0.0.0/0 anywhere.
  • Use internet facing ALB.

About

Autoscaling with EC2 Web Server provide users a smooth experience when visiting their favorite website. Resources dynamically add or reduce capacity depending on traffic conditions—making maintenance easier while providing consistent performance no matter the traffic size.

Topics

Resources

Stars

Watchers

Forks

Languages