Scaling is the ability to increase or decrease the compute capacity of your application. Scaling starts with an event, or scaling action, which instructs an Auto Scaling group to either launch or terminate Amazon EC2 instances.
Considerations for scaling policy:
There are 4 types of scaling policy:
In this lab, we will use Dynamic Scaling with target tracking rule. It is recommend by AWS because it is a easy to use and make your architecture simple. After you can deeply understand the system, you can change to another policy for better optimization. The script creates TargetTrackingScaling rule with average CPU on all instances in ASG. For example, the current ASG has 2 EC2 instances, each CPU workload is 60% and 70%, so the average CPU value is 65%, so the scaling process won’t work. When the first instance’s CPU increase to 80%, now the average CPU value is 85%, and scaling process will work.
Choose metrics
You can create target tracking scaling policies with either predefined metrics or custom metrics. AWS has 4 predefined metrics that you can choose when create ASG on Console: ASGAverageCPUUtilization, ASGAverageNetworkIn, ASGAverageNetworkOut, ALBRequestCountPerTarget.
resource "aws_autoscaling_policy" "cpu" {
name = "cpu-auto-scaling"
autoscaling_group_name = aws_autoscaling_group.web_asg.name
policy_type = "TargetTrackingScaling"
estimated_instance_warmup = 60
enabled = true
target_tracking_configuration {
predefined_metric_specification {
predefined_metric_type = "ASGAverageCPUUtilization"
}
target_value = 70
}
}
