AutoScaling Ec2 Instances based on CPU usage using Terraform
In this Article we will explore How to scale in and scale down EC2 instances based on there CPU utilization using Autoscaling and Loadbalancer
The source Code is available in
https://github.com/Jaffarterraform786/cloudwatch
you can just insert your access key and secret key to Run the Code.
Auto Scaling Groups :
Autoscaling Group also allows you to dynamically control your server pool size — increase it when your web servers are processing more traffic or tasks than usual, or we can decrease or Terminate Instances when Traffic is very less.
Launch Configuration:
AWS Launch Configuration is a template containing all instance settings to apply to each new launched by Auto Scaling Group instance.
Lets create an Loadbalancer ELB .
A Loadbalancer basically Receives the traffic from clients, shuffles the traffic and sends the traffic to the right instance.
USER_DATA:
The user-data
option is filled with a simple bash-script, which installs the apache web server and puts the instance’s local IP address to the index.html file, so we can see it after the instance is up and running.
LIFECYCLE
Lifecycle is a special instruction, which is used in declaring how new launch configuration rules applied during update. We’re using CREATE_BEFORE_DESTROY here to create new instances from a new launch configuration before destroying the old ones. This option used during rolling deployments..
Lets specify health_check configuration, which determines when Load Balancer should transition instances from healthy to unhealthy state and back depending on its ability to reach HTTP port 80 on the target instance.
Lets specify 2 Subnets, where our Load Balancer will look for (listener
configuration) launched instances and turned on CROSS _ZONE_ _LOAD_BALANCING feature, so we could have our instances in different Availability Zones.
Lets define a security Group
SECURITY GROUP:
- A security group acts a layer of security around EC2 instances.
- we can create 5 security groups for an instance.
- we can specify only allow rules , not deny rules.
Lets launch autoscaling group:
- There will be minimum one instance to serve the traffic.
- Auto Scaling Group will be launched with 2 instances and put each of them in separate Availability Zones in different Subnets.
- Auto Scaling Group will get information about instance availability from the
ELB
. - We’re set up collection for some Cloud Watch metrics to monitor our Auto Scaling Group state.
- Each instance launched from this Auto Scaling Group will have
Name
tag set toweb
.
Auto scaling policies:
aws_autoscaling_policy
defines how AWS should change Auto Scaling Group instances count in case ofaws_cloudwatch_metric_alarm
.
cooldown
option is needed to give our infrastructure some time (300 seconds) before increasing Auto Scaling Group again.
aws_cloudwatch_metric_alarm
is a straightforward alarm, which will be fired, if the total CPU utilization of all instances in our Auto Scaling Group is greater or equalthreshold
(60% CPU utilization) during 120 seconds.
Here we’re decreasing Auto Scaling Group size by one instance every 300 seconds if its total CPU utilization is less or equals 10%
Lets Do Terraform Plan to provision the resources.
As the desired capacity is 2 we can see two EC2 instnaces Launched in console.
Figure: Initially 2 instances Launched
Figure : out of 2 instances 1 shutting Down
We can see 1 instance is shutting down.
This is because of the Cloud Watch alarm , Here we’re decreasing Auto Scaling Group size by one instance every 300 seconds if its total CPU utilization is less or equals 10%.
Conclusion:
In this Article we have seen how to set up a dynamic Auto Scaling Group and Load Balancer to distribute traffic to your instances in several Availability Zones with cloudwatch Metrics.